Skip to content
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

Merged
merged 3 commits into from
Jan 20, 2022
Merged

Improve type for setdefault() #6941

merged 3 commits into from
Jan 20, 2022

Conversation

JelleZijlstra
Copy link
Member

  • 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)

- 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)
@JelleZijlstra
Copy link
Member Author

Actually, this is not correct: one-argument setdefault() should only be allowed if the dict's value type is compatible with None. But I don't know if there's a way to express that in the current type system.

@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 17, 2022

Actually, this is not correct: one-argument setdefault() should only be allowed if the dict's value type is compatible with None. But I don't know if there's a way to express that in the current type system.

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.

@JelleZijlstra
Copy link
Member Author

Yes, I think this change is still an improvement. I added a comment to make it explicit.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a 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?

@JelleZijlstra
Copy link
Member Author

That works! Test case with mypy:

from typing import Any


d: dict[int, str] = {}
d2: dict[int, str | None] = {}
d3: dict[int, Any] = {}

reveal_type(d.setdefault(1))
reveal_type(d.setdefault(1, "x"))
reveal_type(d2.setdefault(1))
reveal_type(d2.setdefault(1, "x"))
reveal_type(d3.setdefault(1))
reveal_type(d3.setdefault(1, "x"))

# stdlib/setdefault.py:8: error: Too few arguments for "setdefault" of "MutableMapping"
# stdlib/setdefault.py:8: note: Revealed type is "builtins.str*"
# stdlib/setdefault.py:9: note: Revealed type is "builtins.str*"
# stdlib/setdefault.py:10: note: Revealed type is "Union[builtins.str, None]"
# stdlib/setdefault.py:11: note: Revealed type is "Union[builtins.str, None]"
# stdlib/setdefault.py:12: note: Revealed type is "Union[Any, None]"
# stdlib/setdefault.py:13: note: Revealed type is "Any"

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja merged commit 1f000d2 into master Jan 20, 2022
@hauntsaninja hauntsaninja deleted the JelleZijlstra-patch-1 branch January 20, 2022 05:44
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.

3 participants