-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
+7
−3
Merged
Changes from all commits
Commits
File filter
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
commit 7680e2ef77853c927d834ae560225760c390e391
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: ... | ||
@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: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be:
It's possible that
_KT
is constrained tostr
anyway via the self mapping, but I still think being explicit would be better (and easier on the type checkers).There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#14100 for these cleanups.