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

[RUF021]: Add an autofix #9449

Merged
merged 3 commits into from
Jan 9, 2024
Merged

Conversation

AlexWaygood
Copy link
Member

Summary

This adds an autofix for the newly added RUF021 (see #9440).

The PR currently only adds an autofix to the diagnostic if:

  1. There are no line breaks inside the expression as it currently exists;
  2. We determine that we could safely add parentheses to the subexpression in question without the line getting too long.

If we know that both of the above are true, the fix is pretty simple to implement. I think it should also be pretty safe, as well -- we shouldn't be making any changes here that impact the AST.

Two remarks:

  1. It would be possible to add an autofix if the pre-existing expression spanned multiple lines, but it would be much harder, I think. You'd have to worry about whether there were comments at the end of lines or in between lines; you'd have to worry about indentation; you'd have to worry about whether the newly parenthesized subexpression should be formatted into a single line or should itself be split into several lines. (But maybe there's an easy way of resolving all those things that I'm not aware of 😄.)
  2. Currently the PR just doesn't add a fix if the resulting fix would cause the line to be too long. But maybe we could add the fix even then, but just mark it as unsafe? Not sure.

Test Plan

cargo test / cargo insta review

@AlexWaygood AlexWaygood added fixes Related to suggested fixes for violations rule Implementing or modifying a lint rule labels Jan 9, 2024
Copy link
Contributor

github-actions bot commented Jan 9, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+0 -30 violations, +114 -0 fixes in 11 projects; 32 projects unchanged)

DisnakeDev/disnake (+0 -0 violations, +10 -0 fixes)

ruff check --no-cache --exit-zero --preview

- disnake/ext/commands/converter.py:1168:12: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ disnake/ext/commands/converter.py:1168:12: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- disnake/interactions/base.py:1880:21: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ disnake/interactions/base.py:1880:21: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- disnake/interactions/base.py:1905:21: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ disnake/interactions/base.py:1905:21: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- disnake/interactions/base.py:1923:18: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ disnake/interactions/base.py:1923:18: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- disnake/interactions/base.py:213:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ disnake/interactions/base.py:213:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

RasaHQ/rasa (+0 -0 violations, +12 -0 fixes)

ruff check --no-cache --exit-zero --preview

- rasa/cli/utils.py:74:27: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rasa/cli/utils.py:74:27: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rasa/core/processor.py:587:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rasa/core/processor.py:587:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rasa/shared/core/training_data/structures.py:619:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rasa/shared/core/training_data/structures.py:619:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rasa/shared/core/training_data/structures.py:621:20: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rasa/shared/core/training_data/structures.py:621:20: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rasa/utils/train_utils.py:534:9: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rasa/utils/train_utils.py:534:9: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
... 2 additional changes omitted for project

apache/airflow (+0 -0 violations, +30 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

- airflow/metrics/validators.py:217:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ airflow/metrics/validators.py:217:16: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- airflow/models/dag.py:3222:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ airflow/models/dag.py:3222:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- airflow/models/dagrun.py:1070:20: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ airflow/models/dagrun.py:1070:20: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- airflow/operators/python.py:369:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ airflow/operators/python.py:369:16: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- airflow/providers/cncf/kubernetes/operators/pod.py:389:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ airflow/providers/cncf/kubernetes/operators/pod.py:389:16: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
... 20 additional changes omitted for project

bokeh/bokeh (+0 -0 violations, +4 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

- src/bokeh/core/property/dataspec.py:334:35: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ src/bokeh/core/property/dataspec.py:334:35: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- src/bokeh/plotting/_graph.py:74:8: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ src/bokeh/plotting/_graph.py:74:8: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

ibis-project/ibis (+0 -0 violations, +8 -0 fixes)

ruff check --no-cache --exit-zero --preview

- ibis/expr/analysis.py:346:20: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ ibis/expr/analysis.py:346:20: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- ibis/expr/datatypes/cast.py:122:9: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ ibis/expr/datatypes/cast.py:122:9: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- ibis/expr/datatypes/cast.py:124:12: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ ibis/expr/datatypes/cast.py:124:12: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- ibis/expr/datatypes/cast.py:127:12: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ ibis/expr/datatypes/cast.py:127:12: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

milvus-io/pymilvus (+0 -0 violations, +2 -0 fixes)

ruff check --no-cache --exit-zero --preview

- pymilvus/client/check.py:238:26: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ pymilvus/client/check.py:238:26: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

pandas-dev/pandas (+0 -27 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

- pandas/_testing/asserters.py:754:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/_testing/asserters.py:756:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/_testing/asserters.py:911:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/_testing/asserters.py:913:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/computation/eval.py:340:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/computation/expr.py:510:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/computation/pytables.py:410:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/computation/pytables.py:412:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/dtypes/dtypes.py:1061:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- pandas/core/dtypes/dtypes.py:1714:21: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
... 17 additional changes omitted for project

python-poetry/poetry (+0 -3 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

- src/poetry/console/io/inputs/run_argv_input.py:48:38: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- src/poetry/utils/dependency_specification.py:161:16: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- src/poetry/utils/env/env_manager.py:235:33: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

rotki/rotki (+0 -0 violations, +34 -0 fixes)

ruff check --no-cache --exit-zero --preview

- rotkehlchen/accounting/export/csv.py:155:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rotkehlchen/accounting/export/csv.py:155:13: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rotkehlchen/accounting/export/csv.py:156:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rotkehlchen/accounting/export/csv.py:156:13: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rotkehlchen/api/rest.py:1077:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rotkehlchen/api/rest.py:1077:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rotkehlchen/api/rest.py:1078:17: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rotkehlchen/api/rest.py:1078:17: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rotkehlchen/api/v1/schemas.py:3223:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ rotkehlchen/api/v1/schemas.py:3223:13: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
- rotkehlchen/api/v1/schemas.py:3224:13: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
... 23 additional changes omitted for project

scikit-build/scikit-build-core (+0 -0 violations, +2 -0 fixes)

ruff check --no-cache --exit-zero --preview

- tests/test_settings_overrides.py:401:45: RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear
+ tests/test_settings_overrides.py:401:45: RUF021 [*] Parenthesize `a and b` expressions when chaining `and` and `or` together, to make the precedence clear

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF021 144 0 30 114 0

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Jan 9, 2024

pandas-dev/pandas (+0 -4 violations, +0 -0 fixes)

This is weird -- I can't repro this locally. If I run ruff with this branch locally on the pandas codebase, we still get RUF021 (correctly) triggering on all the lines here where it says the violation is going away

@AlexWaygood
Copy link
Member Author

Okay, I think I addressed everybody's comments!

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Nice!

@AlexWaygood AlexWaygood merged commit 6be7332 into astral-sh:main Jan 9, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the force-parens-fix branch January 9, 2024 17:55
@AlexWaygood
Copy link
Member Author

Thanks all for the reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants