-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
WIP: Mapping.{get,pop} can return default type #223
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
WIP: Mapping.{get,pop} can return default type #223
Conversation
Yes, that looks better. That's what pytype has in its builtins, too: https://github.com/google/pytype/blob/master/pytype/pytd/builtins/__builtin__.pytd#L485 |
def get(self, k: _KT, default: _VT = None) -> _VT: ... | ||
def pop(self, k: _KT, default: _VT = ...) -> _VT: ... | ||
def get(self, k: _KT, default: _T = None) -> Union[_VT, _T]: ... | ||
def pop(self, k: _KT, default: _T = ...) -> Union[_VT, _T]: ... |
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 pop() will need to be overloaded -- with one arg there's no possibility of it returning the default value (unlike get()).
ae7430e
to
cceebd5
Compare
I took the path of overloading, it seems nicer (and clearer). We still need to merge the mypy PR though. |
@overload | ||
def get(self, k: _KT) -> Optional[_VT]: ... | ||
@overload | ||
def get(self, k: _KT, default: _T = ...) -> Union[_VT, _T]: ... |
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.
This overload variant (and all the others like it) should no longer have a default = ...
.
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.
Ho, correct, thanks for reviewing.
The overload will be needed under strict optional checking anyways, as otherwise there's no way to have the default With this change we can also make |
cceebd5
to
3de5d20
Compare
So I did, but, you may want to check it because I just replaced |
Sadly this broke more than mypy. Our two internal repos each showed several problems due to this. I think I'll go roll this back so that some other mypy developer doesn't accidentally release mypy with this incorporated. |
This reverts commit d43adbe. Here's a simple example of code that breaks with this PR: from typing import Mapping, Dict, Tuple a = {('0', '0'): 42} # type: Mapping[Tuple[str, str], int] b = a.get(('1', '1'), 0) This gives an error on the last line: error: No overload variant of "get" of "dict" matches argument types [Tuple[builtins.str, builtins.str], builtins.int]
Here's a simple repo: it seems to occur whenever the mapping keys are a tuple: from typing import Mapping, Dict, Tuple
a = {('0', '0'): 42} # type: Dict[Tuple[str, str], int]
b = a.get(('1', '1'), 0) This gives an error on the last line:
A similar error shows when the type uses |
So python/mypy#1595 is fixed, but there are still some remaining issues with this PR in our internal repos. I will see if I can find the time to understand those. |
Note to self: this was reverted, but I want to try again, because I still On Sat, May 28, 2016 at 2:09 AM, Valérian Rousset [email protected]
--Guido van Rossum (python.org/~guido) |
Because this PR was merged, it'd be easy for us to lose track of there still being something to do here, so I filed a new issue #278 for round 2. |
Fixes several issues related to subclass checks against custom subclasses of generic collections.
Do not merge yet, it makes some tests from mypy fail, first merge it in mypy.
When using a
Mapping
, there is usually the idea toget
orpop
a key with adefault
of an existing guard object (instead of trying and expecting an exception). But in current typeshed, it is stated asWouldn't it be more correct to have something like this?
thus also respecting the definition of
dict.get
which is stated in the documentation as