From 9bed9805f9298ce6027a435f2f06f2364c5bba11 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 2 Oct 2022 20:48:06 +0200 Subject: [PATCH 1/5] Add initial support for TypeVarLike default parameters (PEP 696) --- stdlib/typing_extensions.pyi | 84 ++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 787af1f4034e..2c7f3fbeae86 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -2,6 +2,7 @@ import _typeshed import abc import collections import sys +import typing from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import IdentityFunction from collections.abc import Iterable @@ -27,7 +28,6 @@ from typing import ( # noqa: Y022,Y027,Y039 Sequence, Text as Text, Type as Type, - TypeVar, _Alias, overload as overload, type_check_only, @@ -43,6 +43,7 @@ __all__ = [ "ParamSpecKwargs", "Self", "Type", + "TypeVar", "TypeVarTuple", "Unpack", "Awaitable", @@ -89,9 +90,9 @@ __all__ = [ "get_type_hints", ] -_T = TypeVar("_T") -_F = TypeVar("_F", bound=Callable[..., Any]) -_TC = TypeVar("_TC", bound=Type[object]) +_T = typing.TypeVar("_T") +_F = typing.TypeVar("_F", bound=Callable[..., Any]) +_TC = typing.TypeVar("_TC", bound=Type[object]) # unfortunately we have to duplicate this class definition from typing.pyi or we break pytype class _SpecialForm: @@ -167,7 +168,6 @@ class SupportsIndex(Protocol, metaclass=abc.ABCMeta): if sys.version_info >= (3, 10): from typing import ( Concatenate as Concatenate, - ParamSpec as ParamSpec, ParamSpecArgs as ParamSpecArgs, ParamSpecKwargs as ParamSpecKwargs, TypeAlias as TypeAlias, @@ -183,18 +183,6 @@ else: __origin__: ParamSpec def __init__(self, origin: ParamSpec) -> None: ... - class ParamSpec: - __name__: str - __bound__: type[Any] | None - __covariant__: bool - __contravariant__: bool - def __init__( - self, name: str, *, bound: None | type[Any] | str = ..., contravariant: bool = ..., covariant: bool = ... - ) -> None: ... - @property - def args(self) -> ParamSpecArgs: ... - @property - def kwargs(self) -> ParamSpecKwargs: ... Concatenate: _SpecialForm TypeAlias: _SpecialForm TypeGuard: _SpecialForm @@ -233,12 +221,6 @@ else: LiteralString: _SpecialForm Unpack: _SpecialForm - @final - class TypeVarTuple: - __name__: str - def __init__(self, name: str) -> None: ... - def __iter__(self) -> Any: ... # Unpack[Self] - def dataclass_transform( *, eq_default: bool = ..., @@ -268,3 +250,59 @@ else: def _asdict(self) -> collections.OrderedDict[str, Any]: ... def _replace(self: _typeshed.Self, **kwargs: Any) -> _typeshed.Self: ... + +# New things in 3.12 +# The `default` parameter was added to TypeVar, ParamSpec, and TypeVarTuple (PEP 696) +if sys.version_info >= (3, 12): + from typing import ParamSpec as ParamSpec, TypeVar as TypeVar, TypeVarTuple as TypeVarTuple +else: + @final + class TypeVar: + __name__: str + __bound__: Any | None + __constraints__: tuple[Any, ...] + __covariant__: bool + __contravariant__: bool + __default__: Any | None + def __init__( + self, + name: str, + *constraints: Any, + bound: Any | None = ..., + covariant: bool = ..., + contravariant: bool = ..., + default: Any | None = ..., + ) -> None: ... + if sys.version_info >= (3, 10): + def __or__(self, right: Any) -> _SpecialForm: ... + def __ror__(self, left: Any) -> _SpecialForm: ... + if sys.version_info >= (3, 11): + def __typing_subst__(self, arg: Incomplete) -> Incomplete: ... + + @final + class ParamSpec: + __name__: str + __bound__: type[Any] | None + __covariant__: bool + __contravariant__: bool + __default__: type[Any] | None + def __init__( + self, + name: str, + *, + bound: None | type[Any] | str = ..., + contravariant: bool = ..., + covariant: bool = ..., + default: type[Any] | str | None = ..., + ) -> None: ... + @property + def args(self) -> ParamSpecArgs: ... + @property + def kwargs(self) -> ParamSpecKwargs: ... + + @final + class TypeVarTuple: + __name__: str + __default__: Any | None + def __init__(self, name: str, *, default: Any | None = ...) -> None: ... + def __iter__(self) -> Any: ... # Unpack[Self] From 29dffc94105bb88db6f0e3dd5831b3982d1d5855 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 2 Oct 2022 21:00:37 +0200 Subject: [PATCH 2/5] Fix tests --- stdlib/typing_extensions.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 2c7f3fbeae86..dbef3243e8a5 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -4,7 +4,7 @@ import collections import sys import typing from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction +from _typeshed import IdentityFunction, Incomplete from collections.abc import Iterable from typing import ( # noqa: Y022,Y027,Y039 TYPE_CHECKING as TYPE_CHECKING, @@ -198,7 +198,6 @@ if sys.version_info >= (3, 11): NotRequired as NotRequired, Required as Required, Self as Self, - TypeVarTuple as TypeVarTuple, Unpack as Unpack, assert_never as assert_never, assert_type as assert_type, From 8c8708a4346ee28df56cea9462e17b5c82d97f89 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 3 Oct 2022 22:42:55 +0200 Subject: [PATCH 3/5] Revert conditional exports --- stdlib/typing_extensions.pyi | 101 +++++++++++++++++------------------ 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index dbef3243e8a5..39b32442a8c4 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -250,58 +250,55 @@ else: def _replace(self: _typeshed.Self, **kwargs: Any) -> _typeshed.Self: ... -# New things in 3.12 +# New things in 3.xx # The `default` parameter was added to TypeVar, ParamSpec, and TypeVarTuple (PEP 696) -if sys.version_info >= (3, 12): - from typing import ParamSpec as ParamSpec, TypeVar as TypeVar, TypeVarTuple as TypeVarTuple -else: - @final - class TypeVar: - __name__: str - __bound__: Any | None - __constraints__: tuple[Any, ...] - __covariant__: bool - __contravariant__: bool - __default__: Any | None - def __init__( - self, - name: str, - *constraints: Any, - bound: Any | None = ..., - covariant: bool = ..., - contravariant: bool = ..., - default: Any | None = ..., - ) -> None: ... - if sys.version_info >= (3, 10): - def __or__(self, right: Any) -> _SpecialForm: ... - def __ror__(self, left: Any) -> _SpecialForm: ... - if sys.version_info >= (3, 11): - def __typing_subst__(self, arg: Incomplete) -> Incomplete: ... +@final +class TypeVar: + __name__: str + __bound__: Any | None + __constraints__: tuple[Any, ...] + __covariant__: bool + __contravariant__: bool + __default__: Any | None + def __init__( + self, + name: str, + *constraints: Any, + bound: Any | None = ..., + covariant: bool = ..., + contravariant: bool = ..., + default: Any | None = ..., + ) -> None: ... + if sys.version_info >= (3, 10): + def __or__(self, right: Any) -> _SpecialForm: ... + def __ror__(self, left: Any) -> _SpecialForm: ... + if sys.version_info >= (3, 11): + def __typing_subst__(self, arg: Incomplete) -> Incomplete: ... - @final - class ParamSpec: - __name__: str - __bound__: type[Any] | None - __covariant__: bool - __contravariant__: bool - __default__: type[Any] | None - def __init__( - self, - name: str, - *, - bound: None | type[Any] | str = ..., - contravariant: bool = ..., - covariant: bool = ..., - default: type[Any] | str | None = ..., - ) -> None: ... - @property - def args(self) -> ParamSpecArgs: ... - @property - def kwargs(self) -> ParamSpecKwargs: ... +@final +class ParamSpec: + __name__: str + __bound__: type[Any] | None + __covariant__: bool + __contravariant__: bool + __default__: type[Any] | None + def __init__( + self, + name: str, + *, + bound: None | type[Any] | str = ..., + contravariant: bool = ..., + covariant: bool = ..., + default: type[Any] | str | None = ..., + ) -> None: ... + @property + def args(self) -> ParamSpecArgs: ... + @property + def kwargs(self) -> ParamSpecKwargs: ... - @final - class TypeVarTuple: - __name__: str - __default__: Any | None - def __init__(self, name: str, *, default: Any | None = ...) -> None: ... - def __iter__(self) -> Any: ... # Unpack[Self] +@final +class TypeVarTuple: + __name__: str + __default__: Any | None + def __init__(self, name: str, *, default: Any | None = ...) -> None: ... + def __iter__(self) -> Any: ... # Unpack[Self] From 06e0bf7a16b165dfe62b36c87a167687e55f48f3 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 6 Oct 2022 16:00:08 -0700 Subject: [PATCH 4/5] Apply suggestions from code review --- stdlib/typing_extensions.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 39b32442a8c4..5f74b872930f 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -252,6 +252,7 @@ else: # 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 (PEP 695) @final class TypeVar: __name__: str @@ -268,6 +269,7 @@ class TypeVar: covariant: bool = ..., contravariant: bool = ..., default: Any | None = ..., + infer_variance: bool = ..., ) -> None: ... if sys.version_info >= (3, 10): def __or__(self, right: Any) -> _SpecialForm: ... From 53209fcb19c456a2cb96c291046ca68a20e2520d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 6 Oct 2022 16:04:55 -0700 Subject: [PATCH 5/5] Add Any and override --- stdlib/typing_extensions.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index 5f74b872930f..df2c1c431c65 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -8,7 +8,7 @@ from _typeshed import IdentityFunction, Incomplete from collections.abc import Iterable from typing import ( # noqa: Y022,Y027,Y039 TYPE_CHECKING as TYPE_CHECKING, - Any, + Any as Any, AsyncContextManager as AsyncContextManager, AsyncGenerator as AsyncGenerator, AsyncIterable as AsyncIterable, @@ -34,6 +34,7 @@ from typing import ( # noqa: Y022,Y027,Y039 ) __all__ = [ + "Any", "ClassVar", "Concatenate", "Final", @@ -71,6 +72,7 @@ __all__ = [ "Literal", "NewType", "overload", + "override", "Protocol", "reveal_type", "runtime", @@ -253,6 +255,7 @@ else: # 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 (PEP 695) +# typing_extensions.override (PEP 698) @final class TypeVar: __name__: str @@ -304,3 +307,5 @@ class TypeVarTuple: __default__: Any | None def __init__(self, name: str, *, default: Any | None = ...) -> None: ... def __iter__(self) -> Any: ... # Unpack[Self] + +def override(__arg: _F) -> _F: ...