From ef0a5c2d12c75276c5aa89ae91cc0d84d3df4834 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sun, 12 May 2024 15:38:38 +0200 Subject: [PATCH] Replace types._Cell with types.CellType (#11904) Closes #11901 --- stdlib/builtins.pyi | 8 ++++---- stdlib/types.pyi | 21 +++++++++------------ stubs/six/six/__init__.pyi | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 7587ee0d3..f2da50b25 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -31,7 +31,7 @@ from _typeshed import ( ) from collections.abc import Awaitable, Callable, Iterable, Iterator, MutableSet, Reversible, Set as AbstractSet, Sized from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper -from types import CodeType, TracebackType, _Cell +from types import CellType, CodeType, TracebackType # mypy crashes if any of {ByteString, Sequence, MutableSequence, Mapping, MutableMapping} are imported from collections.abc in builtins.pyi from typing import ( # noqa: Y022 @@ -951,7 +951,7 @@ class tuple(Sequence[_T_co]): class function: # Make sure this class definition stays roughly in line with `types.FunctionType` @property - def __closure__(self) -> tuple[_Cell, ...] | None: ... + def __closure__(self) -> tuple[CellType, ...] | None: ... __code__: CodeType __defaults__: tuple[Any, ...] | None __dict__: dict[str, Any] @@ -1333,7 +1333,7 @@ if sys.version_info >= (3, 11): locals: Mapping[str, object] | None = None, /, *, - closure: tuple[_Cell, ...] | None = None, + closure: tuple[CellType, ...] | None = None, ) -> None: ... else: @@ -1794,7 +1794,7 @@ def __import__( fromlist: Sequence[str] = (), level: int = 0, ) -> types.ModuleType: ... -def __build_class__(func: Callable[[], _Cell | Any], name: str, /, *bases: Any, metaclass: Any = ..., **kwds: Any) -> Any: ... +def __build_class__(func: Callable[[], CellType | Any], name: str, /, *bases: Any, metaclass: Any = ..., **kwds: Any) -> Any: ... if sys.version_info >= (3, 10): from types import EllipsisType diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 52af9ef8f..38940b434 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -65,18 +65,11 @@ _T2 = TypeVar("_T2") _KT = TypeVar("_KT") _VT_co = TypeVar("_VT_co", covariant=True) -@final -class _Cell: - def __new__(cls, contents: object = ..., /) -> Self: ... - def __eq__(self, value: object, /) -> bool: ... - __hash__: ClassVar[None] # type: ignore[assignment] - cell_contents: Any - # Make sure this class definition stays roughly in line with `builtins.function` @final class FunctionType: @property - def __closure__(self) -> tuple[_Cell, ...] | None: ... + def __closure__(self) -> tuple[CellType, ...] | None: ... __code__: CodeType __defaults__: tuple[Any, ...] | None __dict__: dict[str, Any] @@ -99,7 +92,7 @@ class FunctionType: globals: dict[str, Any], name: str | None = ..., argdefs: tuple[object, ...] | None = ..., - closure: tuple[_Cell, ...] | None = ..., + closure: tuple[CellType, ...] | None = ..., ) -> Self: ... def __call__(self, *args: Any, **kwargs: Any) -> Any: ... @overload @@ -334,6 +327,12 @@ class ModuleType: # using `builtins.__import__` or `importlib.import_module` less painful def __getattr__(self, name: str) -> Any: ... +@final +class CellType: + def __new__(cls, contents: object = ..., /) -> Self: ... + __hash__: ClassVar[None] # type: ignore[assignment] + cell_contents: Any + _YieldT_co = TypeVar("_YieldT_co", covariant=True) _SendT_contra = TypeVar("_SendT_contra", contravariant=True) _ReturnT_co = TypeVar("_ReturnT_co", covariant=True) @@ -403,7 +402,7 @@ class CoroutineType(Coroutine[_YieldT_co, _SendT_contra, _ReturnT_co]): @final class MethodType: @property - def __closure__(self) -> tuple[_Cell, ...] | None: ... # inherited from the added function + def __closure__(self) -> tuple[CellType, ...] | None: ... # inherited from the added function @property def __defaults__(self) -> tuple[Any, ...] | None: ... # inherited from the added function @property @@ -568,8 +567,6 @@ def coroutine(func: Callable[_P, Generator[Any, Any, _R]]) -> Callable[_P, Await @overload def coroutine(func: _Fn) -> _Fn: ... -CellType = _Cell - if sys.version_info >= (3, 9): class GenericAlias: @property diff --git a/stubs/six/six/__init__.pyi b/stubs/six/six/__init__.pyi index 7f317169b..e5a8fdbaa 100644 --- a/stubs/six/six/__init__.pyi +++ b/stubs/six/six/__init__.pyi @@ -44,7 +44,7 @@ Iterator = object def get_method_function(meth: types.MethodType) -> types.FunctionType: ... def get_method_self(meth: types.MethodType) -> object: ... -def get_function_closure(fun: types.FunctionType) -> tuple[types._Cell, ...] | None: ... +def get_function_closure(fun: types.FunctionType) -> tuple[types.CellType, ...] | None: ... def get_function_code(fun: types.FunctionType) -> types.CodeType: ... def get_function_defaults(fun: types.FunctionType) -> tuple[Any, ...] | None: ... def get_function_globals(fun: types.FunctionType) -> dict[str, Any]: ...