Skip to content

Loosen Mapping.update() overloads a little #14593

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

Merged
merged 2 commits into from
Aug 19, 2025
Merged

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Aug 19, 2025

We received a report in ty that said that the changes made in #14099 are now too restrictive. Mypy and ty will now reject a call like this, which feel like it should be valid because object is obviously a supertype of str:

def f(x: dict[object, object]):
    x.update(foo="bar")

the call is rejected because Mapping is invariant in its key type, meaning that mypy and ty cannot select this overload: dict[object, object] is not assignable to Mapping[str, _VT]:

typeshed/stdlib/typing.pyi

Lines 809 to 810 in ab0968b

@overload
def update(self: Mapping[str, _VT], **kwargs: _VT) -> None: ...

this PR uses a contravariant protocol for the self annotations in the MutableMapping.update() overloads, so that this overload can now be selected if the mapping has a key type that is str or any supertype of str. I think this should be sound?

Fixes python/mypy#10970, fixes astral-sh/ty#1048

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

aioredis (https://github.com/aio-libs/aioredis)
+ aioredis/client.py:4116: error: Unused "type: ignore" comment  [unused-ignore]
+ aioredis/client.py:4152: error: Unused "type: ignore" comment  [unused-ignore]

discord.py (https://github.com/Rapptz/discord.py)
- discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
+ discord/ext/commands/hybrid.py:508: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]
- discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "name", "description"  [misc]
+ discord/ext/commands/hybrid.py:629: error: Overlap between argument names and ** TypedDict items: "description", "name"  [misc]

@AlexWaygood
Copy link
Member Author

Nice -- the aioredis hits look like a false positive going away! https://github.com/aio-libs-abandoned/aioredis-py/blob/19be499015a8cf32580e937cbfd711fd48489eca/aioredis/client.py#L4105-L4116

@AlexWaygood AlexWaygood marked this pull request as ready for review August 19, 2025 18:16
@JelleZijlstra JelleZijlstra merged commit 8a56044 into main Aug 19, 2025
66 checks passed
@JelleZijlstra JelleZijlstra deleted the alex/mapping-update branch August 19, 2025 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: false positive in df.attrs.update() Subtype of mapping is not compatible type in .update() method
2 participants