Skip to content

Commit 2602adc

Browse files
use contravariant typevar
1 parent 53675b6 commit 2602adc

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

stdlib/typing.pyi

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ _S = TypeVar("_S")
403403
_KT = TypeVar("_KT") # Key type.
404404
_VT = TypeVar("_VT") # Value type.
405405
_T_co = TypeVar("_T_co", covariant=True) # Any type covariant containers.
406+
_T_contra = TypeVar("_T_contra", contravariant=True) # Any type contravariant containers.
406407
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
407408
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
408409
_TC = TypeVar("_TC", bound=type[object])
@@ -641,18 +642,12 @@ class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_cont
641642
def aclose(self) -> Coroutine[Any, Any, None]: ...
642643

643644
@runtime_checkable
644-
class Container(Protocol[_T_co]):
645-
# This Protocol is broken, as it should have used a contravariant type variable.
646-
# But since it has been used in contravariant types, we cannot change it without
647-
# causing breakage.
648-
# Therefore, the key is annotated as `Any`, so that implemented can override it
649-
# appropriately.
650-
# A typical usage in Collection[X] types may be to set it to the upper bound of X.
645+
class Container(Protocol[_T_contra]):
651646
@abstractmethod
652-
def __contains__(self, x: Any, /) -> bool: ...
647+
def __contains__(self, x: _T_contra, /) -> bool: ...
653648

654649
@runtime_checkable
655-
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
650+
class Collection(Iterable[_T_co], Container[Any], Protocol[_T_co]):
656651
# Implement Sized (but don't have it as a base class).
657652
@abstractmethod
658653
def __len__(self) -> int: ...

0 commit comments

Comments
 (0)