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

RUF018 false positive. Assignment for variables only used in other asserts #14658

Closed
Skylion007 opened this issue Nov 28, 2024 · 0 comments · Fixed by #14661
Closed

RUF018 false positive. Assignment for variables only used in other asserts #14658

Skylion007 opened this issue Nov 28, 2024 · 0 comments · Fixed by #14661
Assignees
Labels
bug Something isn't working

Comments

@Skylion007
Copy link
Contributor

Skylion007 commented Nov 28, 2024

RUF018 flags any asserts which assigns values in them, which is sensible. However, there are two cases where value assignment is actually fine, and even makes the code more readable. This rule is to prevent code breakages when asserts are disabled. I ran it on the sympy code and found two use cases that make sense.

    assert (t:=cancel((F, G))) == (1, P, Q)
    assert isinstance(t, tuple)

Here this is using a variable which is ONLY referenced in other assert expressions. There is no clean way to write this compound assert without knowing what assert failed, without storing the variable t in a temporary variable. Furthermore, thet:= may be expensive construct, we may only want to assign this variable when asserts are enabled.

The other less useful, but interesting case is using the assert expression in the error message of the assert. See here:

     assert (g:=solve(groebner(eqs, s), dict=True)) == sol, g

without this, the value has to be repeated to know what g actually is in this context, or it needs to be assigned to a temporary variable which may be constructed when asserts are also disabled. g should be cast to a string, but that is a fix for another rule`

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
2 participants