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
Refactor typing.NamedTuple #84366
Comments
typing.NamedTuple is used in two ways.
In both cases it is not a real class. You cannot create an instance of NamedTuple or a subclass of NamedTuple. But it is implemented as a class, and help() shows methods and data descriptors for it, which are useless. The proposed PR implements NamedTuple like a function. Implementation of the __mro_entries__ method allows to use it as a base in the class statement. |
See also bpo-40187. |
This seems to have caused a test failure in astroid project. I am not sure if the fix needs to be done from their end probably relying on some implementation detail or from the patch itself. Feel free to ignore if it's a third-party only error. Reproducer script : /tmp/namedtuple_astroid.py from astroid import parse
mod = parse('''from typing import NamedTuple; NamedTuple("A")''')
print(str(next(mod.body[-1].value.infer())))
assert str(next(mod.body[-1].value.infer())) != 'Uninferable' Before this commit : python /tmp/namedtuple_astroid.py After this commit : python /tmp/namedtuple_astroid.py
Uninferable
Traceback (most recent call last):
File "/tmp/namedtuple_astroid.py", line 4, in <module>
assert str(next(mod.body[-1].value.infer())) != 'Uninferable'
AssertionError |
$ echo 'from typing import NamedTuple; NamedTuple("A")' | ./python -m ast
Module(
body=[
ImportFrom(
module='typing',
names=[
alias(name='NamedTuple')],
level=0),
Expr(
value=Call(
func=Name(id='NamedTuple', ctx=Load()),
args=[
Constant(value='A')],
keywords=[]))],
type_ignores=[]) So mod.body[-1].value is a call:
I do not know what Astroid does with this node, but seem the problem is in its infer() method. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: