RUF018
false positive. Assignment for variables only used in other asserts
#14658
Labels
bug
Something isn't working
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.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, the
t:=
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:
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`The text was updated successfully, but these errors were encountered: