Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix generic NamedTuple class pattern matching
  • Loading branch information
paulorochaoliveira committed Apr 1, 2026
commit 883b8660d1e82c7483a17c169b8c030fac924b5d
2 changes: 2 additions & 0 deletions mypy/checkpattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ def get_class_pattern_type_ranges(self, typ: Type, o: ClassPattern) -> list[Type

if isinstance(p_typ, FunctionLike) and p_typ.is_type_obj():
typ = fill_typevars_with_any(p_typ.type_object())
if isinstance(typ, TupleType) and typ.partial_fallback.type.is_named_tuple:
typ = typ.partial_fallback
return [TypeRange(typ, is_upper_bound=False)]
if (
isinstance(o.class_ref.node, Var)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,22 @@ match m:
reveal_type(m) # N: Revealed type is "__main__.A[builtins.int]"
reveal_type(i) # N: Revealed type is "builtins.int"

[case testMatchClassPatternCaptureGenericNamedTupleAlreadyKnown]
from typing import Generic, NamedTuple, TypeVar

T = TypeVar("T")

class A(NamedTuple, Generic[T]):
a: T

m: A[int]

match m:
case A(a=i):
reveal_type(i) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testMatchClassPatternCaptureFilledGenericTypeAlias]
from typing import Generic, TypeVar

Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,20 @@ e: M[bool] # E: Value of type variable "T" of "M" cannot be "bool"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testMatchClassPatternCapturePEP695GenericNamedTupleAlreadyKnown]
from typing import NamedTuple

class N[T](NamedTuple):
x: T

n: N[int]

match n:
case N(x=value):
reveal_type(value) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

[case testPEP695GenericTypedDict]
from typing import TypedDict

Expand Down
Loading