summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-05-17 06:03:58 -0700
committerGitHub <noreply@github.com>2023-05-17 06:03:58 -0700
commit0cb2fdc6217aa7c04b5c798cfd195c8d0f4af353 (patch)
tree2a8879b22fa7d0bebc6c131b72faa443ef10e5a1
parent2f630e1ce18ad2e07428296532a68b11dc66ad10 (diff)
downloadcpython-git-0cb2fdc6217aa7c04b5c798cfd195c8d0f4af353.tar.gz
typing: Use PEP 695 syntax in typing.py (#104553)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
-rw-r--r--Lib/typing.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 50a8f51594..8210730073 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -2471,8 +2471,9 @@ def final(f):
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 @@ Type.__doc__ = \
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,22 +2630,22 @@ class SupportsIndex(Protocol):
@runtime_checkable
-class SupportsAbs(Protocol[T_co]):
+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(Protocol[T_co]):
+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
@@ -3183,7 +3182,7 @@ re.__name__ = __name__ + '.re'
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,6 +3202,11 @@ def reveal_type(obj: T, /) -> T:
return obj
+class _IdentityCallable(Protocol):
+ def __call__[T](self, arg: T, /) -> T:
+ ...
+
+
def dataclass_transform(
*,
eq_default: bool = True,
@@ -3211,7 +3215,7 @@ def dataclass_transform(
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.
@@ -3288,8 +3292,10 @@ def dataclass_transform(
return decorator
+type _Func = Callable[..., Any]
+
-def override(method: F, /) -> F:
+def override[F: _Func](method: F, /) -> F:
"""Indicate that a method is intended to override a method in a base class.
Usage: