-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Improve type for setdefault() #6941
Conversation
- With one argument, it may return None - With two arguments, it returns the default's type or the dict's value type. - Also remove incorrect `= ...` from `pop()`. The one-argument case has its own overload. Context: python/typing#1033 (reply in thread)
Actually, this is not correct: one-argument setdefault() should only be allowed if the dict's value type is compatible with |
It's not correct, but I think it might nonetheless be better than the current situation, in which mypy is "falling back" to the other (well, currently the only) overload and confidently proclaiming that a variable has a completely incorrect type. |
Yes, I think this change is still an improvement. I added a comment to make it explicit. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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.
Could do it with evaluated types :-)
Would (self: MutableMapping[_KT, _T | None], __key: KT) -> _T | None
work?
That works! Test case with mypy:
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
= ...
frompop()
. The one-argument case has its own overload.Context: python/typing#1033 (reply in thread)