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

Allow nested classes in NamedTuple bodies #15776

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mypy/semanal_namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def check_namedtuple_classdef(
# And docstrings.
if isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, StrExpr):
continue
# And nested classes, they need to be analyzed further:
if isinstance(stmt, ClassDef):
continue
statements.pop()
defn.removed_statements.append(stmt)
self.fail(NAMEDTUP_CLASS_ERROR, stmt)
Expand Down
2 changes: 2 additions & 0 deletions mypy/server/astmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ def process_synthetic_type_info(self, info: TypeInfo) -> None:
# tables separately, unlike normal classes.
self.process_type_info(info)
for name, node in info.names.items():
if isinstance(node.node, TypeInfo):
continue # There might be nested classes in some cases, skip them
if node.node:
node.node.accept(self)

Expand Down
40 changes: 40 additions & 0 deletions test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,46 @@ class X(NamedTuple):
y = 2 # E: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
[builtins fixtures/tuple.pyi]

[case testNewNamedTupleWithNestedClass]
from typing import NamedTuple

class A(NamedTuple):
x: int
class B:
x: str

a: A
reveal_type(A.B) # N: Revealed type is "def () -> __main__.A.B"
b: A.B
reveal_type(b.x) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]

[case testNewNamedTupleWithNestedNamedTuple]
from typing import NamedTuple

class A(NamedTuple):
x: int
class B(NamedTuple):
x: str
y: int = 1
def method(self) -> int: ...

# Correct:
A(1)
A.B('a')
A.B('a', 2)

b: A.B
reveal_type(b.x) # N: Revealed type is "builtins.str"
reveal_type(b.y) # N: Revealed type is "builtins.int"
reveal_type(b.method()) # N: Revealed type is "builtins.int"

# Incorrect:
A.B() # E: Missing positional argument "x" in call to "B"
A.B(1, 'a') # E: Argument 1 to "B" has incompatible type "int"; expected "str" \
# E: Argument 2 to "B" has incompatible type "str"; expected "int"
[builtins fixtures/tuple.pyi]

[case testTypeUsingTypeCNamedTuple]
from typing import NamedTuple, Type

Expand Down
15 changes: 9 additions & 6 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -10192,23 +10192,25 @@ import m
from typing import NamedTuple

class NT(NamedTuple):
class C: ...
class C: ... # classes are fine
x: int
y: int
assert True # assert statements are not allowed

[file m.py.2]
from typing import NamedTuple

class NT(NamedTuple):
class C: ...
class C: ... # classes are fine
x: int
y: int
assert True # assert statements are not allowed
# change
[builtins fixtures/tuple.pyi]
[out]
m.py:4: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
m.py:7: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
==
m.py:4: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
m.py:7: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"

[case testNamedTupleNestedClassRecheck]
import n
Expand All @@ -10223,16 +10225,17 @@ class NT(NamedTuple):
class C: ...
x: int
y: A
assert True

[file f.py]
A = int
[file f.py.2]
A = str
[builtins fixtures/tuple.pyi]
[out]
m.py:5: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
m.py:8: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
==
m.py:5: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"
m.py:8: error: Invalid statement in NamedTuple definition; expected "field_name: field_type [= default]"

[case testTypedDictNestedClassRecheck]
import n
Expand Down
31 changes: 17 additions & 14 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,16 @@ class RegularClass:
x: int
y: str = 'a'
z: str = 'b'

class WithNestedNT(NamedTuple):
x: int
y: str = 'a'
class Regular:
x: int
y: str = 'a'
class NestedNamedTuple(NamedTuple):
x: int
y: str = 'a'
[out]
from typing import NamedTuple

Expand All @@ -737,22 +747,15 @@ class RegularClass:
y: str = ...
z: str


[case testNestedClassInNamedTuple_semanal-xfail]
from typing import NamedTuple

# TODO: make sure that nested classes in `NamedTuple` are supported:
class NamedTupleWithNestedClass(NamedTuple):
class Nested:
x: int
y: str = 'a'
[out]
from typing import NamedTuple

class NamedTupleWithNestedClass(NamedTuple):
class Nested:
class WithNestedNT(NamedTuple):
x: int
y: str = ...
class Regular:
x: int
y: str
class NestedNamedTuple(NamedTuple):
x: int
y: str = ...

[case testEmptyNamedtuple]
import collections, typing
Expand Down