Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on unpacking self in NamedTuple #17351

Merged
merged 2 commits into from
Jun 9, 2024
Merged
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
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]
Loading