Skip to content

Commit 2fe2202

Browse files
committed
Fix false positive for constrained NamedTuple self types
1 parent b41157b commit 2fe2202

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

mypy/checker.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,14 +1785,25 @@ def require_correct_self_argument(self, func: Type, defn: FuncDef) -> bool:
17851785
# and incorrectly rejected.
17861786
#
17871787
# Skip this check for constrained generic NamedTuple tuple self types.
1788+
has_constrained_typevar = False
1789+
namedtuple_ref_type: TupleType | None = None
1790+
1791+
proper_ref_type = get_proper_type(ref_type)
1792+
proper_arg_type = get_proper_type(arg_type)
1793+
1794+
if isinstance(proper_ref_type, TupleType):
1795+
namedtuple_ref_type = proper_ref_type
1796+
1797+
for item in proper_ref_type.items:
1798+
if isinstance(item, TypeVarType) and item.values:
1799+
has_constrained_typevar = True
1800+
break
1801+
17881802
skip_namedtuple_constrained_check = (
1789-
isinstance(ref_type, TupleType)
1790-
and isinstance(arg_type, TupleType)
1791-
and ref_type.partial_fallback.type.tuple_type is not None
1792-
and any(
1793-
isinstance(item, TypeVarType) and item.values
1794-
for item in ref_type.items
1795-
)
1803+
namedtuple_ref_type is not None
1804+
and isinstance(proper_arg_type, TupleType)
1805+
and namedtuple_ref_type.partial_fallback.type.is_named_tuple
1806+
and has_constrained_typevar
17961807
)
17971808

17981809
if (

0 commit comments

Comments
 (0)