[ruff] Avoid emitting assignment-in-assert when all references to the assigned variable are themselves inside asserts (RUF018)#14661
Merged
AlexWaygood merged 2 commits intomainfrom Nov 29, 2024
Merged
Conversation
… the assigned variable are themselves inside `assert`s (`RUF018`)
Contributor
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF100 | 13 | 13 | 0 | 0 | 0 |
| RUF018 | 3 | 0 | 3 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+13 -3 violations, +0 -0 fixes in 3 projects; 52 projects unchanged)
apache/airflow (+0 -1 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL
- providers/tests/google/cloud/transfers/test_sql_to_gcs.py:487:68: RUF018 Avoid assignment expressions in `assert` statements
apache/superset (+0 -2 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL
- tests/unit_tests/db_engine_specs/utils.py:44:13: RUF018 Avoid assignment expressions in `assert` statements - tests/unit_tests/db_engine_specs/utils.py:60:13: RUF018 Avoid assignment expressions in `assert` statements
ibis-project/ibis (+13 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview
+ ibis/common/tests/test_patterns.py:266:62: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:267:58: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:269:62: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:801:78: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:803:70: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:806:70: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:918:72: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:922:64: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:924:68: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:935:41: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:951:86: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:968:71: RUF100 Unused `noqa` directive (unused: `RUF018`) + ibis/common/tests/test_patterns.py:973:71: RUF100 Unused `noqa` directive (unused: `RUF018`)
Changes by rule (2 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| RUF100 | 13 | 13 | 0 | 0 | 0 |
| RUF018 | 3 | 0 | 3 | 0 | 0 |
Member
Author
|
The ecosystem hits all look good to me |
dhruvmanila
reviewed
Nov 29, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14658.
The stated rationale for RUF018 is that it's unsafe to assign a variable using an assignment expression inside an
assertstatement, because if you run Python in optimised mode the assertion might be stripped out of the program at compilation time, meaning the variable is never actually defined. This concern is irrelevant, however, if the variable is only ever read from inside otherassertstatements: either allassertstatements will be stripped from the Python program, or none will be. This PR therefore moves the rule tobindings.rsand modifies the rule so that it iterates through all references to the binding introduced by the assignment expression to see whether those references take place insideassertstatements or not.In order to determine whether a
BindingorResolvedReferencetook place inside anassertstatement, it's necessary to addSemanticModelFlags::ASSERT_STATEMENTandBindingFlags::IN_ASSERT_STATEMENTto Ruff's semantic model.Test Plan
New fixtures added.
cargo test -p ruff_linter --lib.