3.14: add annotationlib, update typing and inspect (#13985)

This commit is contained in:
Jelle Zijlstra
2025-05-10 14:25:06 -07:00
committed by GitHub
parent 8bd5455b32
commit 5ff32f377c
10 changed files with 488 additions and 477 deletions
+142 -82
View File
@@ -6,7 +6,7 @@ import collections # noqa: F401 # pyright: ignore[reportUnusedImport]
import sys
import typing_extensions
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import IdentityFunction, ReadableBuffer, SupportsKeysAndGetItem
from _typeshed import AnnotationForm, IdentityFunction, ReadableBuffer, SupportsKeysAndGetItem
from abc import ABCMeta, abstractmethod
from re import Match as Match, Pattern as Pattern
from types import (
@@ -23,6 +23,11 @@ from types import (
)
from typing_extensions import Never as _Never, ParamSpec as _ParamSpec, deprecated
if sys.version_info >= (3, 14):
from _typeshed import EvaluateFunc
from annotationlib import Format
if sys.version_info >= (3, 10):
from types import UnionType
@@ -108,6 +113,9 @@ __all__ = [
if sys.version_info < (3, 14):
__all__ += ["ByteString"]
if sys.version_info >= (3, 14):
__all__ += ["evaluate_forward_ref"]
if sys.version_info >= (3, 10):
__all__ += ["Concatenate", "ParamSpec", "ParamSpecArgs", "ParamSpecKwargs", "TypeAlias", "TypeGuard", "is_typeddict"]
@@ -143,9 +151,9 @@ class TypeVar:
@property
def __name__(self) -> str: ...
@property
def __bound__(self) -> Any | None: ...
def __bound__(self) -> AnnotationForm | None: ...
@property
def __constraints__(self) -> tuple[Any, ...]: ...
def __constraints__(self) -> tuple[AnnotationForm, ...]: ...
@property
def __covariant__(self) -> bool: ...
@property
@@ -155,44 +163,61 @@ class TypeVar:
def __infer_variance__(self) -> bool: ...
if sys.version_info >= (3, 13):
@property
def __default__(self) -> Any: ...
def __default__(self) -> AnnotationForm: ...
if sys.version_info >= (3, 13):
def __new__(
cls,
name: str,
*constraints: Any,
bound: Any | None = None,
*constraints: AnnotationForm,
bound: AnnotationForm | None = None,
contravariant: bool = False,
covariant: bool = False,
infer_variance: bool = False,
default: Any = ...,
default: AnnotationForm = ...,
) -> Self: ...
elif sys.version_info >= (3, 12):
def __new__(
cls,
name: str,
*constraints: Any,
bound: Any | None = None,
*constraints: AnnotationForm,
bound: AnnotationForm | None = None,
covariant: bool = False,
contravariant: bool = False,
infer_variance: bool = False,
) -> Self: ...
elif sys.version_info >= (3, 11):
def __new__(
cls, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False
cls,
name: str,
*constraints: AnnotationForm,
bound: AnnotationForm | None = None,
covariant: bool = False,
contravariant: bool = False,
) -> Self: ...
else:
def __init__(
self, name: str, *constraints: Any, bound: Any | None = None, covariant: bool = False, contravariant: bool = False
self,
name: str,
*constraints: AnnotationForm,
bound: AnnotationForm | None = None,
covariant: bool = False,
contravariant: bool = False,
) -> None: ...
if sys.version_info >= (3, 10):
def __or__(self, right: Any) -> _SpecialForm: ...
def __ror__(self, left: Any) -> _SpecialForm: ...
def __or__(self, right: AnnotationForm) -> _SpecialForm: ...
def __ror__(self, left: AnnotationForm) -> _SpecialForm: ...
if sys.version_info >= (3, 11):
def __typing_subst__(self, arg: Any) -> Any: ...
if sys.version_info >= (3, 13):
def __typing_prepare_subst__(self, alias: Any, args: Any) -> tuple[Any, ...]: ...
def has_default(self) -> bool: ...
if sys.version_info >= (3, 14):
@property
def evaluate_bound(self) -> EvaluateFunc | None: ...
@property
def evaluate_constraints(self) -> EvaluateFunc | None: ...
@property
def evaluate_default(self) -> EvaluateFunc | None: ...
# Used for an undocumented mypy feature. Does not exist at runtime.
_promote = object()
@@ -234,10 +259,10 @@ if sys.version_info >= (3, 11):
def __name__(self) -> str: ...
if sys.version_info >= (3, 13):
@property
def __default__(self) -> Any: ...
def __default__(self) -> AnnotationForm: ...
def has_default(self) -> bool: ...
if sys.version_info >= (3, 13):
def __new__(cls, name: str, *, default: Any = ...) -> Self: ...
def __new__(cls, name: str, *, default: AnnotationForm = ...) -> Self: ...
elif sys.version_info >= (3, 12):
def __new__(cls, name: str) -> Self: ...
else:
@@ -246,6 +271,9 @@ if sys.version_info >= (3, 11):
def __iter__(self) -> Any: ...
def __typing_subst__(self, arg: Never) -> Never: ...
def __typing_prepare_subst__(self, alias: Any, args: Any) -> tuple[Any, ...]: ...
if sys.version_info >= (3, 14):
@property
def evaluate_default(self) -> EvaluateFunc | None: ...
if sys.version_info >= (3, 10):
@final
@@ -277,7 +305,7 @@ if sys.version_info >= (3, 10):
@property
def __name__(self) -> str: ...
@property
def __bound__(self) -> Any | None: ...
def __bound__(self) -> AnnotationForm | None: ...
@property
def __covariant__(self) -> bool: ...
@property
@@ -287,35 +315,35 @@ if sys.version_info >= (3, 10):
def __infer_variance__(self) -> bool: ...
if sys.version_info >= (3, 13):
@property
def __default__(self) -> Any: ...
def __default__(self) -> AnnotationForm: ...
if sys.version_info >= (3, 13):
def __new__(
cls,
name: str,
*,
bound: Any | None = None,
bound: AnnotationForm | None = None,
contravariant: bool = False,
covariant: bool = False,
infer_variance: bool = False,
default: Any = ...,
default: AnnotationForm = ...,
) -> Self: ...
elif sys.version_info >= (3, 12):
def __new__(
cls,
name: str,
*,
bound: Any | None = None,
bound: AnnotationForm | None = None,
contravariant: bool = False,
covariant: bool = False,
infer_variance: bool = False,
) -> Self: ...
elif sys.version_info >= (3, 11):
def __new__(
cls, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False
cls, name: str, *, bound: AnnotationForm | None = None, contravariant: bool = False, covariant: bool = False
) -> Self: ...
else:
def __init__(
self, name: str, *, bound: Any | None = None, contravariant: bool = False, covariant: bool = False
self, name: str, *, bound: AnnotationForm | None = None, contravariant: bool = False, covariant: bool = False
) -> None: ...
@property
@@ -330,13 +358,16 @@ if sys.version_info >= (3, 10):
def __ror__(self, left: Any) -> _SpecialForm: ...
if sys.version_info >= (3, 13):
def has_default(self) -> bool: ...
if sys.version_info >= (3, 14):
@property
def evaluate_default(self) -> EvaluateFunc | None: ...
Concatenate: _SpecialForm
TypeAlias: _SpecialForm
TypeGuard: _SpecialForm
class NewType:
def __init__(self, name: str, tp: Any) -> None: ...
def __init__(self, name: str, tp: AnnotationForm) -> None: ...
if sys.version_info >= (3, 11):
@staticmethod
def __call__(x: _T, /) -> _T: ...
@@ -860,13 +891,25 @@ _get_type_hints_obj_allowed_types: typing_extensions.TypeAlias = ( # noqa: Y042
| MethodDescriptorType
)
def get_type_hints(
obj: _get_type_hints_obj_allowed_types,
globalns: dict[str, Any] | None = None,
localns: Mapping[str, Any] | None = None,
include_extras: bool = False,
) -> dict[str, Any]: ...
def get_args(tp: Any) -> tuple[Any, ...]: ...
if sys.version_info >= (3, 14):
def get_type_hints(
obj: _get_type_hints_obj_allowed_types,
globalns: dict[str, Any] | None = None,
localns: Mapping[str, Any] | None = None,
include_extras: bool = False,
*,
format: Format | None = None,
) -> dict[str, AnnotationForm]: ...
else:
def get_type_hints(
obj: _get_type_hints_obj_allowed_types,
globalns: dict[str, Any] | None = None,
localns: Mapping[str, Any] | None = None,
include_extras: bool = False,
) -> dict[str, AnnotationForm]: ...
def get_args(tp: AnnotationForm) -> tuple[AnnotationForm, ...]: ...
if sys.version_info >= (3, 10):
@overload
@@ -877,7 +920,7 @@ if sys.version_info >= (3, 10):
@overload
def get_origin(tp: GenericAlias) -> type: ...
@overload
def get_origin(tp: Any) -> Any | None: ...
def get_origin(tp: AnnotationForm) -> AnnotationForm | None: ...
@overload
def cast(typ: type[_T], val: Any) -> _T: ...
@overload
@@ -888,7 +931,7 @@ def cast(typ: object, val: Any) -> Any: ...
if sys.version_info >= (3, 11):
def reveal_type(obj: _T, /) -> _T: ...
def assert_never(arg: Never, /) -> Never: ...
def assert_type(val: _T, typ: Any, /) -> _T: ...
def assert_type(val: _T, typ: AnnotationForm, /) -> _T: ...
def clear_overloads() -> None: ...
def get_overloads(func: Callable[..., object]) -> Sequence[Callable[..., object]]: ...
def dataclass_transform(
@@ -962,56 +1005,70 @@ class _TypedDict(Mapping[str, object], metaclass=ABCMeta):
# supposedly incompatible definitions of __or__ and __ior__
def __ior__(self, value: typing_extensions.Self, /) -> typing_extensions.Self: ... # type: ignore[misc]
@final
class ForwardRef(_Final):
__forward_arg__: str
__forward_code__: CodeType
__forward_evaluated__: bool
__forward_value__: Any | None
__forward_is_argument__: bool
__forward_is_class__: bool
__forward_module__: Any | None
if sys.version_info >= (3, 14):
from annotationlib import ForwardRef as ForwardRef
def __init__(self, arg: str, is_argument: bool = True, module: Any | None = None, *, is_class: bool = False) -> None: ...
def evaluate_forward_ref(
forward_ref: ForwardRef,
*,
owner: object = None,
globals: dict[str, Any] | None = None,
locals: Mapping[str, Any] | None = None,
type_params: tuple[TypeVar, ParamSpec, TypeVarTuple] | None = None,
format: Format | None = None,
) -> AnnotationForm: ...
if sys.version_info >= (3, 13):
@overload
@deprecated(
"Failing to pass a value to the 'type_params' parameter of ForwardRef._evaluate() is deprecated, "
"as it leads to incorrect behaviour when evaluating a stringified annotation "
"that references a PEP 695 type parameter. It will be disallowed in Python 3.15."
)
def _evaluate(
self, globalns: dict[str, Any] | None, localns: Mapping[str, Any] | None, *, recursive_guard: frozenset[str]
) -> Any | None: ...
@overload
def _evaluate(
self,
globalns: dict[str, Any] | None,
localns: Mapping[str, Any] | None,
type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...],
*,
recursive_guard: frozenset[str],
) -> Any | None: ...
elif sys.version_info >= (3, 12):
def _evaluate(
self,
globalns: dict[str, Any] | None,
localns: Mapping[str, Any] | None,
type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] | None = None,
*,
recursive_guard: frozenset[str],
) -> Any | None: ...
else:
def _evaluate(
self, globalns: dict[str, Any] | None, localns: Mapping[str, Any] | None, recursive_guard: frozenset[str]
) -> Any | None: ...
else:
@final
class ForwardRef(_Final):
__forward_arg__: str
__forward_code__: CodeType
__forward_evaluated__: bool
__forward_value__: AnnotationForm | None
__forward_is_argument__: bool
__forward_is_class__: bool
__forward_module__: Any | None
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 11):
def __or__(self, other: Any) -> _SpecialForm: ...
def __ror__(self, other: Any) -> _SpecialForm: ...
def __init__(self, arg: str, is_argument: bool = True, module: Any | None = None, *, is_class: bool = False) -> None: ...
if sys.version_info >= (3, 13):
@overload
@deprecated(
"Failing to pass a value to the 'type_params' parameter of ForwardRef._evaluate() is deprecated, "
"as it leads to incorrect behaviour when evaluating a stringified annotation "
"that references a PEP 695 type parameter. It will be disallowed in Python 3.15."
)
def _evaluate(
self, globalns: dict[str, Any] | None, localns: Mapping[str, Any] | None, *, recursive_guard: frozenset[str]
) -> AnnotationForm | None: ...
@overload
def _evaluate(
self,
globalns: dict[str, Any] | None,
localns: Mapping[str, Any] | None,
type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...],
*,
recursive_guard: frozenset[str],
) -> AnnotationForm | None: ...
elif sys.version_info >= (3, 12):
def _evaluate(
self,
globalns: dict[str, Any] | None,
localns: Mapping[str, Any] | None,
type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] | None = None,
*,
recursive_guard: frozenset[str],
) -> AnnotationForm | None: ...
else:
def _evaluate(
self, globalns: dict[str, Any] | None, localns: Mapping[str, Any] | None, recursive_guard: frozenset[str]
) -> AnnotationForm | None: ...
def __eq__(self, other: object) -> bool: ...
def __hash__(self) -> int: ...
if sys.version_info >= (3, 11):
def __or__(self, other: Any) -> _SpecialForm: ...
def __ror__(self, other: Any) -> _SpecialForm: ...
if sys.version_info >= (3, 10):
def is_typeddict(tp: object) -> bool: ...
@@ -1024,19 +1081,22 @@ if sys.version_info >= (3, 12):
class TypeAliasType:
def __new__(cls, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = ()) -> Self: ...
@property
def __value__(self) -> Any: ...
def __value__(self) -> AnnotationForm: ...
@property
def __type_params__(self) -> tuple[TypeVar | ParamSpec | TypeVarTuple, ...]: ...
@property
def __parameters__(self) -> tuple[Any, ...]: ...
def __parameters__(self) -> tuple[AnnotationForm, ...]: ...
@property
def __name__(self) -> str: ...
# It's writable on types, but not on instances of TypeAliasType.
@property
def __module__(self) -> str | None: ... # type: ignore[override]
def __getitem__(self, parameters: Any) -> GenericAlias: ...
def __getitem__(self, parameters: AnnotationForm) -> GenericAlias: ...
def __or__(self, right: Any) -> _SpecialForm: ...
def __ror__(self, left: Any) -> _SpecialForm: ...
if sys.version_info >= (3, 14):
@property
def evaluate_value(self) -> EvaluateFunc: ...
if sys.version_info >= (3, 13):
def is_protocol(tp: type, /) -> bool: ...