From 53675b6ba3e268258e1d31be5d077f090f7428f1 Mon Sep 17 00:00:00 2001 From: Randolf Scholz Date: Sat, 24 Jan 2026 15:46:41 +0100 Subject: [PATCH] use 'Any' rather than object in Container --- stdlib/typing.pyi | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 96a05a291604..bb80d3f48fcf 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -642,9 +642,14 @@ class AsyncGenerator(AsyncIterator[_YieldT_co], Protocol[_YieldT_co, _SendT_cont @runtime_checkable class Container(Protocol[_T_co]): - # This is generic more on vibes than anything else - @abstractmethod - def __contains__(self, x: object, /) -> bool: ... + # This Protocol is broken, as it should have used a contravariant type variable. + # But since it has been used in contravariant types, we cannot change it without + # causing breakage. + # Therefore, the key is annotated as `Any`, so that implemented can override it + # appropriately. + # A typical usage in Collection[X] types may be to set it to the upper bound of X. + @abstractmethod + def __contains__(self, x: Any, /) -> bool: ... @runtime_checkable class Collection(Iterable[_T_co], Container[_T_co], Protocol[_T_co]):