Skip to content

Commit 610c125

Browse files
onlinedilevkivskyi
authored andcommitted
Fix crash when namedtuple, classmethod and generic types used (#6669)
Fixes #5996.
1 parent ec5353f commit 610c125

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

mypy/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,10 @@ def is_type_obj(self) -> bool:
923923
def type_object(self) -> mypy.nodes.TypeInfo:
924924
assert self.is_type_obj()
925925
ret = self.ret_type
926-
if isinstance(ret, TupleType):
927-
ret = ret.partial_fallback
928926
if isinstance(ret, TypeVarType):
929927
ret = ret.upper_bound
928+
if isinstance(ret, TupleType):
929+
ret = ret.partial_fallback
930930
assert isinstance(ret, Instance)
931931
return ret.type
932932

test-data/unit/check-namedtuple.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,21 @@ class Child(Base):
828828

829829
Base(param=10)
830830
Child(param=10)
831+
832+
[case testNamedTupleClassMethodWithGenericReturnValue]
833+
from typing import TypeVar, Type, NamedTuple
834+
835+
T = TypeVar('T', bound='Parent')
836+
837+
class Parent(NamedTuple):
838+
x: str
839+
840+
@classmethod
841+
def class_method(cls: Type[T]) -> T:
842+
return cls(x='text')
843+
844+
class Child(Parent):
845+
pass
846+
847+
reveal_type(Child.class_method()) # E: Revealed type is 'Tuple[builtins.str, fallback=__main__.Child]'
848+
[builtins fixtures/classmethod.pyi]

0 commit comments

Comments
 (0)