Skip to content
Open
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
6 changes: 5 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self) -> None:
IterationErrorWatcher,
report_internal_error,
)
from mypy.expandtype import expand_type
from mypy.expandtype import expand_type, expand_type_by_instance
from mypy.literals import Key, extract_var_from_literal_hash, literal, literal_hash
from mypy.maptype import map_instance_to_supertype
from mypy.meet import is_overlapping_types, meet_types
Expand Down Expand Up @@ -1778,6 +1778,10 @@ def require_correct_self_argument(self, func: Type, defn: FuncDef) -> bool:
return True

arg_type = func.arg_types[0]
p_arg = get_proper_type(arg_type)
ref_type = get_proper_type(ref_type)
if isinstance(ref_type, TupleType) and isinstance(p_arg, TupleType):
ref_type = expand_type_by_instance(ref_type, p_arg.partial_fallback)
if defn.is_class or defn.name == "__new__":
ref_type = mypy.types.TypeType.make_normalized(ref_type)
if is_same_type(arg_type, ref_type):
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,22 @@ reveal_type(foo(x, nts)) # N: Revealed type is "builtins.tuple[builtins.int | b
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testGenericNamedTupleWithConstrainedTypeParams]
# https://github.com/python/mypy/issues/21453

from typing import Generic, NamedTuple, TypeVar

S = TypeVar("S", str, bytes)

class Result(NamedTuple, Generic[S]):
value: S

def get_value(self) -> S:
reveal_type(self) # N: Revealed type is "tuple[builtins.str, fallback=__main__.Result[builtins.str]]" \
# N: Revealed type is "tuple[builtins.bytes, fallback=__main__.Result[builtins.bytes]]"
return self.value
[builtins fixtures/tuple.pyi]

[case testGenericNamedTupleCallSyntax]
from typing import NamedTuple, TypeVar

Expand Down
Loading