Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing: Use PEP 695 syntax in typing.py #104553

Merged
merged 5 commits into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was added as part of implementing override earlier in 3.12, so there's no need to keep it around for compatibility.


@runtime_checkable
class SupportsInt(Protocol):
Expand Down Expand Up @@ -2631,22 +2630,22 @@ def __index__(self) -> int:


@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


Expand Down Expand Up @@ -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()``,
Expand All @@ -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,
Expand All @@ -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.

Expand Down Expand Up @@ -3288,8 +3292,10 @@ def decorator(cls_or_fn):
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:
Expand Down