Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ruff to lint Python code #518

Merged
merged 2 commits into from
Nov 9, 2024
Merged

Use ruff to lint Python code #518

merged 2 commits into from
Nov 9, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Nov 9, 2024

Ruff supports over 800 lint rules and can be used to replace Flake8 (plus dozens of plugins), isort, pydocstyle, yesqa, eradicate, pyupgrade, and autoflake, all while executing (in Rust) tens or hundreds of times faster than any individual tool.

ruff check --output-format=github provides intuitive GitHub Annotations to contributors...

image

Related to

@ulgens

@cclauss cclauss force-pushed the ruff branch 2 times, most recently from 1a20cf3 to 00f15fe Compare November 9, 2024 03:50
Copy link
Contributor

@dancergraham dancergraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff is a great tool. This allows dev workflows with constant linting. It makes no significant difference to ci times as flake 8 runs in parallel with the tests and finishes first.

Django uses flake8 but I don't see a good reason why this library needs to wait for django to move

I've looked through the changes and can't see anything which should break.

]
ignore = ["F401"]

[tool.ruff.lint.mccabe]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why complexity 23 ?

Copy link
Contributor Author

@cclauss cclauss Nov 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set it to 22 and see what happens...

% pipx run ruff check --config mccabe.max-complexity=22

line-length = 120
target-version = "py38"

[tool.ruff.lint]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why this set of lint rules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the ones that currently pass. Future PRs can uncomment rules that might be of interest. We do not want this PR to contain so many changes that it slows the review process.

@@ -41,16 +41,15 @@ def render(self, context):
def handle_token(cls, parser, token, kind, condition):
bits = token.split_contents()
if len(bits) < 2:
raise template.TemplateSyntaxError("%r tag requires an argument" %
bits[0])
raise template.TemplateSyntaxError(f"{bits[0]!r} tag requires an argument")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: use repr(bits[0]) for readability in the spirit of https://peps.python.org/pep-0498/#s-r-and-a-are-redundant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is about automatic changes, not manual changes. Turning on different rules may make other automatic changes.

@dancergraham
Copy link
Contributor

I think the pre-commit.yaml file needs to be updated as well

@cclauss cclauss requested a review from dancergraham November 9, 2024 14:47
Copy link
Contributor

@dancergraham dancergraham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm new around here so reluctant to personally approve such a big PR but it looks OK to me


[tool.ruff.lint.per-file-ignores]
"docs/conf.py" = ["INP001"]
"test_app/models.py" = ["DJ008"] # FIXME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: nice to highlight this in the comment


[tool.ruff.lint.pylint]
allow-magic-value-types = ["float", "int", "str"]
max-args = 6 # default is 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: nice to have the defaults mentioned here as a possible future target for refactoring

"ICN", # flake8-import-conventions
"INP", # flake8-no-pep420
"INT", # flake8-gettext
"NPY", # NumPy-specific rules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this library doesn't use numpy or pandas and seems unlikely to start doing so so these could be omitted for a minor gain in readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this library doesn't use numpy or pandas and seems unlikely to start doing so

Famous last words. You never know when code will be forked or when contributors will have new ideas. Given that ruff can lint the entire CPython codebase in less than a third of a second with no cache, I take the stance that if a family of rules pass then run them.

@clintonb clintonb merged commit 29b36b6 into jazzband:master Nov 9, 2024
10 checks passed
@cclauss cclauss deleted the ruff branch November 9, 2024 15:52
MatthewEthanTam pushed a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam added a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam pushed a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam added a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam pushed a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam added a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
MatthewEthanTam pushed a commit to MatthewEthanTam/django-waffle that referenced this pull request Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants