Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix MutableMapping.update to require string keys #14099

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 1 commit into from
May 19, 2025
Merged
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
Fix MutableMapping.update to require string keys
Currently with most type checkers you can do:

```python
d: dict[int, int] = {}
d.update(a=2)
```

The reason for that is that `update` merely requires `MutableMapping[_KT, _VT]`, whereas as soon as you are passing kwargs, you must have a str key type.
  • Loading branch information
ndmitchell authored May 19, 2025
commit 7680e2ef77853c927d834ae560225760c390e391
10 changes: 7 additions & 3 deletions 10 stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -797,11 +797,15 @@ class MutableMapping(Mapping[_KT, _VT]):
# -- weakref.WeakValueDictionary.__ior__
# -- weakref.WeakKeyDictionary.__ior__
@overload
def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /, **kwargs: _VT) -> None: ...
def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
@overload
def update(self, m: Iterable[tuple[_KT, _VT]], /, **kwargs: _VT) -> None: ...
def update(self: Mapping[str, _VT], m: SupportsKeysAndGetItem[_KT, _VT], /, **kwargs: _VT) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be:

def update(self: Mapping[str, _VT], m: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ...

It's possible that _KT is constrained to str anyway via the self mapping, but I still think being explicit would be better (and easier on the type checkers).

Copy link
Member

Choose a reason for hiding this comment

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

I think SupportsKeysAndGetItem is invariant in the first arg so that might cause trouble?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#14100 for these cleanups.

@overload
def update(self, **kwargs: _VT) -> None: ...
def update(self, m: Iterable[tuple[_KT, _VT]], /) -> None: ...
@overload
def update(self: Mapping[str, _VT], m: Iterable[tuple[_KT, _VT]], /, **kwargs: _VT) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here.

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

Text = str

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.