misc,python: Run pre-commit run --all-files
Applies the `pyupgrade` hook to all files in the repo. Change-Id: I9879c634a65c5fcaa9567c63bc5977ff97d5d3bf
This commit is contained in:
@@ -101,7 +101,7 @@ def lang_type(filename, firstline=None, openok=True):
|
||||
# if a first line was not provided but the file is ok to open,
|
||||
# grab the first line of the file.
|
||||
if firstline is None and openok:
|
||||
handle = open(filename, "r")
|
||||
handle = open(filename)
|
||||
firstline = handle.readline()
|
||||
handle.close()
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
class _neg_inf(object):
|
||||
class _neg_inf:
|
||||
"""This object always compares less than any other object"""
|
||||
|
||||
def __repr__(self):
|
||||
@@ -53,7 +53,7 @@ class _neg_inf(object):
|
||||
neg_inf = _neg_inf()
|
||||
|
||||
|
||||
class _pos_inf(object):
|
||||
class _pos_inf:
|
||||
"""This object always compares greater than any other object"""
|
||||
|
||||
def __repr__(self):
|
||||
@@ -176,7 +176,7 @@ class Region(tuple):
|
||||
return self[1] > other
|
||||
|
||||
|
||||
class Regions(object):
|
||||
class Regions:
|
||||
"""A set of regions (ranges). Basically a region with holes.
|
||||
Includes utility functions to merge regions and figure out if
|
||||
something is in one of the regions."""
|
||||
|
||||
@@ -43,7 +43,7 @@ from .region import *
|
||||
from .style import modified_regions
|
||||
|
||||
|
||||
class AbstractRepo(object, metaclass=ABCMeta):
|
||||
class AbstractRepo(metaclass=ABCMeta):
|
||||
def file_path(self, fname):
|
||||
"""Get the absolute path to a file relative within the repository. The
|
||||
input file name must be a valid path within the repository.
|
||||
@@ -76,7 +76,7 @@ class AbstractRepo(object, metaclass=ABCMeta):
|
||||
to the repository root.
|
||||
|
||||
"""
|
||||
with open(self.file_path(name), "r") as f:
|
||||
with open(self.file_path(name)) as f:
|
||||
return f.read()
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -94,7 +94,7 @@ def _include_matcher(keyword="#include", delim="<>"):
|
||||
"""Match an include statement and return a (keyword, file, extra)
|
||||
duple, or a touple of None values if there isn't a match."""
|
||||
|
||||
rex = re.compile(r"^(%s)\s*%s(.*)%s(.*)$" % (keyword, delim[0], delim[1]))
|
||||
rex = re.compile(rf"^({keyword})\s*{delim[0]}(.*){delim[1]}(.*)$")
|
||||
|
||||
def matcher(context, line):
|
||||
m = rex.match(line)
|
||||
@@ -146,7 +146,7 @@ def _include_matcher_main():
|
||||
return matcher
|
||||
|
||||
|
||||
class SortIncludes(object):
|
||||
class SortIncludes:
|
||||
# different types of includes for different sorting of headers
|
||||
# <Python.h> - Python header needs to be first if it exists
|
||||
# <*.h> - system headers (directories before files)
|
||||
@@ -155,17 +155,21 @@ class SortIncludes(object):
|
||||
# "*" - M5 headers (directories before files)
|
||||
includes_re = (
|
||||
("main", '""', _include_matcher_main()),
|
||||
("python", "<>", _include_matcher_fname("^Python\.h$")),
|
||||
("python", "<>", _include_matcher_fname(r"^Python\.h$")),
|
||||
(
|
||||
"pybind",
|
||||
'""',
|
||||
_include_matcher_fname("^pybind11/.*\.h$", delim='""'),
|
||||
_include_matcher_fname(r"^pybind11/.*\.h$", delim='""'),
|
||||
),
|
||||
("m5shared", "<>", _include_matcher_fname("^gem5/")),
|
||||
("c", "<>", _include_matcher_fname("^.*\.h$")),
|
||||
("stl", "<>", _include_matcher_fname("^\w+$")),
|
||||
("cc", "<>", _include_matcher_fname("^.*\.(hh|hxx|hpp|H)$")),
|
||||
("m5header", '""', _include_matcher_fname("^.*\.h{1,2}$", delim='""')),
|
||||
("c", "<>", _include_matcher_fname(r"^.*\.h$")),
|
||||
("stl", "<>", _include_matcher_fname(r"^\w+$")),
|
||||
("cc", "<>", _include_matcher_fname(r"^.*\.(hh|hxx|hpp|H)$")),
|
||||
(
|
||||
"m5header",
|
||||
'""',
|
||||
_include_matcher_fname(r"^.*\.h{1,2}$", delim='""'),
|
||||
),
|
||||
("swig0", "<>", _include_matcher(keyword="%import")),
|
||||
("swig1", "<>", _include_matcher(keyword="%include")),
|
||||
("swig2", '""', _include_matcher(keyword="%import", delim='""')),
|
||||
@@ -266,8 +270,7 @@ class SortIncludes(object):
|
||||
|
||||
# Output pending includes, a new line between, and the
|
||||
# current l.
|
||||
for include in self.dump_includes():
|
||||
yield include
|
||||
yield from self.dump_includes()
|
||||
yield ""
|
||||
yield line
|
||||
else:
|
||||
@@ -276,8 +279,7 @@ class SortIncludes(object):
|
||||
|
||||
# We've reached EOF, so dump any pending includes
|
||||
if processing_includes:
|
||||
for include in self.dump_includes():
|
||||
yield include
|
||||
yield from self.dump_includes()
|
||||
|
||||
|
||||
# default language types to try to apply our sorting rules to
|
||||
|
||||
@@ -52,7 +52,7 @@ trail = re.compile(r"([ \t]+)$")
|
||||
any_control = re.compile(r"\b(if|while|for)([ \t]*)\(")
|
||||
|
||||
|
||||
class UserInterface(object, metaclass=ABCMeta):
|
||||
class UserInterface(metaclass=ABCMeta):
|
||||
def __init__(self, verbose=False):
|
||||
self.verbose = verbose
|
||||
|
||||
@@ -118,8 +118,8 @@ style_ignores = [
|
||||
# Only include Scons files and those with extensions that suggest source
|
||||
# code
|
||||
_re_only(
|
||||
"^((.*\/)?(SConscript|SConstruct)|"
|
||||
".*\.(c|h|cc|hh|cpp|hpp|isa|proto))$"
|
||||
r"^((.*\/)?(SConscript|SConstruct)|"
|
||||
r".*\.(c|h|cc|hh|cpp|hpp|isa|proto))$"
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ def _modified_regions(old, new):
|
||||
return regions
|
||||
|
||||
|
||||
class Verifier(object, metaclass=ABCMeta):
|
||||
class Verifier(metaclass=ABCMeta):
|
||||
"""Base class for style verifiers
|
||||
|
||||
Verifiers check for style violations and optionally fix such
|
||||
@@ -292,10 +292,18 @@ class Whitespace(LineVerifier):
|
||||
- No trailing whitespace
|
||||
"""
|
||||
|
||||
languages = set(
|
||||
("C", "C++", "swig", "python", "asm", "isa", "scons", "make", "dts")
|
||||
)
|
||||
trail_only = set(("make", "dts"))
|
||||
languages = {
|
||||
"C",
|
||||
"C++",
|
||||
"swig",
|
||||
"python",
|
||||
"asm",
|
||||
"isa",
|
||||
"scons",
|
||||
"make",
|
||||
"dts",
|
||||
}
|
||||
trail_only = {"make", "dts"}
|
||||
|
||||
test_name = "whitespace"
|
||||
opt_name = "white"
|
||||
@@ -345,7 +353,7 @@ class SortedIncludes(Verifier):
|
||||
opt_name = "include"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(SortedIncludes, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
self.sort_includes = sort_includes.SortIncludes()
|
||||
|
||||
def check(self, filename, regions=all_regions, fobj=None, silent=False):
|
||||
@@ -404,7 +412,7 @@ class SortedIncludes(Verifier):
|
||||
class ControlSpace(LineVerifier):
|
||||
"""Check for exactly one space after if/while/for"""
|
||||
|
||||
languages = set(("C", "C++"))
|
||||
languages = {"C", "C++"}
|
||||
test_name = "spacing after if/while/for"
|
||||
opt_name = "control"
|
||||
|
||||
@@ -420,7 +428,7 @@ class ControlSpace(LineVerifier):
|
||||
|
||||
|
||||
class LineLength(LineVerifier):
|
||||
languages = set(("C", "C++", "swig", "python", "asm", "isa", "scons"))
|
||||
languages = {"C", "C++", "swig", "python", "asm", "isa", "scons"}
|
||||
test_name = "line length"
|
||||
opt_name = "length"
|
||||
|
||||
@@ -439,7 +447,7 @@ class LineLength(LineVerifier):
|
||||
|
||||
|
||||
class ControlCharacters(LineVerifier):
|
||||
languages = set(("C", "C++", "swig", "python", "asm", "isa", "scons"))
|
||||
languages = {"C", "C++", "swig", "python", "asm", "isa", "scons"}
|
||||
test_name = "control character"
|
||||
opt_name = "ascii"
|
||||
|
||||
@@ -455,7 +463,7 @@ class ControlCharacters(LineVerifier):
|
||||
|
||||
|
||||
class BoolCompare(LineVerifier):
|
||||
languages = set(("C", "C++", "python"))
|
||||
languages = {"C", "C++", "python"}
|
||||
test_name = "boolean comparison"
|
||||
opt_name = "boolcomp"
|
||||
|
||||
@@ -503,22 +511,22 @@ class StructureBraces(LineVerifier):
|
||||
: public BaseClass {
|
||||
"""
|
||||
|
||||
languages = set(("C", "C++"))
|
||||
languages = {"C", "C++"}
|
||||
test_name = "structure opening brace position"
|
||||
opt_name = "structurebrace"
|
||||
|
||||
# Matches the indentation of the line
|
||||
regex_indentation = "(?P<indentation>\s*)"
|
||||
regex_indentation = r"(?P<indentation>\s*)"
|
||||
# Matches an optional "typedef" before the keyword
|
||||
regex_typedef = "(?P<typedef>(typedef\s+)?)"
|
||||
regex_typedef = r"(?P<typedef>(typedef\s+)?)"
|
||||
# Matches the structure's keyword
|
||||
regex_keyword = "(?P<keyword>class|struct|enum|union)"
|
||||
# A negative lookahead to avoid incorrect matches with variable's names
|
||||
# e.g., "classifications = {" should not be fixed here.
|
||||
regex_avoid = "(?![^\{\s])"
|
||||
regex_avoid = r"(?![^\{\s])"
|
||||
# Matches anything after the keyword and before the opening brace.
|
||||
# e.g., structure name, base type, type of inheritance, etc
|
||||
regex_name = "(?P<name>[^\{]*)"
|
||||
regex_name = r"(?P<name>[^\{]*)"
|
||||
# Matches anything after the opening brace, which should be
|
||||
# parsed recursively
|
||||
regex_extra = "(?P<extra>.*)$"
|
||||
@@ -529,7 +537,7 @@ class StructureBraces(LineVerifier):
|
||||
+ regex_keyword
|
||||
+ regex_avoid
|
||||
+ regex_name
|
||||
+ "\{"
|
||||
+ r"\{"
|
||||
+ regex_extra
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user