-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
redefined-outer-name emitted for variables in the same namespace #5608
Comments
Hi there @superbobry, I think your contrasting examples articulate quite nicely what |
Thanks for the clarification @jacobtylerwalls. I think I understand why
nor for
even though technically the loop variable redefines a preceding name in the first snippet and is itself redefined in the loop body in the second one. Is it worth having that special-case at all in your opinion? |
Just my two cents: Your first example seems like a false positive. The second example should probably be another (new) message like |
You mean false negative, right? As in, we should emit |
@jacobtylerwalls Ah yes! |
Note that to really get the first example right pylint would likely have to do proper reachability analysis (and I really think it shouldn't tbh). E.g. imagine
|
Ah, but in that example, |
I guess that would depend on what "outer" means. The help string for in which case |
Wouldn't it be better if def func(coords):
x_coords, y_coords = [], []
for x, y in coords:
x_coords.append(x)
y_coords.append(y)
# Check a condition and set future return values
if x > y:
x_coord, y_coord = x, y
# Some additional code that makes you forget about x_coord
...
# For loop that shadows one of the return values
for x_coord in x_coords:
print(x_coord)
return x_coord, y_coord This is still very made-up and doesn't necessarily reflect a real world scenario but I think this is a potential bug that |
I have no specific examples to back this up, but my gut feeling is that the false positive rate of such a check would be too high for it to be useful. |
Could you give an example of a potential false positive? I mean, if |
After reading the discussion on #5146, I think that the first example in #5608 (comment) (and which @DanielNoord elaborated on) is valid usage, and should not emit a message (so pylint's current behavior is right.) This is the correct way to guard against the iterable being empty if the loop variable is accessed afterward: x_coord = 0
for x_coord in maybe_empty:
x_coord = random.random()
print(x_coord)
I agree that should warn, but I don't see much of a difference between for y in range(...):
for y in range(...): # [already emits redefined-outer-name]
...
for y in range(...):
y = None # [I think this should also emit redefined-outer-name] Both have the effect of overwriting/defeating the first
I agree this is worded poorly. Maybe "outer scope, or enclosing loop or except handler". |
This is tough, because although I'm somewhat partial to creating a new checker instead of extending for line in f:
line = line.strip() ... is very similar to the nested loop scenario that already works, and if we make them both for y in range(...):
for y in range(...): # pylint: disable=redefined-outer-name
... |
… loop variables in body
… loop variables in body
… loop variables in body
Is there precedent for allowing, say, a disable of |
There's |
… loop variables in body
We should probably do the same for redefined exceptions, which were added in #5370, since they are also technically in the same namespace. |
That should probably be discussed in a new issue in order not to lose this. Would you be able to open one @superbobry? |
Bug description
pylint emits
redefined-outer-name
for loop variables defined in nested for statements. For example,Technically, both definitions of
y
are in the same namespace, and while this code does look fishy, theredefined-outer-name
message seems irrelevant. The message is not emitted fornor
Command used
Pylint output
Expected behavior
redefined-outer-name
is not emitted.Pylint version
The text was updated successfully, but these errors were encountered: