Skip to content

Commit 5b13195

Browse files
committed
Remove use of join during semantic analysis
MROs may not be populated yet, so the join may crash. See #3316 for additional context. Fixes #3315.
1 parent 09e7b12 commit 5b13195

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

mypy/semanal.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,9 +2048,12 @@ def build_namedtuple_typeinfo(self, name: str, items: List[str], types: List[Typ
20482048
# Actual signature should return OrderedDict[str, Union[types]]
20492049
ordereddictype = (self.named_type_or_none('builtins.dict', [strtype, AnyType()])
20502050
or self.object_type())
2051-
# 'builtins.tuple' has only one type parameter, the corresponding type argument
2052-
# in the fallback instance is a join of all item types.
2053-
fallback = self.named_type('__builtins__.tuple', [join.join_type_list(types)])
2051+
# 'builtins.tuple' has only one type parameter.
2052+
#
2053+
# TODO: The corresponding type argument in the fallback instance should be a join of
2054+
# all item types, but we can't do joins during this pass of semantic analysis
2055+
# and we are using Any as a workaround.
2056+
fallback = self.named_type('__builtins__.tuple', [AnyType()])
20542057
# Note: actual signature should accept an invariant version of Iterable[UnionType[types]].
20552058
# but it can't be expressed. 'new' and 'len' should be callable types.
20562059
iterable_type = self.named_type_or_none('typing.Iterable', [AnyType()])

test-data/unit/check-namedtuple.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,13 @@ def f(x: a.X) -> None:
449449
[out]
450450
tmp/b.py:6: error: Revealed type is 'a.X'
451451
tmp/b.py:8: error: Revealed type is 'Tuple[Any, fallback=a.X]'
452+
453+
[case testForwardReferenceInNamedTuple]
454+
from typing import NamedTuple
455+
456+
class A(NamedTuple):
457+
b: 'B'
458+
x: int
459+
460+
class B:
461+
pass

0 commit comments

Comments
 (0)