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

Rule suggestion: Prevent walrus assignments in assert statements #7205

Closed
awaelchli opened this issue Sep 6, 2023 · 1 comment · Fixed by #7856
Closed

Rule suggestion: Prevent walrus assignments in assert statements #7205

awaelchli opened this issue Sep 6, 2023 · 1 comment · Fixed by #7856
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule

Comments

@awaelchli
Copy link

If you want to make your software package run with the python -O optimization, you might want to ensure that the code does not contain a walrus assignment in assert statements:

y = 0
assert (x := y) is not None
x = x + 1

This code example runs fine normally, but if run with python -O the assertion gets removed and the program raises a UnboundLocalError because x is no longer bound.

As a developer, I might want to configure my linter in such a way that walrus assignments are not allowed in assertion statements. This way, I can enforce that the code will be compatible running with python -O.

@awaelchli awaelchli changed the title Rule suggestion: Prevent usage of walrus assignments in assert statements Rule suggestion: Prevent walrus assignments in assert statements Sep 6, 2023
@zanieb zanieb added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Sep 6, 2023
@charliermarsh charliermarsh added accepted Ready for implementation and removed needs-decision Awaiting a decision from a maintainer labels Sep 22, 2023
@charliermarsh
Copy link
Member

Seems reasonable to me. We can add it as a RUF rule.

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

Successfully merging a pull request may close this issue.

3 participants