diff --git a/cpplint.py b/cpplint.py index 6556c97..f9edf3c 100755 --- a/cpplint.py +++ b/cpplint.py @@ -1214,7 +1214,7 @@ def _IsSourceExtension(s): return s in GetNonHeaderExtensions() -class _IncludeState(object): +class _IncludeState: """Tracks line numbers for includes, and the order in which includes appear. include_list contains list of lists of (header, line number) pairs. @@ -1392,7 +1392,7 @@ def CheckNextIncludeOrder(self, header_type): return "" -class _CppLintState(object): +class _CppLintState: """Maintains module-wide state..""" def __init__(self): @@ -1637,7 +1637,7 @@ def _RestoreFilters(): _cpplint_state.RestoreFilters() -class _FunctionState(object): +class _FunctionState: """Tracks current function name and the number of lines in its body.""" _NORMAL_TRIGGER = 250 # for --v=0, 500 for --v=1, etc. @@ -1705,7 +1705,7 @@ class _IncludeError(Exception): pass -class FileInfo(object): +class FileInfo: """Provides utility functions for filenames. FileInfo provides easy access to the components of a file's path @@ -2104,7 +2104,7 @@ def ReplaceAlternateTokens(line): return line -class CleansedLines(object): +class CleansedLines: """Holds 4 copies of all lines with different preprocessing applied to them. 1) elided member contains lines without strings and comments. @@ -2973,7 +2973,7 @@ def IsForwardClassDeclaration(clean_lines, linenum): return re.match(r"^\s*(\btemplate\b)*.*class\s+\w+;\s*$", clean_lines[linenum]) -class _BlockInfo(object): +class _BlockInfo: """Stores information about a generic block of code.""" def __init__(self, linenum, seen_open_brace): @@ -3188,7 +3188,7 @@ def CheckEnd(self, filename, clean_lines, linenum, error): ) -class _PreprocessorInfo(object): +class _PreprocessorInfo: """Stores checkpoints of nesting stacks when #if/#else is seen.""" def __init__(self, stack_before_if): @@ -3202,7 +3202,7 @@ def __init__(self, stack_before_if): self.seen_else = False -class NestingState(object): +class NestingState: """Holds states related to parsing braces.""" def __init__(self): @@ -7417,7 +7417,7 @@ def ProcessConfigOverrides(filename): f"Invalid configuration option ({name}) in file {cfg_file}\n" ) - except IOError: + except OSError: _cpplint_state.PrintError( f"Skipping config file '{cfg_file}': Can't open for reading\n" ) @@ -7478,7 +7478,7 @@ def ProcessFile(filename, vlevel, extra_check_functions=None): else: lf_lines.append(linenum + 1) - except IOError: + except OSError: # TODO: Maybe make this have an exit code of 2 after all is done _cpplint_state.PrintError(f"Skipping input '{filename}': Can't open for reading\n") _RestoreFilters() diff --git a/cpplint_clitest.py b/cpplint_clitest.py index f20d54d..75a9733 100755 --- a/cpplint_clitest.py +++ b/cpplint_clitest.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8; -*- # # Copyright (c) 2009 Google Inc. All rights reserved. # @@ -162,8 +161,8 @@ def _run_and_compare(self, definition_file, args, expected_status, expected_out, # command to reproduce, do not forget first two lines have special meaning print("\ncd " + cwd + " && " + cmd + " " + args + " 2> ") (status, out, err) = run_shell_command(cmd, args, cwd) - self.assertEqual(expected_status, status, "bad command status %s" % status) - prefix = "Failed check in %s comparing to %s for command: %s" % (cwd, definition_file, cmd) + self.assertEqual(expected_status, status, f"bad command status {status}") + prefix = f"Failed check in {cwd} comparing to {definition_file} for command: {cmd}" compare("\n".join(expected_err), err.decode("utf8"), prefix=prefix, show_whitespace=True) compare("\n".join(expected_out), out.decode("utf8"), prefix=prefix, show_whitespace=True) diff --git a/cpplint_unittest.py b/cpplint_unittest.py index 33d3fc8..bae157d 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8; -*- # # Copyright (c) 2009 Google Inc. All rights reserved. # @@ -51,15 +50,13 @@ def codecs_latin_encode(x): - if sys.version_info < (3,): - return x return codecs.latin_1_encode(x)[0] # This class works as an error collector and replaces cpplint.Error # function for the unit tests. We also verify each category we see # is in cpplint._ERROR_CATEGORIES, to help keep that list up to date. -class ErrorCollector(object): +class ErrorCollector: # These are a global list, covering all categories seen ever. _ERROR_CATEGORIES = cpplint._ERROR_CATEGORIES _SEEN_ERROR_CATEGORIES = {} @@ -110,7 +107,7 @@ def RemoveIfPresent(self, substr): # This class is a lame mock of codecs. We do not verify filename, mode, or # encoding, but for the current use case it is not needed. -class MockIo(object): +class MockIo: def __init__(self, mock_file): # wrap list to allow "with open(mock)" class EnterableList(list): @@ -357,7 +354,7 @@ def testNestingInNamespace(self): # Test get line width. def testGetLineWidth(self): self.assertEqual(0, cpplint.GetLineWidth("")) - self.assertEqual(10, cpplint.GetLineWidth(str("x") * 10)) + self.assertEqual(10, cpplint.GetLineWidth("x" * 10)) self.assertEqual(16, cpplint.GetLineWidth("\u90fd|\u9053|\u5e9c|\u770c|\u652f\u5e81")) self.assertEqual(16, cpplint.GetLineWidth("都|道|府|県|支庁")) self.assertEqual(5 + 13 + 9, cpplint.GetLineWidth("d𝐱/dt" + "f : t ⨯ 𝐱 → ℝ" + "t ⨯ 𝐱 → ℝ")) @@ -1056,21 +1053,17 @@ def testMockMethod(self): self.assertEqual( 0, error_collector.Results().count( - ( - "Using deprecated casting style. " - "Use static_cast(...) instead " - "[readability/casting] [4]" - ) + "Using deprecated casting style. " + "Use static_cast(...) instead " + "[readability/casting] [4]" ), ) self.assertEqual( 1, error_collector.Results().count( - ( - "Using deprecated casting style. " - "Use static_cast(...) instead " - "[readability/casting] [4]" - ) + "Using deprecated casting style. " + "Use static_cast(...) instead " + "[readability/casting] [4]" ), ) @@ -4842,9 +4835,7 @@ def testRecursiveExclude(self): expected = [os.path.join("src", "one.cc")] cpplint._excludes = None - actual = cpplint.ParseArguments( - ["--recursive", "--exclude=src{0}t*".format(os.sep), "src"] - ) + actual = cpplint.ParseArguments(["--recursive", f"--exclude=src{os.sep}t*", "src"]) self.assertEqual(set(expected), set(actual)) expected = [os.path.join("src", "one.cc")] diff --git a/pyproject.toml b/pyproject.toml index 951958b..641c92c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -103,6 +103,7 @@ lint.select = [ "TC", # flake8-type-checking "TID", # flake8-tidy-imports "TRY", # tryceratops + "UP", # pyupgrade "W", # pycodestyle "YTT", # flake8-2020 # "ANN", # flake8-annotations @@ -124,7 +125,6 @@ lint.select = [ # "SLF", # flake8-self # "T20", # flake8-print # "TD", # flake8-todos - # "UP", # pyupgrade ] lint.ignore = [ "FBT003", # flake8-boolean-trap @@ -133,7 +133,7 @@ lint.ignore = [ "PIE790", # Unnecessary `pass` statement ] lint.per-file-ignores."cpplint.py" = [ "ICN001", "PERF401", "PLR5501", "PLW0603", "PLW2901" ] -lint.per-file-ignores."cpplint_unittest.py" = [ "FLY002", "PLW0604" ] +lint.per-file-ignores."cpplint_unittest.py" = [ "FLY002", "PLW0604", "UP031" ] lint.mccabe.max-complexity = 29 lint.pylint.allow-magic-value-types = [ "int", "str" ] lint.pylint.max-args = 10 # Default is 5 @@ -145,13 +145,13 @@ lint.pylint.max-returns = 9 # Default is 9 lint.pylint.max-statements = 74 # Default is 50 [tool.pylint.basic] +argument-rgx = "[a-z_][a-z0-9_]{0,49}$" +class-rgx = "[A-Z_][a-zA-Z0-9]+$" +const-rgx = "[a-zA-Z_][A-Za-z0-9_]{2,49}$" +function-rgx = "[A-Z_][A-Za-z0-9]{2,49}$|main" include-naming-hint = true method-rgx = "[A-Z_][A-Za-z0-9]{2,49}$|__init__|__str__|__contains__" -function-rgx = "[A-Z_][A-Za-z0-9]{2,49}$|main" -const-rgx = "[a-zA-Z_][A-Za-z0-9_]{2,49}$" variable-rgx = "[a-z_][a-z0-9_]{0,49}$" -argument-rgx = "[a-z_][a-z0-9_]{0,49}$" -class-rgx = "[A-Z_][a-zA-Z0-9]+$" [tool.pylint.messages-control] disable = [ @@ -180,25 +180,25 @@ reports = false score = false [tool.pylint.format] -indent-string = ' ' indent-after-paren = 4 +indent-string = ' ' max-module-lines = 10000 [tool.pylint.design] -max-locals = 25 -max-line-length = 100 +max-args = 20 max-attributes = 10 +max-bool-expr = 10 max-branches = 30 -max-args = 20 -max-statements = 75 +max-line-length = 100 +max-locals = 25 max-returns = 10 +max-statements = 75 min-public-methods = 0 -max-bool-expr = 10 [tool.pytest.ini_options] +# fail if coverage is under 90% +addopts = "--color=yes --cov-fail-under=90 --cov=cpplint" python_files = [ "*test.py" ] -testpaths = [ "." ] required_plugins = [ "pytest-cov", "pytest-timeout" ] +testpaths = [ "." ] timeout = 60 -# fail if coverage is under 90% -addopts = "--color=yes --cov-fail-under=90 --cov=cpplint"