Skip to content

Commit 09e6a2b

Browse files
authored
Fix crash on unpacking self in NamedTuple (#17351)
Fixes #17010 Fix is trivial: replicate the `TypeVar` handling logic in the caller.
1 parent 428a035 commit 09e6a2b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

mypy/checker.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,6 +3839,8 @@ def check_multi_assignment_from_tuple(
38393839
self.expr_checker.accept(rvalue, lvalue_type)
38403840
)
38413841

3842+
if isinstance(reinferred_rvalue_type, TypeVarLikeType):
3843+
reinferred_rvalue_type = get_proper_type(reinferred_rvalue_type.upper_bound)
38423844
if isinstance(reinferred_rvalue_type, UnionType):
38433845
# If this is an Optional type in non-strict Optional code, unwrap it.
38443846
relevant_items = reinferred_rvalue_type.relevant_items()

test-data/unit/check-namedtuple.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,3 +1412,14 @@ A(x=0).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "A" has inco
14121412
A(x=0).__replace__(y=1) # E: Unexpected keyword argument "y" for "__replace__" of "A"
14131413
[builtins fixtures/tuple.pyi]
14141414
[typing fixtures/typing-namedtuple.pyi]
1415+
1416+
[case testUnpackSelfNamedTuple]
1417+
import typing
1418+
1419+
class Foo(typing.NamedTuple):
1420+
bar: int
1421+
def baz(self: typing.Self) -> None:
1422+
x, = self
1423+
reveal_type(x) # N: Revealed type is "builtins.int"
1424+
[builtins fixtures/tuple.pyi]
1425+
[typing fixtures/typing-namedtuple.pyi]

0 commit comments

Comments
 (0)