Skip to content

Commit

Permalink
Use ruff to lint Python code
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Jul 27, 2023
1 parent be05c2c commit 344bf58
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 29 deletions.
8 changes: 0 additions & 8 deletions .flake8

This file was deleted.

6 changes: 1 addition & 5 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ jobs:
python -m pip install tox-gh-actions
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
run: ruff --format=github .

typecheck:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To be mergeable, patches must:
- not change existing tests without a *very* good reason,
- add tests for new code (bug fixes should include regression tests, new
features should have relevant tests),
- not introduce any new flake8_ errors (run ``./run.sh lint``),
- not introduce any new ruff_ errors (run ``./run.sh lint``),
- not introduce any new mypy_ errors (run ``./run.sh typecheck``),
- include updated source translations (run ``./run.sh makemessages`` and ``./run.sh compilemessages``),
- document any new features, and
Expand All @@ -72,6 +72,6 @@ with it.

.. _open a new issue: https://github.com/django-waffle/django-waffle/issues/new
.. _Fork: https://github.com/django-waffle/django-waffle/fork
.. _flake8: https://pypi.python.org/pypi/flake8
.. _ruff: https://pypi.python.org/pypi/ruff
.. _mypy: https://www.mypy-lang.org/
.. _good commit message: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
4 changes: 2 additions & 2 deletions docs/about/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To be mergeable, patches must:
- not change existing tests without a *very* good reason,
- add tests for new code (bug fixes should include regression tests, new
features should have relevant tests),
- not introduce any new flake8_ errors (run ``./run.sh lint``),
- not introduce any new ruff_ errors (run ``./run.sh lint``),
- document any new features, and
- have a `good commit message`_.

Expand All @@ -70,5 +70,5 @@ with it.

.. _open a new issue: https://github.com/django-waffle/django-waffle/issues/new
.. _Fork: https://github.com/django-waffle/django-waffle/fork
.. _flake8: https://pypi.python.org/pypi/flake8
.. _ruff: https://pypi.python.org/pypi/ruff
.. _good commit message: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
84 changes: 84 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,87 @@ strict_equality = true
[[tool.mypy.overrides]]
module = ["django.*"]
ignore_missing_imports = true

[tool.ruff]
select = [
"AIR", # Airflow
"ASYNC", # flake8-async
"BLE", # flake8-blind-except
"C90", # McCabe cyclomatic complexity
"CPY", # flake8-copyright
"DJ", # flake8-django
"DTZ", # flake8-datetimez
"E", # pycodestyle errors
"F", # Pyflakes
"FIX", # flake8-fixme
"FLY", # flynt
"G", # flake8-logging-format
"ICN", # flake8-import-conventions
"INP", # flake8-no-pep420
"INT", # flake8-gettext
"NPY", # NumPy-specific rules
"PD", # pandas-vet
"PIE", # flake8-pie
"PL", # Pylint
"PYI", # flake8-pyi
"RSE", # flake8-raise
"SLOT", # flake8-slots
"T10", # flake8-debugger
"T20", # flake8-print
"TD", # flake8-todos
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle warnings
"YTT", # flake8-2020
# "A", # flake8-builtins
# "ANN", # flake8-annotations
# "ARG", # flake8-unused-arguments
# "B", # flake8-bugbear
# "C4", # flake8-comprehensions
# "COM", # flake8-commas
# "D", # pydocstyle
# "EM", # flake8-errmsg
# "ERA", # eradicate
# "EXE", # flake8-executable
# "FA", # flake8-future-annotations
# "FBT", # flake8-boolean-trap
# "I", # isort
# "ISC", # flake8-implicit-str-concat
# "N", # pep8-naming
# "PERF", # Perflint
# "PGH", # pygrep-hooks
# "PT", # flake8-pytest-style
# "PTH", # flake8-use-pathlib
# "Q", # flake8-quotes
# "RET", # flake8-return
# "RUF", # Ruff-specific rules
# "S", # flake8-bandit
# "SIM", # flake8-simplify
# "SLF", # flake8-self
# "TCH", # flake8-type-checking
# "TRY", # tryceratops
]
# Files not checked:
# - migrations: most of these are autogenerated and don't need a check
# - docs: contains autogenerated code that doesn't need a check
exclude = [
"*/migrations/*",
"docs",
]
ignore = ["F401"]
line-length = 120
target-version = "py38"

[tool.ruff.mccabe]
max-complexity = 23

[tool.ruff.per-file-ignores]
"test_app/models.py" = ["DJ008"] # FIXME
"waffle/models.py" = ["DJ012"] # FIXME

[tool.ruff.pylint]
allow-magic-value-types = ["float", "int", "str"]
max-args = 6 # default is 5
max-branches = 23 # default is 12
max-returns = 13 # default is 6
max-statements = 51 # default is 50
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ Django
django-jinja>=2.4.1,<3
transifex-client

flake8
mypy
ruff
tox
2 changes: 1 addition & 1 deletion requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
flake8
ruff
tox
4 changes: 2 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export DJANGO_SETTINGS_MODULE="test_settings"
usage() {
echo "USAGE: $0 [command]"
echo " test - run the waffle tests"
echo " lint - run flake8"
echo " lint - run ruff"
echo " typecheck - run mypy"
echo " shell - open the Django shell"
echo " makemigrations - create a schema migration"
Expand All @@ -20,7 +20,7 @@ case "$CMD" in
"test" )
DJANGO_SETTINGS_MODULE=test_settings django-admin test waffle $@ ;;
"lint" )
flake8 waffle $@ ;;
ruff . ;;
"typecheck" )
mypy waffle $@ ;;
"shell" )
Expand Down
2 changes: 1 addition & 1 deletion test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Make filepaths relative to settings.
ROOT = os.path.dirname(os.path.abspath(__file__))
path = lambda *a: os.path.join(ROOT, *a)
path = lambda *a: os.path.join(ROOT, *a) # noqa: E731

DEBUG = True
TEST_RUNNER = 'django.test.runner.DiscoverRunner'
Expand Down
8 changes: 2 additions & 6 deletions waffle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,8 @@ def get_waffle_model(setting_name: str) -> (
try:
return django_apps.get_model(flag_model_name)
except ValueError:
raise ImproperlyConfigured("WAFFLE_{} must be of the form 'app_label.model_name'".format(
setting_name
))
raise ImproperlyConfigured(f"WAFFLE_{setting_name} must be of the form 'app_label.model_name'")
except LookupError:
raise ImproperlyConfigured(
"WAFFLE_{} refers to model '{}' that has not been installed".format(
setting_name, flag_model_name
)
f"WAFFLE_{setting_name} refers to model '{flag_model_name}' that has not been installed"
)
2 changes: 1 addition & 1 deletion waffle/management/commands/waffle_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def handle(self, *args: Any, **options: Any) -> None:
try:
percent = float(percent)
if not (0.0 <= percent <= 100.0):
raise ValueError()
raise ValueError
except ValueError:
raise CommandError('You need to enter a valid percentage value.')

Expand Down

0 comments on commit 344bf58

Please sign in to comment.