From 7c7629d9096dd7c096e823f94748202d578297aa Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sat, 16 Nov 2024 07:55:38 -0800 Subject: [PATCH] add typing._Final (#13015) This is the subset of typing module internal base classes that are stable over all supported versions of python. --- stdlib/@tests/stubtest_allowlists/common.txt | 2 ++ stdlib/typing.pyi | 6 ++++-- stdlib/typing_extensions.pyi | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 572785db2..b6efe09bf 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -462,6 +462,8 @@ typing_extensions\.ParamSpec.* typing(_extensions)?\.Generic typing\.Protocol typing(_extensions)?\._TypedDict +typing._Final +typing._Final.__init_subclass__ # Special primitives typing_extensions\.Final diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 27ae11dab..f92936ec3 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -133,6 +133,8 @@ if sys.version_info >= (3, 13): Any = object() +class _Final: ... + def final(f: _T) -> _T: ... @final class TypeVar: @@ -191,7 +193,7 @@ _promote = object() # N.B. Keep this definition in sync with typing_extensions._SpecialForm @final -class _SpecialForm: +class _SpecialForm(_Final): def __getitem__(self, parameters: Any) -> object: ... if sys.version_info >= (3, 10): def __or__(self, other: Any) -> _SpecialForm: ... @@ -973,7 +975,7 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta): def __ior__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc] @final -class ForwardRef: +class ForwardRef(_Final): __forward_arg__: str __forward_code__: CodeType __forward_evaluated__: bool diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 3240eff0f..c32bf7516 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -190,8 +190,10 @@ _T = typing.TypeVar("_T") _F = typing.TypeVar("_F", bound=Callable[..., Any]) _TC = typing.TypeVar("_TC", bound=type[object]) +class _Final: ... # This should be imported from typing but that breaks pytype + # unfortunately we have to duplicate this class definition from typing.pyi or we break pytype -class _SpecialForm: +class _SpecialForm(_Final): def __getitem__(self, parameters: Any) -> object: ... if sys.version_info >= (3, 10): def __or__(self, other: Any) -> _SpecialForm: ...