Skip to content

Commit b77fa8f

Browse files
committed
[_typeshed] Add SupportsGet
Commonly useful protocol. This also allows us to document the `__get__` annotations in a central place.
1 parent e7fe3fe commit b77fa8f

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

stdlib/_typeshed/__init__.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ class SupportsTrunc(Protocol):
160160

161161
# Mapping-like protocols
162162

163+
# The second and third overload could technically be combined, but splitting
164+
# them works better with some type checkers.
165+
class SupportsGet(Protocol[_KT_contra, _VT_co]):
166+
@overload
167+
def get(self, key: _KT_contra, /) -> _VT_co | None: ...
168+
@overload
169+
def get(self, key: _KT_contra, default: _VT_co, /) -> _VT_co: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant type as parameter
170+
@overload
171+
def get(self, key: _KT_contra, default: _T, /) -> _VT_co | _T: ...
172+
163173
# stable
164174
class SupportsItems(Protocol[_KT_co, _VT_co]):
165175
def items(self) -> AbstractSet[tuple[_KT_co, _VT_co]]: ...

0 commit comments

Comments
 (0)