Skip to content

Commit

Permalink
build: replace Python linter flake8 with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Apr 12, 2023
1 parent 56ccd59 commit fc555cd
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

3 changes: 2 additions & 1 deletion .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
!.eslintignore
!.eslintrc.js
!.eslintrc.yaml
!.flake8
!.gitattributes
!.github
!.gitignore
Expand Down
19 changes: 6 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion tools/checkimports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tools/gyp/.github/workflows/Python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/gyp_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
12 changes: 6 additions & 6 deletions tools/gyp/pylib/gyp/generator/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)) $@
Expand Down Expand Up @@ -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 = """\
Expand All @@ -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 = """\
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -573,7 +573,7 @@ def CalculateGeneratorInputInfo(params):
.PHONY: FORCE_DO_CMD
FORCE_DO_CMD:
""" # noqa: E501
"""
)

SHARED_HEADER_MAC_COMMANDS = """
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/pylib/gyp/generator/msvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ def _EscapeEnvironmentVariableExpansion(s):
Returns:
The escaped string.
""" # noqa: E731,E123,E501
"""
s = s.replace("%", "%%")
return s

Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/pylib/gyp/generator/ninja.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/pylib/gyp/xcode_emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/pylib/gyp/xcodeproj_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/gyp/requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
flake8
pytest
ruff
2 changes: 1 addition & 1 deletion tools/icu/shrink-icu-src.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::]
Expand Down
2 changes: 1 addition & 1 deletion tools/mkssldef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit fc555cd

Please sign in to comment.