From ef346aba0d214abc5f027876739a5cb68861815e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Wed, 29 Nov 2023 19:00:58 +0000 Subject: [PATCH] Fixes to `typing` and `typing_extensions` stubs (#11086) --- requirements-tests.txt | 2 +- stdlib/typing.pyi | 7 +++++- stdlib/typing_extensions.pyi | 30 +++++++++++++++++------- tests/stubtest_allowlists/py310.txt | 3 --- tests/stubtest_allowlists/py311.txt | 1 - tests/stubtest_allowlists/py312.txt | 2 -- tests/stubtest_allowlists/py3_common.txt | 3 --- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/requirements-tests.txt b/requirements-tests.txt index 084734cc5..510506f93 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -20,7 +20,7 @@ stubdefaulter==0.1.0 termcolor>=2.3 tomli==2.0.1 tomlkit==0.12.3 -typing_extensions>=4.8.0 +typing_extensions>=4.9.0rc1 # Type stubs used to type check our scripts. types-pyyaml>=6.0.12.7 diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 7694157d7..ffe67e2e2 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -281,7 +281,12 @@ if sys.version_info >= (3, 10): class NewType: def __init__(self, name: str, tp: Any) -> None: ... - def __call__(self, __x: _T) -> _T: ... + if sys.version_info >= (3, 11): + @staticmethod + def __call__(__x: _T) -> _T: ... + else: + def __call__(self, x: _T) -> _T: ... + def __or__(self, other: Any) -> _SpecialForm: ... def __ror__(self, other: Any) -> _SpecialForm: ... __supertype__: type diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index b5e2341cd..5c5b756f5 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -182,6 +182,7 @@ __all__ = [ "is_protocol", "no_type_check", "no_type_check_decorator", + "ReadOnly", ] _T = typing.TypeVar("_T") @@ -220,6 +221,8 @@ def IntVar(name: str) -> Any: ... # returns a new TypeVar class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): __required_keys__: ClassVar[frozenset[str]] __optional_keys__: ClassVar[frozenset[str]] + __readonly_keys__: ClassVar[frozenset[str]] + __mutable_keys__: ClassVar[frozenset[str]] __total__: ClassVar[bool] __orig_bases__: ClassVar[tuple[Any, ...]] def copy(self) -> Self: ... @@ -283,7 +286,6 @@ class SupportsIndex(Protocol, metaclass=abc.ABCMeta): if sys.version_info >= (3, 10): from typing import ( Concatenate as Concatenate, - NewType as NewType, ParamSpecArgs as ParamSpecArgs, ParamSpecKwargs as ParamSpecKwargs, TypeAlias as TypeAlias, @@ -308,18 +310,13 @@ else: TypeGuard: _SpecialForm def is_typeddict(tp: object) -> bool: ... - class NewType: - def __init__(self, name: str, tp: Any) -> None: ... - def __call__(self, __x: _T) -> _T: ... - __supertype__: type - -# New things in 3.11 -# NamedTuples are not new, but the ability to create generic NamedTuples is new in 3.11 +# New and changed things in 3.11 if sys.version_info >= (3, 11): from typing import ( LiteralString as LiteralString, NamedTuple as NamedTuple, Never as Never, + NewType as NewType, NotRequired as NotRequired, Required as Required, Self as Self, @@ -376,6 +373,14 @@ else: def _replace(self, **kwargs: Any) -> Self: ... + class NewType: + def __init__(self, name: str, tp: Any) -> None: ... + def __call__(self, __obj: _T) -> _T: ... + __supertype__: type + if sys.version_info >= (3, 10): + def __or__(self, other: Any) -> _SpecialForm: ... + def __ror__(self, other: Any) -> _SpecialForm: ... + # New things in 3.xx # The `default` parameter was added to TypeVar, ParamSpec, and TypeVarTuple (PEP 696) # The `infer_variance` parameter was added to TypeVar in 3.12 (PEP 695) @@ -449,7 +454,12 @@ class TypeVarTuple: def __init__(self, name: str, *, default: Any | None = None) -> None: ... def __iter__(self) -> Any: ... # Unpack[Self] -def deprecated(__msg: str, *, category: type[Warning] | None = ..., stacklevel: int = 1) -> Callable[[_T], _T]: ... +class deprecated: + message: str + category: type[Warning] | None + stacklevel: int + def __init__(self, __message: str, *, category: type[Warning] | None = ..., stacklevel: int = 1) -> None: ... + def __call__(self, __arg: _T) -> _T: ... if sys.version_info >= (3, 12): from collections.abc import Buffer as Buffer @@ -496,3 +506,5 @@ class Doc: def __init__(self, __documentation: str) -> None: ... def __hash__(self) -> int: ... def __eq__(self, other: object) -> bool: ... + +ReadOnly: _SpecialForm diff --git a/tests/stubtest_allowlists/py310.txt b/tests/stubtest_allowlists/py310.txt index ed7f6eb62..8b49457bc 100644 --- a/tests/stubtest_allowlists/py310.txt +++ b/tests/stubtest_allowlists/py310.txt @@ -149,9 +149,6 @@ dataclasses.KW_ONLY # We pretend it's a read-only property for forward compatibility with 3.12 typing.ParamSpec(Args|Kwargs).__origin__ -# https://github.com/python/mypy/issues/15302 -typing.NewType.__call__ - # Problematic protocol signatures at runtime, see source code comments. importlib.abc.Traversable.joinpath importlib.abc.Traversable.open diff --git a/tests/stubtest_allowlists/py311.txt b/tests/stubtest_allowlists/py311.txt index c34c55bf1..a4a95680c 100644 --- a/tests/stubtest_allowlists/py311.txt +++ b/tests/stubtest_allowlists/py311.txt @@ -14,7 +14,6 @@ enum.Enum._generate_next_value_ enum.StrEnum._generate_next_value_ importlib.abc.Finder.find_module tkinter._VersionInfoType.__doc__ -typing.NewType.__call__ typing.NewType.__mro_entries__ # Modules that exist at runtime, but shouldn't be added to typeshed diff --git a/tests/stubtest_allowlists/py312.txt b/tests/stubtest_allowlists/py312.txt index 7fc637ee6..792f4a25d 100644 --- a/tests/stubtest_allowlists/py312.txt +++ b/tests/stubtest_allowlists/py312.txt @@ -12,7 +12,6 @@ _csv.Writer configparser.LegacyInterpolation.__init__ enum.Enum.__init__ tkinter._VersionInfoType.__doc__ -typing.NewType.__call__ typing.NewType.__mro_entries__ # ========== @@ -103,7 +102,6 @@ typing\._SpecialForm.* typing\.NamedTuple typing\.LiteralString typing\.Annotated -typing_extensions\.Protocol # These only exist to give a better error message if you try to subclass an instance typing.ParamSpec.__mro_entries__ diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index ab16e4ef6..6f5eb0b72 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -523,9 +523,6 @@ typing(_extensions)?\.AbstractSet typing_extensions.NewType.__mro_entries__ # just exists for an error message -# https://github.com/python/mypy/issues/15302 -typing_extensions.NewType.__call__ - # Typing-related weirdness _collections_abc.Callable _typeshed.* # Utility types for typeshed, doesn't exist at runtime