Skip to content

Commit 8e2a19f

Browse files
typing: Use PEP 695 syntax in typing.py (python#104553)
Co-authored-by: Alex Waygood <[email protected]>
1 parent 9792ae0 commit 8e2a19f

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Lib/typing.py

+17-11
Original file line numberDiff line numberDiff line change
@@ -2471,8 +2471,9 @@ class Other(Leaf): # Error reported by type checker
24712471
return f
24722472

24732473

2474-
# Some unconstrained type variables. These are used by the container types.
2475-
# (These are not for export.)
2474+
# Some unconstrained type variables. These were initially used by the container types.
2475+
# They were never meant for export and are now unused, but we keep them around to
2476+
# avoid breaking compatibility with users who import them.
24762477
T = TypeVar('T') # Any type.
24772478
KT = TypeVar('KT') # Key type.
24782479
VT = TypeVar('VT') # Value type.
@@ -2577,8 +2578,6 @@ def new_user(user_class: Type[U]) -> U:
25772578
At this point the type checker knows that joe has type BasicUser.
25782579
"""
25792580

2580-
# Internal type variable for callables. Not for export.
2581-
F = TypeVar("F", bound=Callable[..., Any])
25822581

25832582
@runtime_checkable
25842583
class SupportsInt(Protocol):
@@ -2631,22 +2630,22 @@ def __index__(self) -> int:
26312630

26322631

26332632
@runtime_checkable
2634-
class SupportsAbs(Protocol[T_co]):
2633+
class SupportsAbs[T](Protocol):
26352634
"""An ABC with one abstract method __abs__ that is covariant in its return type."""
26362635
__slots__ = ()
26372636

26382637
@abstractmethod
2639-
def __abs__(self) -> T_co:
2638+
def __abs__(self) -> T:
26402639
pass
26412640

26422641

26432642
@runtime_checkable
2644-
class SupportsRound(Protocol[T_co]):
2643+
class SupportsRound[T](Protocol):
26452644
"""An ABC with one abstract method __round__ that is covariant in its return type."""
26462645
__slots__ = ()
26472646

26482647
@abstractmethod
2649-
def __round__(self, ndigits: int = 0) -> T_co:
2648+
def __round__(self, ndigits: int = 0) -> T:
26502649
pass
26512650

26522651

@@ -3183,7 +3182,7 @@ class re(metaclass=_DeprecatedType):
31833182
sys.modules[re.__name__] = re
31843183

31853184

3186-
def reveal_type(obj: T, /) -> T:
3185+
def reveal_type[T](obj: T, /) -> T:
31873186
"""Reveal the inferred type of a variable.
31883187
31893188
When a static type checker encounters a call to ``reveal_type()``,
@@ -3203,6 +3202,11 @@ def reveal_type(obj: T, /) -> T:
32033202
return obj
32043203

32053204

3205+
class _IdentityCallable(Protocol):
3206+
def __call__[T](self, arg: T, /) -> T:
3207+
...
3208+
3209+
32063210
def dataclass_transform(
32073211
*,
32083212
eq_default: bool = True,
@@ -3211,7 +3215,7 @@ def dataclass_transform(
32113215
frozen_default: bool = False,
32123216
field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (),
32133217
**kwargs: Any,
3214-
) -> Callable[[T], T]:
3218+
) -> _IdentityCallable:
32153219
"""Decorator that marks a function, class, or metaclass as providing
32163220
dataclass-like behavior.
32173221
@@ -3288,8 +3292,10 @@ def decorator(cls_or_fn):
32883292
return decorator
32893293

32903294

3295+
type _Func = Callable[..., Any]
3296+
32913297

3292-
def override(method: F, /) -> F:
3298+
def override[F: _Func](method: F, /) -> F:
32933299
"""Indicate that a method is intended to override a method in a base class.
32943300
32953301
Usage:

0 commit comments

Comments
 (0)