Skip to content

Commit

Permalink
Fix crash on unpacking self in NamedTuple (#17351)
Browse files Browse the repository at this point in the history
Fixes #17010

Fix is trivial: replicate the `TypeVar` handling logic in the caller.
  • Loading branch information
ilevkivskyi authored Jun 9, 2024
1 parent 428a035 commit 09e6a2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3839,6 +3839,8 @@ def check_multi_assignment_from_tuple(
self.expr_checker.accept(rvalue, lvalue_type)
)

if isinstance(reinferred_rvalue_type, TypeVarLikeType):
reinferred_rvalue_type = get_proper_type(reinferred_rvalue_type.upper_bound)
if isinstance(reinferred_rvalue_type, UnionType):
# If this is an Optional type in non-strict Optional code, unwrap it.
relevant_items = reinferred_rvalue_type.relevant_items()
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1412,3 +1412,14 @@ A(x=0).__replace__(x="asdf") # E: Argument "x" to "__replace__" of "A" has inco
A(x=0).__replace__(y=1) # E: Unexpected keyword argument "y" for "__replace__" of "A"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testUnpackSelfNamedTuple]
import typing

class Foo(typing.NamedTuple):
bar: int
def baz(self: typing.Self) -> None:
x, = self
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

0 comments on commit 09e6a2b

Please sign in to comment.