Skip to content

Commit d174d75

Browse files
authored
New semantic analyzer: fix bogus messages and update tests (#7124)
Various recursive type definitions no longer cause crashes. Fix bogus messages about cyclic definitions when definition is acyclic when there are also recursive type definitions in the same target. Closes #6445.
1 parent ad6b4f6 commit d174d75

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

mypy/newsemanal/semanal.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,13 +2398,14 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
23982398
# Otherwise just replace existing placeholder with type alias.
23992399
existing.node = alias_node
24002400
updated = True
2401-
if self.final_iteration:
2402-
self.cannot_resolve_name(lvalue.name, 'name', s)
2403-
return True
2404-
elif updated:
2405-
self.progress = True
2406-
# We need to defer so that this change can get propagated to base classes.
2407-
self.defer()
2401+
if updated:
2402+
if self.final_iteration:
2403+
self.cannot_resolve_name(lvalue.name, 'name', s)
2404+
return True
2405+
else:
2406+
self.progress = True
2407+
# We need to defer so that this change can get propagated to base classes.
2408+
self.defer()
24082409
else:
24092410
self.add_symbol(lvalue.name, alias_node, s)
24102411
if isinstance(rvalue, RefExpr) and isinstance(rvalue.node, TypeAlias):

test-data/unit/check-classes.test

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,7 +4355,6 @@ class A(Tuple[int, str]): pass
43554355
-- -----------------------
43564356

43574357
[case testCrashOnSelfRecursiveNamedTupleVar]
4358-
# FIXME: #6445
43594358
# flags: --new-semantic-analyzer
43604359
from typing import NamedTuple
43614360

@@ -4364,15 +4363,13 @@ n: N
43644363
reveal_type(n) # N: Revealed type is 'Tuple[Any, fallback=__main__.N]'
43654364

43664365
[case testCrashOnSelfRecursiveTypedDictVar]
4367-
# FIXME: #6445
43684366
from mypy_extensions import TypedDict
43694367

43704368
A = TypedDict('A', {'a': 'A'}) # type: ignore
43714369
a: A
43724370
[builtins fixtures/isinstancelist.pyi]
43734371

43744372
[case testCrashInJoinOfSelfRecursiveNamedTuples]
4375-
# FIXME: #6445
43764373
# flags: --new-semantic-analyzer
43774374
from typing import NamedTuple
43784375

@@ -4387,7 +4384,6 @@ lst = [n, m]
43874384
[builtins fixtures/isinstancelist.pyi]
43884385

43894386
[case testCorrectJoinOfSelfRecursiveTypedDicts]
4390-
# FIXME: #6445
43914387
# flags: --new-semantic-analyzer
43924388
from mypy_extensions import TypedDict
43934389

test-data/unit/check-flags.test

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,32 +1053,29 @@ always_false = BLAH, BLAH1
10531053
[builtins fixtures/bool.pyi]
10541054

10551055
[case testCheckDisallowAnyGenericsNamedTuple]
1056-
# Crashes on new semantic analyzer: https://github.com/python/mypy/issues/6445
1057-
# flags: --disallow-any-generics --no-new-semantic-analyzer
1056+
# flags: --disallow-any-generics --new-semantic-analyzer
10581057
from typing import NamedTuple
10591058

10601059
N = NamedTuple('N', [('x', N)]) # type: ignore
10611060
n: N
10621061
[out]
10631062

10641063
[case testCheckDisallowAnyGenericsTypedDict]
1065-
# Crashes on new semantic analyzer: https://github.com/python/mypy/issues/6445
1066-
# flags: --disallow-any-generics --no-new-semantic-analyzer
1064+
# flags: --disallow-any-generics --new-semantic-analyzer
10671065
from typing import Dict, Any, Optional
10681066
from mypy_extensions import TypedDict
10691067

10701068
VarsDict = Dict[str, Any]
10711069
HostsDict = Dict[str, Optional[VarsDict]]
10721070

1073-
GroupDataDict = TypedDict( # type: ignore
1074-
"GroupDataDict", {"children": "GroupsDict",
1071+
GroupDataDict = TypedDict(
1072+
"GroupDataDict", {"children": "GroupsDict", # type: ignore
10751073
"vars": VarsDict,
10761074
"hosts": HostsDict}, total=False
10771075
)
10781076

10791077
GroupsDict = Dict[str, GroupDataDict] # type: ignore
10801078
[builtins fixtures/dict.pyi]
1081-
[out]
10821079

10831080
[case testCheckDefaultAllowAnyGeneric]
10841081
from typing import TypeVar, Callable

test-data/unit/check-unions.test

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -957,13 +957,10 @@ def takes_int(arg: int) -> None: pass
957957
takes_int(x) # E: Argument 1 to "takes_int" has incompatible type <union: 6 items>; expected "int"
958958

959959
[case testRecursiveForwardReferenceInUnion]
960-
# https://github.com/python/mypy/issues/6445
961960
# flags: --new-semantic-analyzer
962961
from typing import List, Union
963-
MYTYPE = List[Union[str, "MYTYPE"]]
962+
MYTYPE = List[Union[str, "MYTYPE"]] # E: Cannot resolve name "MYTYPE" (possible cyclic definition)
964963
[builtins fixtures/list.pyi]
965-
[out]
966-
main:4: error: Cannot resolve name "MYTYPE" (possible cyclic definition)
967964

968965
[case testNonStrictOptional]
969966
from typing import Optional, List

0 commit comments

Comments
 (0)