From e1ff41381def68b4a13d61958e14cf85b71e4597 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 16 May 2023 07:38:07 -0700 Subject: [PATCH 1/3] typing: Use PEP 695 syntax in typing.py --- Lib/typing.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 8d132e2cbf8771..24bc56c03208b2 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2471,8 +2471,9 @@ class Other(Leaf): # Error reported by type checker return f -# Some unconstrained type variables. These are used by the container types. -# (These are not for export.) +# Some unconstrained type variables. These were initially used by the container types. +# They were never meant for export and are now unused, but we keep them around to +# avoid breaking compatibility with users who import them. T = TypeVar('T') # Any type. KT = TypeVar('KT') # Key type. VT = TypeVar('VT') # Value type. @@ -2577,8 +2578,6 @@ def new_user(user_class: Type[U]) -> U: At this point the type checker knows that joe has type BasicUser. """ -# Internal type variable for callables. Not for export. -F = TypeVar("F", bound=Callable[..., Any]) @runtime_checkable class SupportsInt(Protocol): @@ -2631,7 +2630,7 @@ def __index__(self) -> int: @runtime_checkable -class SupportsAbs(Protocol[T_co]): +class SupportsAbs[T_co](Protocol): """An ABC with one abstract method __abs__ that is covariant in its return type.""" __slots__ = () @@ -2641,7 +2640,7 @@ def __abs__(self) -> T_co: @runtime_checkable -class SupportsRound(Protocol[T_co]): +class SupportsRound[T_co](Protocol): """An ABC with one abstract method __round__ that is covariant in its return type.""" __slots__ = () @@ -3183,7 +3182,7 @@ class re(metaclass=_DeprecatedType): sys.modules[re.__name__] = re -def reveal_type(obj: T, /) -> T: +def reveal_type[T](obj: T, /) -> T: """Reveal the inferred type of a variable. When a static type checker encounters a call to ``reveal_type()``, @@ -3203,7 +3202,7 @@ def reveal_type(obj: T, /) -> T: return obj -def dataclass_transform( +def dataclass_transform[T]( *, eq_default: bool = True, order_default: bool = False, @@ -3288,8 +3287,7 @@ def decorator(cls_or_fn): return decorator - -def override(method: F, /) -> F: +def override[F: Callable[..., Any]](method: F, /) -> F: """Indicate that a method is intended to override a method in a base class. Usage: From 16b09782f22be7b4d5c71898d3ed9a50bc380f86 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 16 May 2023 09:02:10 -0700 Subject: [PATCH 2/3] Use an alias, remove unnecessary _co suffix --- Lib/typing.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 24bc56c03208b2..4e51ee894d3ef4 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2630,22 +2630,22 @@ def __index__(self) -> int: @runtime_checkable -class SupportsAbs[T_co](Protocol): +class SupportsAbs[T](Protocol): """An ABC with one abstract method __abs__ that is covariant in its return type.""" __slots__ = () @abstractmethod - def __abs__(self) -> T_co: + def __abs__(self) -> T: pass @runtime_checkable -class SupportsRound[T_co](Protocol): +class SupportsRound[T](Protocol): """An ABC with one abstract method __round__ that is covariant in its return type.""" __slots__ = () @abstractmethod - def __round__(self, ndigits: int = 0) -> T_co: + def __round__(self, ndigits: int = 0) -> T: pass @@ -3287,7 +3287,10 @@ def decorator(cls_or_fn): return decorator -def override[F: Callable[..., Any]](method: F, /) -> F: +type _Func = Callable[..., Any] + + +def override[F: _Func](method: F, /) -> F: """Indicate that a method is intended to override a method in a base class. Usage: From 754dda03878798947b615d60fae57b6deb86799e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 16 May 2023 18:05:01 -0700 Subject: [PATCH 3/3] Better type for dataclass_transform --- Lib/typing.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 454d07f1158bd4..82107300734a7b 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3202,7 +3202,12 @@ def reveal_type[T](obj: T, /) -> T: return obj -def dataclass_transform[T]( +class _IdentityCallable(Protocol): + def __call__[T](self, arg: T, /) -> T: + ... + + +def dataclass_transform( *, eq_default: bool = True, order_default: bool = False, @@ -3210,7 +3215,7 @@ def dataclass_transform[T]( frozen_default: bool = False, field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (), **kwargs: Any, -) -> Callable[[T], T]: +) -> _IdentityCallable: """Decorator that marks a function, class, or metaclass as providing dataclass-like behavior.