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

[ruff] Avoid emitting assignment-in-assert when all references to the assigned variable are themselves inside asserts (RUF018) #14661

Merged
merged 2 commits into from
Nov 29, 2024

Conversation

AlexWaygood
Copy link
Member

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 other assert statements: either all assert statements will be stripped from the Python program, or none will be. This PR therefore moves the rule to bindings.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 inside assert statements or not.

In order to determine whether a Binding or ResolvedReference took place inside an assert statement, it's necessary to add SemanticModelFlags::ASSERT_STATEMENT and BindingFlags::IN_ASSERT_STATEMENT to Ruff's semantic model.

Test Plan

New fixtures added. cargo test -p ruff_linter --lib.

… the assigned variable are themselves inside `assert`s (`RUF018`)
@AlexWaygood AlexWaygood added the bug Something isn't working label Nov 28, 2024
Copy link
Contributor

github-actions bot commented Nov 28, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ 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 --no-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 --no-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)

+ 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

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

@AlexWaygood
Copy link
Member Author

The ecosystem hits all look good to me

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

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

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUF018 false positive. Assignment for variables only used in other asserts
2 participants