From a2bf62bc61457a57000dbec2fef295a728f86dbc Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Sat, 11 Nov 2023 05:55:19 -0500 Subject: [PATCH 1/4] Use only ruff for Python linting and formatting --- templates/.github/workflows/black.yml | 10 --- templates/.github/workflows/flake8.yml | 19 ------ .../.github/workflows/python-formatting.yml | 10 +++ .../.github/workflows/python-linting.yml | 8 +++ templates/.pre-commit-config.yaml | 20 +++--- templates/pyproject.toml | 28 --------- templates/pyrightconfig.json | 8 +++ templates/ruff.toml | 61 +++++++++++++++++++ 8 files changed, 95 insertions(+), 69 deletions(-) delete mode 100644 templates/.github/workflows/black.yml delete mode 100644 templates/.github/workflows/flake8.yml create mode 100644 templates/.github/workflows/python-formatting.yml create mode 100644 templates/.github/workflows/python-linting.yml delete mode 100644 templates/pyproject.toml create mode 100644 templates/pyrightconfig.json create mode 100644 templates/ruff.toml diff --git a/templates/.github/workflows/black.yml b/templates/.github/workflows/black.yml deleted file mode 100644 index 6f47e98f..00000000 --- a/templates/.github/workflows/black.yml +++ /dev/null @@ -1,10 +0,0 @@ -name: Check black formatting - -on: [push] - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: psf/black@stable diff --git a/templates/.github/workflows/flake8.yml b/templates/.github/workflows/flake8.yml deleted file mode 100644 index 621b03da..00000000 --- a/templates/.github/workflows/flake8.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: Python flake8 check - -on: [push] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Set up Python 3.10 - uses: actions/setup-python@v4 - with: - python-version: "3.10" - - name: Lint with flake8 - run: | - pip install flake8 - flake8 . --exclude=docs --count --ignore=W503,W504,F541,E203 --max-line-length=100 --show-source --statistics diff --git a/templates/.github/workflows/python-formatting.yml b/templates/.github/workflows/python-formatting.yml new file mode 100644 index 00000000..4dc79f7e --- /dev/null +++ b/templates/.github/workflows/python-formatting.yml @@ -0,0 +1,10 @@ +name: check format using ruff +on: [push] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 + with: + args: format --check diff --git a/templates/.github/workflows/python-linting.yml b/templates/.github/workflows/python-linting.yml new file mode 100644 index 00000000..5b842f0c --- /dev/null +++ b/templates/.github/workflows/python-linting.yml @@ -0,0 +1,8 @@ +name: lint code using ruff +on: [push] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 diff --git a/templates/.pre-commit-config.yaml b/templates/.pre-commit-config.yaml index e3e5bcb9..f055986a 100644 --- a/templates/.pre-commit-config.yaml +++ b/templates/.pre-commit-config.yaml @@ -1,13 +1,9 @@ repos: -- repo: https://github.com/psf/black - rev: 23.3.0 - hooks: - - id: black - language_version: python3.10 - -- repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - name: isort (python) - language_version: python3.10 +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.1.5 + hooks: + # Run the linter. + - id: ruff + # Run the formatter. + - id: ruff-format diff --git a/templates/pyproject.toml b/templates/pyproject.toml deleted file mode 100644 index 006a70bb..00000000 --- a/templates/pyproject.toml +++ /dev/null @@ -1,28 +0,0 @@ -[tool.black] -include = '\.pyi?$' -exclude = ''' -/( - \.git - | \.mypy_cache - | \.tox - | \.venv - | \.vscode - | \.eggs - | _build - | buck-out - | build - | dist - | docs -)/ -''' - -[tool.isort] -profile = "black" - -[tool.pyright] -ignore = [ - "**/node_modules", - "**/__pycache__", - "**/*.ipynb", - ".git" -] diff --git a/templates/pyrightconfig.json b/templates/pyrightconfig.json new file mode 100644 index 00000000..7fef666c --- /dev/null +++ b/templates/pyrightconfig.json @@ -0,0 +1,8 @@ +{ + "ignore": [ + "**/node_modules", + "**/__pycache__", + "**/*.ipynb", + ".git" + ] +} \ No newline at end of file diff --git a/templates/ruff.toml b/templates/ruff.toml new file mode 100644 index 00000000..93ad071c --- /dev/null +++ b/templates/ruff.toml @@ -0,0 +1,61 @@ +# Copied originally from pandas +target-version = "py310" + +# fix = true +unfixable = [] + +select = [ + "I", # isort + "F", # pyflakes + "E", "W", # pycodestyle + "YTT", # flake8-2020 + "B", # flake8-bugbear + "Q", # flake8-quotes + "T10", # flake8-debugger + "INT", # flake8-gettext + "PLC", "PLE", "PLR", "PLW", # pylint + "PIE", # misc lints + "PYI", # flake8-pyi + "TID", # tidy imports + "ISC", # implicit string concatenation + "TCH", # type-checking imports + "C4", # comprehensions + "PGH" # pygrep-hooks +] + +ignore = [ + "ISC001", # Disable this for compatibility with ruff format + "B028", # No explicit `stacklevel` keyword argument found + "B905", # `zip()` without an explicit `strict=` parameter + "E402", # module level import not at top of file + "E731", # do not assign a lambda expression, use a def + "PLC1901", # compare-to-empty-string + "PLR0911", # Too many returns + "PLR0912", # Too many branches + "PLR0913", # Too many arguments to function call + "PLR2004", # Magic number + "PYI021", # Docstrings should not be included in stubs + "PLR0915", # Too many statements +] + +# TODO : fix these and stop ignoring. Commented out ones are common and OK to except. +extend-ignore = [ + "PGH004", # Use specific rule codes when using `noqa` +# "C401", # Unnecessary generator (rewrite as a `set` comprehension) +# "C402", # Unnecessary generator (rewrite as a dict comprehension) +# "C405", # Unnecessary `list` literal (rewrite as a `set` literal) +# "C408", # Unnecessary `dict` call (rewrite as a literal) +# "C416", # Unnecessary `dict` comprehension (rewrite using `dict()`) +# "PGH002", # warn is deprecated in favor of warning +# "PYI056", # Calling `.append()` on `__all__` may not be supported by all type checkers +] + +extend-exclude = [ + "docs", +] + +[pycodestyle] +max-line-length = 100 # E501 reports lines that exceed the length of 100. + +[lint.extend-per-file-ignores] +"__init__.py" = ["E402", "F401", "F403"] From 6edb273ee944fe7069b772ad70375418312eb142 Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Mon, 13 Nov 2023 08:25:53 -0500 Subject: [PATCH 2/4] Add a few more useful rules --- templates/ruff.toml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/templates/ruff.toml b/templates/ruff.toml index 93ad071c..8b2b6b14 100644 --- a/templates/ruff.toml +++ b/templates/ruff.toml @@ -23,6 +23,16 @@ select = [ "PGH" # pygrep-hooks ] +# Some additional rules that are useful +extend-select = [ +"UP009", # UTF-8 encoding declaration is unnecessary +"SIM118", # Use `key in dict` instead of `key in dict.keys()` +"D205", # One blank line required between summary line and description +"ARG001", # Unused function argument +"RSE102", # Unnecessary parentheses on raised exception +"PERF401", # Use a list comprehension to create a transformed list +] + ignore = [ "ISC001", # Disable this for compatibility with ruff format "B028", # No explicit `stacklevel` keyword argument found From 3c5320e2b74835f69e0119f5ed2e09ec6986861a Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Mon, 13 Nov 2023 08:57:16 -0500 Subject: [PATCH 3/4] More tweaks --- templates/ruff.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/ruff.toml b/templates/ruff.toml index 8b2b6b14..62bd58b5 100644 --- a/templates/ruff.toml +++ b/templates/ruff.toml @@ -43,9 +43,8 @@ ignore = [ "PLR0911", # Too many returns "PLR0912", # Too many branches "PLR0913", # Too many arguments to function call - "PLR2004", # Magic number - "PYI021", # Docstrings should not be included in stubs "PLR0915", # Too many statements + "PLR2004", # Magic number ] # TODO : fix these and stop ignoring. Commented out ones are common and OK to except. @@ -69,3 +68,4 @@ max-line-length = 100 # E501 reports lines that exceed the length of 100. [lint.extend-per-file-ignores] "__init__.py" = ["E402", "F401", "F403"] +"**/tests/test_*.py" = ["D205"] # Don't worry about test docstrings From cd0daf3d5bf7f3a6800cdfa2ac8263e6006f0d6c Mon Sep 17 00:00:00 2001 From: Tom Aldcroft Date: Mon, 13 Nov 2023 09:24:40 -0500 Subject: [PATCH 4/4] Add ARG001 exclusion for tests --- templates/ruff.toml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/templates/ruff.toml b/templates/ruff.toml index 62bd58b5..be100189 100644 --- a/templates/ruff.toml +++ b/templates/ruff.toml @@ -68,4 +68,7 @@ max-line-length = 100 # E501 reports lines that exceed the length of 100. [lint.extend-per-file-ignores] "__init__.py" = ["E402", "F401", "F403"] -"**/tests/test_*.py" = ["D205"] # Don't worry about test docstrings +# For tests: +# - D205: Don't worry about test docstrings +# - ARG001: Unused function argument false positives for some fixtures +"**/tests/test_*.py" = ["D205", "ARG001"]