Skip to content

Conversation

@a12k
Copy link
Contributor

@a12k a12k commented Jan 3, 2026

Fixes gh-20512

This PR fixes a bug where calling .update() on a specialized generic TypedDict like Group[int] would fail type checking because the plugin reverted to the unspecialized definition (Group[ValT]).

After a lot of dead ends this one wasn't as straightforward as I thought initially. After spending a lot of time in checkmember.py and checkexpr.pyI finally found the issue in mypy/plugins/default.py. The call got the anonymous version of the TypedDict from the TypeInfo, which pointed back to the original generic declaration. This caused specialized types to be replaced by their original TypeVar placeholders during the signature construction for update(). Whew!

The fix basically uses ctx.type (the specialized type of TypedDict being updated) as the source for the items, then applies the anonymous fallback and sets required_keys to an empty set to keep the correct behavior of the update method.

Added a test in check-typeddict.test which seemed like the right place. All tests pass, as well as the original repro script from the linked issue.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solve!

It looks like there isn't actually a use for as_anonymous... Should we change it to get_anonymous_fallback? Could help other code paths avoid a similar mistake

@a12k
Copy link
Contributor Author

a12k commented Jan 5, 2026

Yeah! I can make that change.

@a12k
Copy link
Contributor Author

a12k commented Jan 5, 2026

@hauntsaninja let me know if that's what you had in mind. I tried a few different ways of cleaning this up, but mostly failed the test suite, so landed on the above changes.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 5, 2026

Ah sorry, I should have explained myself better. I just pushed a commit with the refactor I meant... Trying to prevent similar bugs in the future

@github-actions
Copy link
Contributor

github-actions bot commented Jan 5, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@a12k
Copy link
Contributor Author

a12k commented Jan 5, 2026

Ah sorry, I should have explained myself better. I just pushed a commit with the refactor I meant... Trying to prevent similar bugs in the future

Ah, I’m tracking now! Thanks for the refactor.

@hauntsaninja hauntsaninja merged commit c71fdb0 into python:master Jan 5, 2026
23 checks passed
@hauntsaninja
Copy link
Collaborator

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic TypedDict update() doesn't accept another instance of itself

2 participants