Skip to content

Commit

Permalink
Improve type for setdefault()
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
JelleZijlstra authored Jan 17, 2022
1 parent 18d746b commit 92438dc
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,12 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
@overload
def pop(self, __key: _KT) -> _VT: ...
@overload
def pop(self, __key: _KT, __default: _VT | _T = ...) -> _VT | _T: ...
def pop(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ...
def popitem(self) -> tuple[_KT, _VT]: ...
def setdefault(self, __key: _KT, __default: _VT = ...) -> _VT: ...
@overload
def setdefault(self, __key: _KT) -> _VT | None: ...
@overload
def setdefault(self, __key: _KT, __default: _VT) -> _VT: ...
# 'update' used to take a Union, but using overloading is better.
# The second overloaded type here is a bit too general, because
# Mapping[Tuple[_KT, _VT], W] is a subclass of Iterable[Tuple[_KT, _VT]],
Expand Down

0 comments on commit 92438dc

Please sign in to comment.