-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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, gwithout 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`