From fc555cdf458d002d34be643c5ee72a2d57f2b099 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 12 Apr 2023 03:00:48 +0200 Subject: [PATCH] build: replace Python linter flake8 with ruff --- .flake8 | 3 -- .github/workflows/linters.yml | 3 +- .gitignore | 1 - Makefile | 19 +++------ pyproject.toml | 41 ++++++++++++++++++++ tools/checkimports.py | 2 +- tools/gyp/.github/workflows/Python_tests.yml | 8 ++-- tools/gyp/gyp_main.py | 2 +- tools/gyp/pylib/gyp/generator/make.py | 12 +++--- tools/gyp/pylib/gyp/generator/msvs.py | 2 +- tools/gyp/pylib/gyp/generator/ninja.py | 2 +- tools/gyp/pylib/gyp/xcode_emulation.py | 2 +- tools/gyp/pylib/gyp/xcodeproj_file.py | 2 +- tools/gyp/requirements_dev.txt | 2 +- tools/icu/shrink-icu-src.py | 2 +- tools/mkssldef.py | 2 +- 16 files changed, 68 insertions(+), 37 deletions(-) delete mode 100644 .flake8 create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 deleted file mode 100644 index eee7c33b89d262..00000000000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -exclude=.git,deps,lib,src,tools/gyp,tools/inspector_protocol,tools/pip,tools/v8_gypfiles/broken -ignore=E1,E2,E3,E4,E5,E7,W5,W6 diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml index 96eb4604a20b70..8339c96bb26910 100644 --- a/.github/workflows/linters.yml +++ b/.github/workflows/linters.yml @@ -129,7 +129,8 @@ jobs: run: npx envinfo - name: Lint Python run: | - make lint-py-build || true + make lint-py-build + pip install ruff make lint-py lint-yaml: if: github.event.pull_request.draft == false diff --git a/.gitignore b/.gitignore index b3399fa0df2655..e10a56d9de7926 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,6 @@ !.eslintignore !.eslintrc.js !.eslintrc.yaml -!.flake8 !.gitattributes !.github !.gitignore diff --git a/Makefile b/Makefile index 831f9613233881..fbf22dbcc4c3a0 100644 --- a/Makefile +++ b/Makefile @@ -1489,24 +1489,17 @@ cpplint: lint-cpp $(warning Please use lint-cpp instead of cpplint) .PHONY: lint-py-build -# python -m pip install flake8 +# python -m pip install ruff # Try with '--system' if it fails without; the system may have set '--user' lint-py-build: - $(info Pip installing flake8 linter on $(shell $(PYTHON) --version)...) - $(PYTHON) -m pip install --upgrade -t tools/pip/site-packages flake8 || \ - $(PYTHON) -m pip install --upgrade --system -t tools/pip/site-packages flake8 + $(info Pip installing ruff linter on $(shell $(PYTHON) --version)...) + $(PYTHON) -m pip install --upgrade --target tools/pip/site-packages ruff || \ + $(PYTHON) -m pip install --upgrade --system -target tools/pip/site-packages ruff .PHONY: lint-py -ifneq ("","$(wildcard tools/pip/site-packages/flake8)") -# Lints the Python code with flake8. -# Flag the build if there are Python syntax errors or undefined names +# Lints the Python code with ruff. lint-py: - PYTHONPATH=tools/pip $(PYTHON) -m flake8 --count --show-source --statistics . -else -lint-py: - $(warning Python linting with flake8 is not available) - $(warning Run 'make lint-py-build') -endif + ruff . .PHONY: lint-yaml-build # python -m pip install yamllint diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000000000..8e9f6b33862caf --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,41 @@ +[tool.ruff] +select = [ + "C90", # McCabe cyclomatic complexity + "E", # pycodestyle + "F", # Pyflakes + "ICN", # flake8-import-conventions + "INT", # flake8-gettext + "PLC", # Pylint conventions + "PLE", # Pylint errors + "PLR09", # Pylint refactoring: max-args, max-branches, max returns, max-statements + "PYI", # flake8-pyi + "RSE", # flake8-raise + "RUF", # Ruff-specific rules + "T10", # flake8-debugger + "TCH", # flake8-type-checking + "TID", # flake8-tidy-imports + "W", # pycodestyle + "YTT", # flake8-2020 +] +exclude = [ + "deps", + "tools/inspector_protocol", +] +ignore = [ + "E401", + "E402", + "E7", + "PLC1901", + "RUF005", +] +line-length = 172 +target-version = "py37" + +[tool.ruff.mccabe] +max-complexity = 100 + +[tool.ruff.pylint] +max-args = 12 +max-branches = 110 +max-returns = 12 +max-statements = 289 diff --git a/tools/checkimports.py b/tools/checkimports.py index b94919e3cc47e4..629569c06abd3f 100755 --- a/tools/checkimports.py +++ b/tools/checkimports.py @@ -8,7 +8,7 @@ import itertools def do_exist(file_name, lines, imported): - if not any(not re.match('using \w+::{0};'.format(imported), line) and + if not any(not re.match('using \\w+::{0};'.format(imported), line) and re.search('\\b{0}\\b'.format(imported), line) for line in lines): print('File "{0}" does not use "{1}"'.format(file_name, imported)) return False diff --git a/tools/gyp/.github/workflows/Python_tests.yml b/tools/gyp/.github/workflows/Python_tests.yml index 1cfa42f563ce5f..94a64606040019 100644 --- a/tools/gyp/.github/workflows/Python_tests.yml +++ b/tools/gyp/.github/workflows/Python_tests.yml @@ -11,19 +11,19 @@ jobs: max-parallel: 8 matrix: os: [macos-latest, ubuntu-latest] # , windows-latest] - python-version: ["3.7", "3.8", "3.9", "3.10"] + python-version: ["3.7", "3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v3 + uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements_dev.txt - - name: Lint with flake8 - run: flake8 . --ignore=E203,W503 --max-complexity=101 --max-line-length=88 --show-source --statistics + - name: Lint with ruff + run: ruff --format=github . - name: Test with pytest run: pytest # - name: Run doctests with pytest diff --git a/tools/gyp/gyp_main.py b/tools/gyp/gyp_main.py index f23dcdf882d1b0..754ce5b3cdc0c1 100755 --- a/tools/gyp/gyp_main.py +++ b/tools/gyp/gyp_main.py @@ -39,7 +39,7 @@ def UnixifyPath(path): # else the 'gyp' library will not be found path = UnixifyPath(sys.argv[0]) sys.path.insert(0, os.path.join(os.path.dirname(path), "pylib")) -import gyp # noqa: E402 +import gyp if __name__ == "__main__": sys.exit(gyp.script_main()) diff --git a/tools/gyp/pylib/gyp/generator/make.py b/tools/gyp/pylib/gyp/generator/make.py index e225326e1d09b6..bf9be1b02b7556 100644 --- a/tools/gyp/pylib/gyp/generator/make.py +++ b/tools/gyp/pylib/gyp/generator/make.py @@ -216,7 +216,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -bundle $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" # noqa: E501 +""" LINK_COMMANDS_ANDROID = """\ quiet_cmd_alink = AR($(TOOLSET)) $@ @@ -286,7 +286,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" # noqa: E501 +""" LINK_COMMANDS_OS400 = """\ @@ -304,7 +304,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@ cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS) -""" # noqa: E501 +""" LINK_COMMANDS_OS390 = """\ @@ -474,7 +474,7 @@ def CalculateGeneratorInputInfo(params): cmd_symlink = ln -sf "$<" "$@" %(link_commands)s -""" # noqa: E501 +""" r""" # Define an escape_quotes function to escape single quotes. # This allows us to handle quotes properly as long as we always use @@ -573,7 +573,7 @@ def CalculateGeneratorInputInfo(params): .PHONY: FORCE_DO_CMD FORCE_DO_CMD: -""" # noqa: E501 +""" ) SHARED_HEADER_MAC_COMMANDS = """ @@ -604,7 +604,7 @@ def CalculateGeneratorInputInfo(params): quiet_cmd_infoplist = INFOPLIST $@ cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@" -""" # noqa: E501 +""" def WriteRootHeaderSuffixRules(writer): diff --git a/tools/gyp/pylib/gyp/generator/msvs.py b/tools/gyp/pylib/gyp/generator/msvs.py index fd950057847980..5af21b37c255d0 100644 --- a/tools/gyp/pylib/gyp/generator/msvs.py +++ b/tools/gyp/pylib/gyp/generator/msvs.py @@ -756,7 +756,7 @@ def _EscapeEnvironmentVariableExpansion(s): Returns: The escaped string. - """ # noqa: E731,E123,E501 + """ s = s.replace("%", "%%") return s diff --git a/tools/gyp/pylib/gyp/generator/ninja.py b/tools/gyp/pylib/gyp/generator/ninja.py index 3db3771ac97855..d51c64230b7c72 100644 --- a/tools/gyp/pylib/gyp/generator/ninja.py +++ b/tools/gyp/pylib/gyp/generator/ninja.py @@ -2533,7 +2533,7 @@ def GenerateOutputForConfig(target_list, target_dicts, data, params, config_name description="SOLINK $lib", restat=True, command=mtime_preserving_solink_base - % {"suffix": "@$link_file_list"}, # noqa: E501 + % {"suffix": "@$link_file_list"}, rspfile="$link_file_list", rspfile_content=( "-Wl,--whole-archive $in $solibs -Wl," "--no-whole-archive $libs" diff --git a/tools/gyp/pylib/gyp/xcode_emulation.py b/tools/gyp/pylib/gyp/xcode_emulation.py index a75d8eeab7bda0..35062211a59307 100644 --- a/tools/gyp/pylib/gyp/xcode_emulation.py +++ b/tools/gyp/pylib/gyp/xcode_emulation.py @@ -1733,7 +1733,7 @@ def _GetXcodeEnv( "CONFIGURATION": configuration, "PRODUCT_NAME": xcode_settings.GetProductName(), # For FULL_PRODUCT_NAME see: - # /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX\ Product\ Types.xcspec # noqa: E501 + # /Developer/Platforms/MacOSX.platform/Developer/Library/Xcode/Specifications/MacOSX\ Product\ Types.xcspec "SRCROOT": srcroot, "SOURCE_ROOT": "${SRCROOT}", # This is not true for static libraries, but currently the env is only diff --git a/tools/gyp/pylib/gyp/xcodeproj_file.py b/tools/gyp/pylib/gyp/xcodeproj_file.py index 076eea37211179..62fb53646f7cdc 100644 --- a/tools/gyp/pylib/gyp/xcodeproj_file.py +++ b/tools/gyp/pylib/gyp/xcodeproj_file.py @@ -2770,7 +2770,7 @@ def __init__(self, properties=None, id=None, parent=None, path=None): self.path = path self._other_pbxprojects = {} # super - return XCContainerPortal.__init__(self, properties, id, parent) + XCContainerPortal.__init__(self, properties, id, parent) def Name(self): name = self.path diff --git a/tools/gyp/requirements_dev.txt b/tools/gyp/requirements_dev.txt index 28ecacab602938..71cc5395b4a5e9 100644 --- a/tools/gyp/requirements_dev.txt +++ b/tools/gyp/requirements_dev.txt @@ -1,2 +1,2 @@ -flake8 pytest +ruff diff --git a/tools/icu/shrink-icu-src.py b/tools/icu/shrink-icu-src.py index 3a9ba2fbfbf118..30d34070c9ae17 100644 --- a/tools/icu/shrink-icu-src.py +++ b/tools/icu/shrink-icu-src.py @@ -48,7 +48,7 @@ def print_size(fn): size = (os.stat(fn).st_size) / 1024000 print('%dM\t%s' % (size, fn)) -ignore_regex = re.compile('^.*\.(vcxproj|filters|nrm|icu|dat|xml|txt|ac|guess|m4|in|sub|py|mak)$') +ignore_regex = re.compile('^.*\\.(vcxproj|filters|nrm|icu|dat|xml|txt|ac|guess|m4|in|sub|py|mak)$') def icu_ignore(dir, files): subdir = dir[len(options.icusrc)+1::] diff --git a/tools/mkssldef.py b/tools/mkssldef.py index 4768d1ff0ff699..6b60142bb86baf 100755 --- a/tools/mkssldef.py +++ b/tools/mkssldef.py @@ -26,7 +26,7 @@ for filename in filenames: for line in open(filename).readlines(): - name, _, _, meta, _ = re.split('\s+', line) + name, _, _, meta, _ = re.split('\\s+', line) if any(p.match(name) for p in excludes): continue meta = meta.split(':') assert meta[0] in ('EXIST', 'NOEXIST')