Skip to content

Add initial support for TypeVarLike default parameters (PEP 696) #8821

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

Merged
merged 6 commits into from
Oct 6, 2022
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
93 changes: 67 additions & 26 deletions stdlib/typing_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ 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 _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,
Expand All @@ -27,13 +28,13 @@ from typing import ( # noqa: Y022,Y027,Y039
Sequence,
Text as Text,
Type as Type,
TypeVar,
_Alias,
overload as overload,
type_check_only,
)

__all__ = [
"Any",
"ClassVar",
"Concatenate",
"Final",
Expand All @@ -43,6 +44,7 @@ __all__ = [
"ParamSpecKwargs",
"Self",
"Type",
"TypeVar",
"TypeVarTuple",
"Unpack",
"Awaitable",
Expand Down Expand Up @@ -70,6 +72,7 @@ __all__ = [
"Literal",
"NewType",
"overload",
"override",
"Protocol",
"reveal_type",
"runtime",
Expand All @@ -89,9 +92,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:
Expand Down Expand Up @@ -167,7 +170,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,
Expand All @@ -183,18 +185,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
Expand All @@ -210,7 +200,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,
Expand All @@ -233,12 +222,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 = ...,
Expand Down Expand Up @@ -268,3 +251,61 @@ else:
def _asdict(self) -> collections.OrderedDict[str, Any]: ...

def _replace(self: _typeshed.Self, **kwargs: Any) -> _typeshed.Self: ...

# 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
__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 = ...,
infer_variance: bool = ...,
) -> 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]

def override(__arg: _F) -> _F: ...