-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Wrong line number for invalid iterable that uses __getattr__ #14892
Comments
Is that correct? At runtime, dunders are looked up on the class object rather than the instance for many operations (including the iterator protocol), so >>> class Foo:
... def __getattr__(self, name):
... if name == "__iter__":
... return lambda self: iter(range(10))
...
>>> Foo().__iter__
<range_iterator object at 0x00000194BCD3BB50>
>>> list(Foo().__iter__)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(Foo())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Foo' object is not iterable
|
Using the original example: >>> class C:
... def __getattr__(self, name):
... if name == "__iter__":
... return lambda self: iter(range(2))
...
>>> class D:
... def f(self):
... return C()
... def g(self):
... a, b = self.f()
...
>>> D().g()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in g
TypeError: cannot unpack non-iterable C object |
A type was used as error context, but types don't reliably have valid line numbers during type checking. Pass context explicitly instead. The error in the test case is actually a false positive, but I'm first fixing the line number of the error, since it seems plausible that the wrong line number could cause other problems. Work on #14892.
Yes, you are quite right. So the issue is only about the incorrect line number. |
This generates an error
false positivewhich is reported on the wrong lineso there are likely two bugs:C
should be treated as iterable, since it defines__getattr__
.The text was updated successfully, but these errors were encountered: