You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
g += graph is an assignment in Python: it roughly performs g = g.__iadd__(graph) (falling back to __add__). Therefore the name introduced by with ... as g can be rebound to a different object, which is what PLW2901 is warning about.
It does not change which context manager's __exit__ is called; Python keeps that separately. The risk is that later statements in the block now operate on the replacement value, and code after the block sees that replacement because with does not create a scope.
For RDFLib Graph.__iadd__, if it returns the same graph object, this particular use is intentional and harmless. I would either make the intent explicit (for example, call the mutating method directly if there is a suitable one) or use a targeted # noqa: PLW2901. The rule is conservative because Ruff cannot prove the runtime return identity of an arbitrary __iadd__ implementation.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
In
forloop, it is problematic to run addition assignment to the iterator variable. But can the same be said of thewithclause?I would like to know of examples where such code causes bugs in actual behavior.
My code encountered in
PLW2901:Such examples are not currently included in the test.
https://github.com/charliermarsh/ruff/blob/abaf0a198d9d2cebefd5c029bb271be1a3498f20/crates/ruff/resources/test/fixtures/pylint/redefined_loop_name.py
All reactions