Skip to content
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

NamedTuples cannot have nested classes #15775

Open
sobolevn opened this issue Jul 29, 2023 · 1 comment · May be fixed by #15776
Open

NamedTuples cannot have nested classes #15775

sobolevn opened this issue Jul 29, 2023 · 1 comment · May be fixed by #15776
Assignees
Labels
bug mypy got something wrong topic-named-tuple

Comments

@sobolevn
Copy link
Member

Python allows class definitions inside NamedTuple bodies:

>>> from typing import NamedTuple
>>> class A(NamedTuple):
...     class B: ...
... 
>>> A.B
<class '__main__.A.B'>
>>> A().B
<class '__main__.A.B'>

But, mypy does not allow this:

from typing import NamedTuple

class A(NamedTuple):
    class B: ...  # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"

reveal_type(A.B)  # E: "type[A]" has no attribute "B"
# N: Revealed type is "Any"

I think that this is a bug that needs to be solved, because we don't have any reason not to allow this (my existing patch shows that semanal and typechecker are happy with allowing these classes).

This is the root cause of #15752

@sobolevn sobolevn added bug mypy got something wrong topic-named-tuple labels Jul 29, 2023
@sobolevn sobolevn self-assigned this Jul 29, 2023
@sobolevn sobolevn linked a pull request Jul 29, 2023 that will close this issue
@tmke8
Copy link
Contributor

tmke8 commented Jul 30, 2023

Seems someone tried to fix this before:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-named-tuple
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants