Skip to content

Fix crash when namedtuple, classmethod and generic types used #6669

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

Merged
Merged
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
4 changes: 2 additions & 2 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,10 +923,10 @@ def is_type_obj(self) -> bool:
def type_object(self) -> mypy.nodes.TypeInfo:
assert self.is_type_obj()
ret = self.ret_type
if isinstance(ret, TupleType):
ret = ret.partial_fallback
if isinstance(ret, TypeVarType):
ret = ret.upper_bound
if isinstance(ret, TupleType):
ret = ret.partial_fallback
assert isinstance(ret, Instance)
return ret.type

Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,21 @@ class Child(Base):

Base(param=10)
Child(param=10)

[case testNamedTupleClassMethodWithGenericReturnValue]
from typing import TypeVar, Type, NamedTuple

T = TypeVar('T', bound='Parent')

class Parent(NamedTuple):
x: str

@classmethod
def class_method(cls: Type[T]) -> T:
return cls(x='text')

class Child(Parent):
pass

reveal_type(Child.class_method()) # E: Revealed type is 'Tuple[builtins.str, fallback=__main__.Child]'
[builtins fixtures/classmethod.pyi]