Skip to content

Commit 35b688e

Browse files
Use contravariant type variable rather than object in Container (#15320)
1 parent 8a363f6 commit 35b688e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

stdlib/typing.pyi

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,14 +640,17 @@ class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_cont
640640
) -> Coroutine[Any, Any, _YieldT_co]: ...
641641
def aclose(self) -> Coroutine[Any, Any, None]: ...
642642

643+
_ContainerT_contra = TypeVar("_ContainerT_contra", contravariant=True, default=Any)
644+
643645
@runtime_checkable
644-
class Container(Protocol[_T_co]):
646+
class Container(Protocol[_ContainerT_contra]):
645647
# This is generic more on vibes than anything else
646648
@abstractmethod
647-
def __contains__(self, x: object, /) -> bool: ...
649+
def __contains__(self, x: _ContainerT_contra, /) -> bool: ...
648650

649651
@runtime_checkable
650-
class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):
652+
class Collection(Iterable[_T_co], Container[Any], Protocol[_T_co]):
653+
# Note: need to use Container[Any] instead of Container[_T_co] to ensure covariance.
651654
# Implement Sized (but don't have it as a base class).
652655
@abstractmethod
653656
def __len__(self) -> int: ...

0 commit comments

Comments
 (0)