Skip to content

Commit 9438211

Browse files
authored
New semantic analyzer: enable tests related to recursively defined types (#7080)
There is a feature regression: we no longer expand recursive definition once. I think that this is acceptable for now. We can either try to replicate the expansion of recursive definitions later on, or we can wait until we have full recursive support before we do anything about this. Only enabled tests that do something reasonable. Recursive type aliases and other things still don't work. Work towards #6445.
1 parent 33760b7 commit 9438211

File tree

5 files changed

+77
-91
lines changed

5 files changed

+77
-91
lines changed

test-data/unit/check-classes.test

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,32 +4356,30 @@ class A(Tuple[int, str]): pass
43564356

43574357
[case testCrashOnSelfRecursiveNamedTupleVar]
43584358
# FIXME: #6445
4359-
# flags: --no-new-semantic-analyzer
4359+
# flags: --new-semantic-analyzer
43604360
from typing import NamedTuple
43614361

4362-
N = NamedTuple('N', [('x', N)]) # E: Recursive types not fully supported yet, nested types replaced with "Any"
4362+
N = NamedTuple('N', [('x', N)]) # E: Cannot resolve name "N" (possible cyclic definition)
43634363
n: N
4364-
[out]
4364+
reveal_type(n) # N: Revealed type is 'Tuple[Any, fallback=__main__.N]'
43654365

43664366
[case testCrashOnSelfRecursiveTypedDictVar]
43674367
# FIXME: #6445
4368-
# flags: --no-new-semantic-analyzer
43694368
from mypy_extensions import TypedDict
43704369

43714370
A = TypedDict('A', {'a': 'A'}) # type: ignore
43724371
a: A
43734372
[builtins fixtures/isinstancelist.pyi]
4374-
[out]
43754373

43764374
[case testCrashInJoinOfSelfRecursiveNamedTuples]
43774375
# FIXME: #6445
4378-
# flags: --no-new-semantic-analyzer
4376+
# flags: --new-semantic-analyzer
43794377
from typing import NamedTuple
43804378

4381-
class N(NamedTuple): # type: ignore
4382-
x: N
4383-
class M(NamedTuple): # type: ignore
4384-
x: M
4379+
class N(NamedTuple):
4380+
x: N # type: ignore
4381+
class M(NamedTuple):
4382+
x: M # type: ignore
43854383

43864384
n: N
43874385
m: M
@@ -4390,18 +4388,18 @@ lst = [n, m]
43904388

43914389
[case testCorrectJoinOfSelfRecursiveTypedDicts]
43924390
# FIXME: #6445
4393-
# flags: --no-new-semantic-analyzer
4391+
# flags: --new-semantic-analyzer
43944392
from mypy_extensions import TypedDict
43954393

4396-
class N(TypedDict): # E: Recursive types not fully supported yet, nested types replaced with "Any"
4397-
x: N
4398-
class M(TypedDict): # E: Recursive types not fully supported yet, nested types replaced with "Any"
4399-
x: M
4394+
class N(TypedDict):
4395+
x: N # E: Cannot resolve name "N" (possible cyclic definition)
4396+
class M(TypedDict):
4397+
x: M # E: Cannot resolve name "M" (possible cyclic definition)
44004398

44014399
n: N
44024400
m: M
44034401
lst = [n, m]
4404-
reveal_type(lst[0]['x']) # N: Revealed type is 'TypedDict('__main__.N', {'x': Any})'
4402+
reveal_type(lst[0]['x']) # N: Revealed type is 'Any'
44054403
[builtins fixtures/isinstancelist.pyi]
44064404

44074405
[case testCrashInForwardRefToNamedTupleWithIsinstance]

test-data/unit/check-incremental.test

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,39 +2570,39 @@ M = NamedTuple('M', [('x', int)])
25702570
[out]
25712571

25722572
[case testSelfRefNTIncremental1]
2573-
# flags: --no-new-semantic-analyzer
2573+
# flags: --new-semantic-analyzer
25742574
from typing import Tuple, NamedTuple
25752575

2576-
Node = NamedTuple('Node', [ # type: ignore
2576+
Node = NamedTuple('Node', [
25772577
('name', str),
2578-
('children', Tuple['Node', ...]),
2578+
('children', Tuple['Node', ...]), # type: ignore
25792579
])
25802580
n: Node
25812581
[builtins fixtures/tuple.pyi]
25822582

25832583
[case testSelfRefNTIncremental2]
2584-
# flags: --no-new-semantic-analyzer
2584+
# flags: --new-semantic-analyzer
25852585
from typing import Tuple, NamedTuple
25862586

2587-
A = NamedTuple('A', [ # type: ignore
2587+
A = NamedTuple('A', [
25882588
('x', str),
2589-
('y', Tuple['B', ...]),
2589+
('y', Tuple['B', ...]), # type: ignore
25902590
])
2591-
class B(NamedTuple): # type: ignore
2591+
class B(NamedTuple):
25922592
x: A
25932593
y: int
25942594

25952595
n: A
25962596
[builtins fixtures/tuple.pyi]
25972597

25982598
[case testSelfRefNTIncremental3]
2599-
# flags: --no-new-semantic-analyzer
2599+
# flags: --new-semantic-analyzer
26002600
from typing import NamedTuple, Tuple
26012601

2602-
class B(NamedTuple): # type: ignore
2603-
x: Tuple[A, int]
2602+
class B(NamedTuple):
2603+
x: Tuple[A, int] # type: ignore
26042604
y: int
2605-
A = NamedTuple('A', [ # type: ignore
2605+
A = NamedTuple('A', [
26062606
('x', str),
26072607
('y', 'B'),
26082608
])
@@ -2612,28 +2612,28 @@ lst = [m, n]
26122612
[builtins fixtures/tuple.pyi]
26132613

26142614
[case testSelfRefNTIncremental4]
2615-
# flags: --no-new-semantic-analyzer
2615+
# flags: --new-semantic-analyzer
26162616
from typing import NamedTuple
26172617

2618-
class B(NamedTuple): # type: ignore
2619-
x: A
2618+
class B(NamedTuple):
2619+
x: A # type: ignore
26202620
y: int
2621-
class A(NamedTuple): # type: ignore
2621+
class A(NamedTuple):
26222622
x: str
26232623
y: B
26242624

26252625
n: A
26262626
[builtins fixtures/tuple.pyi]
26272627

26282628
[case testSelfRefNTIncremental5]
2629-
# flags: --no-new-semantic-analyzer
2629+
# flags: --new-semantic-analyzer
26302630
from typing import NamedTuple
26312631

2632-
B = NamedTuple('B', [ # type: ignore
2633-
('x', A),
2632+
B = NamedTuple('B', [
2633+
('x', A), # type: ignore
26342634
('y', int),
26352635
])
2636-
A = NamedTuple('A', [ # type: ignore
2636+
A = NamedTuple('A', [
26372637
('x', str),
26382638
('y', 'B'),
26392639
])
@@ -4620,7 +4620,7 @@ tmp/main.py:2: error: Argument 1 to "f" has incompatible type "int"; expected "s
46204620

46214621
[case testOverrideByIdemAlias]
46224622
# https://github.com/python/mypy/issues/6404
4623-
# flags: --no-new-semantic-analyzer
4623+
# flags: --new-semantic-analyzer
46244624
import a
46254625
[file a.py]
46264626
import lib
@@ -4629,7 +4629,7 @@ x = 1
46294629
import lib
46304630
x = 2
46314631
[file lib.py]
4632-
C = C
4632+
C = C # type: ignore
46334633
class C: # type: ignore
46344634
pass
46354635
[out]

test-data/unit/check-namedtuple.test

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -574,19 +574,16 @@ tmp/b.py:4: note: Revealed type is 'Tuple[Any, fallback=a.N]'
574574
tmp/b.py:7: note: Revealed type is 'Tuple[Any, fallback=a.N]'
575575

576576
[case testSimpleSelfReferentialNamedTuple]
577-
# flags: --no-new-semantic-analyzer
577+
# flags: --new-semantic-analyzer
578578
from typing import NamedTuple
579579
class MyNamedTuple(NamedTuple):
580-
parent: 'MyNamedTuple'
580+
parent: 'MyNamedTuple' # E: Cannot resolve name "MyNamedTuple" (possible cyclic definition)
581581

582582
def bar(nt: MyNamedTuple) -> MyNamedTuple:
583583
return nt
584584

585585
x: MyNamedTuple
586-
reveal_type(x.parent)
587-
[out]
588-
main:3: error: Recursive types not fully supported yet, nested types replaced with "Any"
589-
main:10: note: Revealed type is 'Tuple[Any, fallback=__main__.MyNamedTuple]'
586+
reveal_type(x.parent) # N: Revealed type is 'Any'
590587

591588
-- Some crazy self-referential named tuples and types dicts
592589
-- to be sure that everything works
@@ -613,94 +610,85 @@ class B:
613610
[out]
614611

615612
[case testSelfRefNT1]
616-
# flags: --no-new-semantic-analyzer
613+
# flags: --new-semantic-analyzer
617614
from typing import Tuple, NamedTuple
618615

619-
Node = NamedTuple('Node', [ # E: Recursive types not fully supported yet, nested types replaced with "Any"
616+
Node = NamedTuple('Node', [
620617
('name', str),
621-
('children', Tuple['Node', ...]),
618+
('children', Tuple['Node', ...]), # E: Cannot resolve name "Node" (possible cyclic definition)
622619
])
623620
n: Node
624-
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, builtins.tuple[Tuple[builtins.str, builtins.tuple[Any], fallback=__main__.Node]], fallback=__main__.Node]'
621+
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, builtins.tuple[Any], fallback=__main__.Node]'
625622
[builtins fixtures/tuple.pyi]
626623

627-
628624
[case testSelfRefNT2]
629-
# flags: --no-new-semantic-analyzer
625+
# flags: --new-semantic-analyzer
630626
from typing import Tuple, NamedTuple
631627

632-
A = NamedTuple('A', [ # E
628+
A = NamedTuple('A', [
633629
('x', str),
634-
('y', Tuple['B', ...]),
630+
('y', Tuple['B', ...]), # E: Cannot resolve name "B" (possible cyclic definition)
635631
])
636-
class B(NamedTuple): # E
632+
class B(NamedTuple):
637633
x: A
638634
y: int
639635

640636
n: A
641-
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, builtins.tuple[Tuple[Tuple[builtins.str, builtins.tuple[Any], fallback=__main__.A], builtins.int, fallback=__main__.B]], fallback=__main__.A]'
637+
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, builtins.tuple[Any], fallback=__main__.A]'
642638
[builtins fixtures/tuple.pyi]
643-
[out]
644-
main:4: error: Recursive types not fully supported yet, nested types replaced with "Any"
645-
main:8: error: Recursive types not fully supported yet, nested types replaced with "Any"
646639

647640
[case testSelfRefNT3]
648-
# flags: --no-new-semantic-analyzer
641+
# flags: --new-semantic-analyzer
649642
from typing import NamedTuple, Tuple
650643

651-
class B(NamedTuple): # E
652-
x: Tuple[A, int]
644+
class B(NamedTuple):
645+
x: Tuple[A, int] # E: Cannot resolve name "A" (possible cyclic definition)
653646
y: int
654647

655-
A = NamedTuple('A', [ # E: Recursive types not fully supported yet, nested types replaced with "Any"
648+
A = NamedTuple('A', [
656649
('x', str),
657650
('y', 'B'),
658651
])
659652
n: B
660653
m: A
661-
reveal_type(n.x) # N: Revealed type is 'Tuple[Tuple[builtins.str, Tuple[Tuple[Any, builtins.int], builtins.int, fallback=__main__.B], fallback=__main__.A], builtins.int]'
654+
reveal_type(n.x) # N: Revealed type is 'Tuple[Any, builtins.int]'
662655
reveal_type(m[0]) # N: Revealed type is 'builtins.str'
663656
lst = [m, n]
664657
reveal_type(lst[0]) # N: Revealed type is 'Tuple[builtins.object, builtins.object]'
665658
[builtins fixtures/tuple.pyi]
666-
[out]
667-
main:4: error: Recursive types not fully supported yet, nested types replaced with "Any"
668659

669660
[case testSelfRefNT4]
670-
# flags: --no-new-semantic-analyzer
661+
# flags: --new-semantic-analyzer
671662
from typing import NamedTuple
672663

673-
class B(NamedTuple): # E
674-
x: A
664+
class B(NamedTuple):
665+
x: A # E: Cannot resolve name "A" (possible cyclic definition)
675666
y: int
676667

677-
class A(NamedTuple): # E
668+
class A(NamedTuple):
678669
x: str
679670
y: B
680671

681672
n: A
682-
reveal_type(n.y[0]) # N: Revealed type is 'Tuple[builtins.str, Tuple[Any, builtins.int, fallback=__main__.B], fallback=__main__.A]'
673+
reveal_type(n.y[0]) # N: Revealed type is 'Any'
683674
[builtins fixtures/tuple.pyi]
684-
[out]
685-
main:4: error: Recursive types not fully supported yet, nested types replaced with "Any"
686-
main:8: error: Recursive types not fully supported yet, nested types replaced with "Any"
687675

688676
[case testSelfRefNT5]
689-
# flags: --no-new-semantic-analyzer
677+
# flags: --new-semantic-analyzer
690678
from typing import NamedTuple
691679

692-
B = NamedTuple('B', [ # E: Recursive types not fully supported yet, nested types replaced with "Any"
693-
('x', A),
680+
B = NamedTuple('B', [
681+
('x', A), # E: Cannot resolve name "A" (possible cyclic definition)
694682
('y', int),
695683
])
696-
A = NamedTuple('A', [ # E: Recursive types not fully supported yet, nested types replaced with "Any"
684+
A = NamedTuple('A', [
697685
('x', str),
698686
('y', 'B'),
699687
])
700688
n: A
701689
def f(m: B) -> None: pass
702-
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, Tuple[Tuple[builtins.str, Tuple[Any, builtins.int, fallback=__main__.B], fallback=__main__.A], builtins.int, fallback=__main__.B], fallback=__main__.A]'
703-
reveal_type(f) # N: Revealed type is 'def (m: Tuple[Tuple[builtins.str, Tuple[Any, builtins.int, fallback=__main__.B], fallback=__main__.A], builtins.int, fallback=__main__.B])'
690+
reveal_type(n) # N: Revealed type is 'Tuple[builtins.str, Tuple[Any, builtins.int, fallback=__main__.B], fallback=__main__.A]'
691+
reveal_type(f) # N: Revealed type is 'def (m: Tuple[Any, builtins.int, fallback=__main__.B])'
704692
[builtins fixtures/tuple.pyi]
705693

706694
[case testRecursiveNamedTupleInBases]
@@ -745,17 +733,17 @@ tp = NamedTuple('tp', [('x', int)])
745733
[out]
746734

747735
[case testSubclassOfRecursiveNamedTuple]
748-
# flags: --no-new-semantic-analyzer
736+
# flags: --new-semantic-analyzer
749737
from typing import List, NamedTuple
750738

751-
class Command(NamedTuple): # type: ignore
752-
subcommands: List['Command']
739+
class Command(NamedTuple):
740+
subcommands: List['Command'] # E: Cannot resolve name "Command" (possible cyclic definition)
753741

754742
class HelpCommand(Command):
755743
pass
756744

757745
hc = HelpCommand(subcommands=[])
758-
reveal_type(hc) # N: Revealed type is 'Tuple[builtins.list[Tuple[builtins.list[Any], fallback=__main__.Command]], fallback=__main__.HelpCommand]'
746+
reveal_type(hc) # N: Revealed type is 'Tuple[builtins.list[Any], fallback=__main__.HelpCommand]'
759747
[builtins fixtures/list.pyi]
760748
[out]
761749

test-data/unit/check-typeddict.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,34 +1340,34 @@ reveal_type(x['a']['b']) # N: Revealed type is 'builtins.int'
13401340
[builtins fixtures/dict.pyi]
13411341

13421342
[case testSelfRecursiveTypedDictInheriting]
1343-
# flags: --no-new-semantic-analyzer
1343+
# flags: --new-semantic-analyzer
13441344
from mypy_extensions import TypedDict
13451345

13461346
class MovieBase(TypedDict):
13471347
name: str
13481348
year: int
13491349

1350-
class Movie(MovieBase): # type: ignore # warning about recursive not fully supported
1351-
director: 'Movie'
1350+
class Movie(MovieBase):
1351+
director: 'Movie' # E: Cannot resolve name "Movie" (possible cyclic definition)
13521352

13531353
m: Movie
1354-
reveal_type(m['director']['name']) # N: Revealed type is 'builtins.str'
1354+
reveal_type(m['director']['name']) # N: Revealed type is 'Any'
13551355
[builtins fixtures/dict.pyi]
13561356
[out]
13571357

13581358
[case testSubclassOfRecursiveTypedDict]
1359-
# flags: --no-new-semantic-analyzer
1359+
# flags: --new-semantic-analyzer
13601360
from typing import List
13611361
from mypy_extensions import TypedDict
13621362

1363-
class Command(TypedDict): # type: ignore
1364-
subcommands: List['Command']
1363+
class Command(TypedDict):
1364+
subcommands: List['Command'] # E: Cannot resolve name "Command" (possible cyclic definition)
13651365

13661366
class HelpCommand(Command):
13671367
pass
13681368

13691369
hc = HelpCommand(subcommands=[])
1370-
reveal_type(hc) # N: Revealed type is 'TypedDict('__main__.HelpCommand', {'subcommands': builtins.list[TypedDict('__main__.Command', {'subcommands': builtins.list[Any]})]})'
1370+
reveal_type(hc) # N: Revealed type is 'TypedDict('__main__.HelpCommand', {'subcommands': builtins.list[Any]})'
13711371
[builtins fixtures/list.pyi]
13721372
[out]
13731373

test-data/unit/check-unions.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,12 @@ takes_int(x) # E: Argument 1 to "takes_int" has incompatible type <union: 6 ite
958958

959959
[case testRecursiveForwardReferenceInUnion]
960960
# https://github.com/python/mypy/issues/6445
961-
# flags: --no-new-semantic-analyzer
961+
# flags: --new-semantic-analyzer
962962
from typing import List, Union
963963
MYTYPE = List[Union[str, "MYTYPE"]]
964964
[builtins fixtures/list.pyi]
965965
[out]
966-
main:4: error: Recursive types not fully supported yet, nested types replaced with "Any"
966+
main:4: error: Cannot resolve name "MYTYPE" (possible cyclic definition)
967967

968968
[case testNonStrictOptional]
969969
from typing import Optional, List

0 commit comments

Comments
 (0)