-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[ruff
] Avoid emitting assignment-in-assert
when all references to the assigned variable are themselves inside assert
s (RUF018
)
#14661
Conversation
… the assigned variable are themselves inside `assert`s (`RUF018`)
|
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 |
The ecosystem hits all look good to me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Summary
Fixes #14658.
The stated rationale for RUF018 is that it's unsafe to assign a variable using an assignment expression inside an
assert
statement, 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 otherassert
statements: either allassert
statements will be stripped from the Python program, or none will be. This PR therefore moves the rule tobindings.rs
and 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 insideassert
statements or not.In order to determine whether a
Binding
orResolvedReference
took place inside anassert
statement, it's necessary to addSemanticModelFlags::ASSERT_STATEMENT
andBindingFlags::IN_ASSERT_STATEMENT
to Ruff's semantic model.Test Plan
New fixtures added.
cargo test -p ruff_linter --lib
.