diff --git a/stdlib/@tests/stubtest_allowlists/py314.txt b/stdlib/@tests/stubtest_allowlists/py314.txt index 5f2563a34789..8c04677c9564 100644 --- a/stdlib/@tests/stubtest_allowlists/py314.txt +++ b/stdlib/@tests/stubtest_allowlists/py314.txt @@ -2,117 +2,6 @@ # TODO: New errors in Python 3.14 that need to be fixed or moved below # ==================================================================== -_ast.Add.__annotations_cache__ -_ast.And.__annotations_cache__ -_ast.AnnAssign.__annotations_cache__ -_ast.Assert.__annotations_cache__ -_ast.Assign.__annotations_cache__ -_ast.AsyncFor.__annotations_cache__ -_ast.AsyncFunctionDef.__annotations_cache__ -_ast.AsyncWith.__annotations_cache__ -_ast.Attribute.__annotations_cache__ -_ast.AugAssign.__annotations_cache__ -_ast.Await.__annotations_cache__ -_ast.BinOp.__annotations_cache__ -_ast.BitAnd.__annotations_cache__ -_ast.BitOr.__annotations_cache__ -_ast.BitXor.__annotations_cache__ -_ast.BoolOp.__annotations_cache__ -_ast.Break.__annotations_cache__ -_ast.Call.__annotations_cache__ -_ast.ClassDef.__annotations_cache__ -_ast.Compare.__annotations_cache__ -_ast.Constant.__annotations_cache__ -_ast.Continue.__annotations_cache__ -_ast.Del.__annotations_cache__ -_ast.Delete.__annotations_cache__ -_ast.Dict.__annotations_cache__ -_ast.DictComp.__annotations_cache__ -_ast.Div.__annotations_cache__ -_ast.Eq.__annotations_cache__ -_ast.ExceptHandler.__annotations_cache__ -_ast.Expr.__annotations_cache__ -_ast.Expression.__annotations_cache__ -_ast.FloorDiv.__annotations_cache__ -_ast.For.__annotations_cache__ -_ast.FormattedValue.__annotations_cache__ -_ast.FunctionDef.__annotations_cache__ -_ast.FunctionType.__annotations_cache__ -_ast.GeneratorExp.__annotations_cache__ -_ast.Global.__annotations_cache__ -_ast.Gt.__annotations_cache__ -_ast.GtE.__annotations_cache__ -_ast.If.__annotations_cache__ -_ast.IfExp.__annotations_cache__ -_ast.Import.__annotations_cache__ -_ast.ImportFrom.__annotations_cache__ -_ast.In.__annotations_cache__ -_ast.Interactive.__annotations_cache__ -_ast.Invert.__annotations_cache__ -_ast.Is.__annotations_cache__ -_ast.IsNot.__annotations_cache__ -_ast.JoinedStr.__annotations_cache__ -_ast.LShift.__annotations_cache__ -_ast.Lambda.__annotations_cache__ -_ast.List.__annotations_cache__ -_ast.ListComp.__annotations_cache__ -_ast.Load.__annotations_cache__ -_ast.Lt.__annotations_cache__ -_ast.LtE.__annotations_cache__ -_ast.MatMult.__annotations_cache__ -_ast.Match.__annotations_cache__ -_ast.MatchAs.__annotations_cache__ -_ast.MatchClass.__annotations_cache__ -_ast.MatchMapping.__annotations_cache__ -_ast.MatchOr.__annotations_cache__ -_ast.MatchSequence.__annotations_cache__ -_ast.MatchSingleton.__annotations_cache__ -_ast.MatchStar.__annotations_cache__ -_ast.MatchValue.__annotations_cache__ -_ast.Mod.__annotations_cache__ -_ast.Module.__annotations_cache__ -_ast.Mult.__annotations_cache__ -_ast.Name.__annotations_cache__ -_ast.NamedExpr.__annotations_cache__ -_ast.Nonlocal.__annotations_cache__ -_ast.Not.__annotations_cache__ -_ast.NotEq.__annotations_cache__ -_ast.NotIn.__annotations_cache__ -_ast.Or.__annotations_cache__ -_ast.ParamSpec.__annotations_cache__ -_ast.Pass.__annotations_cache__ -_ast.Pow.__annotations_cache__ -_ast.RShift.__annotations_cache__ -_ast.Raise.__annotations_cache__ -_ast.Return.__annotations_cache__ -_ast.Set.__annotations_cache__ -_ast.SetComp.__annotations_cache__ -_ast.Slice.__annotations_cache__ -_ast.Starred.__annotations_cache__ -_ast.Store.__annotations_cache__ -_ast.Sub.__annotations_cache__ -_ast.Subscript.__annotations_cache__ -_ast.Try.__annotations_cache__ -_ast.TryStar.__annotations_cache__ -_ast.Tuple.__annotations_cache__ -_ast.TypeAlias.__annotations_cache__ -_ast.TypeIgnore.__annotations_cache__ -_ast.TypeVar.__annotations_cache__ -_ast.TypeVarTuple.__annotations_cache__ -_ast.UAdd.__annotations_cache__ -_ast.USub.__annotations_cache__ -_ast.UnaryOp.__annotations_cache__ -_ast.While.__annotations_cache__ -_ast.With.__annotations_cache__ -_ast.Yield.__annotations_cache__ -_ast.YieldFrom.__annotations_cache__ -_ast.alias.__annotations_cache__ -_ast.arg.__annotations_cache__ -_ast.arguments.__annotations_cache__ -_ast.comprehension.__annotations_cache__ -_ast.keyword.__annotations_cache__ -_ast.match_case.__annotations_cache__ -_ast.withitem.__annotations_cache__ _asyncio.all_tasks _asyncio.future_add_to_awaited_by _asyncio.future_discard_from_awaited_by @@ -136,121 +25,9 @@ _socket.if_indextoname _ssl.HAS_PHA _thread.RLock.locked _thread.set_name -annotationlib argparse.HelpFormatter.__init__ -ast.Add.__annotations_cache__ -ast.And.__annotations_cache__ -ast.AnnAssign.__annotations_cache__ -ast.Assert.__annotations_cache__ -ast.Assign.__annotations_cache__ -ast.AsyncFor.__annotations_cache__ -ast.AsyncFunctionDef.__annotations_cache__ -ast.AsyncWith.__annotations_cache__ -ast.Attribute.__annotations_cache__ -ast.AugAssign.__annotations_cache__ -ast.Await.__annotations_cache__ -ast.BinOp.__annotations_cache__ -ast.BitAnd.__annotations_cache__ -ast.BitOr.__annotations_cache__ -ast.BitXor.__annotations_cache__ -ast.BoolOp.__annotations_cache__ -ast.Break.__annotations_cache__ -ast.Call.__annotations_cache__ -ast.ClassDef.__annotations_cache__ -ast.Compare.__annotations_cache__ -ast.Constant.__annotations_cache__ -ast.Continue.__annotations_cache__ -ast.Del.__annotations_cache__ -ast.Delete.__annotations_cache__ -ast.Dict.__annotations_cache__ -ast.DictComp.__annotations_cache__ -ast.Div.__annotations_cache__ -ast.Eq.__annotations_cache__ -ast.ExceptHandler.__annotations_cache__ -ast.Expr.__annotations_cache__ -ast.Expression.__annotations_cache__ -ast.FloorDiv.__annotations_cache__ -ast.For.__annotations_cache__ -ast.FormattedValue.__annotations_cache__ -ast.FunctionDef.__annotations_cache__ -ast.FunctionType.__annotations_cache__ -ast.GeneratorExp.__annotations_cache__ -ast.Global.__annotations_cache__ -ast.Gt.__annotations_cache__ -ast.GtE.__annotations_cache__ -ast.If.__annotations_cache__ -ast.IfExp.__annotations_cache__ -ast.Import.__annotations_cache__ -ast.ImportFrom.__annotations_cache__ -ast.In.__annotations_cache__ -ast.Interactive.__annotations_cache__ ast.Interpolation -ast.Invert.__annotations_cache__ -ast.Is.__annotations_cache__ -ast.IsNot.__annotations_cache__ -ast.JoinedStr.__annotations_cache__ -ast.LShift.__annotations_cache__ -ast.Lambda.__annotations_cache__ -ast.List.__annotations_cache__ -ast.ListComp.__annotations_cache__ -ast.Load.__annotations_cache__ -ast.Lt.__annotations_cache__ -ast.LtE.__annotations_cache__ -ast.MatMult.__annotations_cache__ -ast.Match.__annotations_cache__ -ast.MatchAs.__annotations_cache__ -ast.MatchClass.__annotations_cache__ -ast.MatchMapping.__annotations_cache__ -ast.MatchOr.__annotations_cache__ -ast.MatchSequence.__annotations_cache__ -ast.MatchSingleton.__annotations_cache__ -ast.MatchStar.__annotations_cache__ -ast.MatchValue.__annotations_cache__ -ast.Mod.__annotations_cache__ -ast.Module.__annotations_cache__ -ast.Mult.__annotations_cache__ -ast.Name.__annotations_cache__ -ast.NamedExpr.__annotations_cache__ -ast.Nonlocal.__annotations_cache__ -ast.Not.__annotations_cache__ -ast.NotEq.__annotations_cache__ -ast.NotIn.__annotations_cache__ -ast.Or.__annotations_cache__ -ast.ParamSpec.__annotations_cache__ -ast.Pass.__annotations_cache__ -ast.Pow.__annotations_cache__ -ast.RShift.__annotations_cache__ -ast.Raise.__annotations_cache__ -ast.Return.__annotations_cache__ -ast.Set.__annotations_cache__ -ast.SetComp.__annotations_cache__ -ast.Slice.__annotations_cache__ -ast.Starred.__annotations_cache__ -ast.Store.__annotations_cache__ -ast.Sub.__annotations_cache__ -ast.Subscript.__annotations_cache__ ast.TemplateStr -ast.Try.__annotations_cache__ -ast.TryStar.__annotations_cache__ -ast.Tuple.__annotations_cache__ -ast.TypeAlias.__annotations_cache__ -ast.TypeIgnore.__annotations_cache__ -ast.TypeVar.__annotations_cache__ -ast.TypeVarTuple.__annotations_cache__ -ast.UAdd.__annotations_cache__ -ast.USub.__annotations_cache__ -ast.UnaryOp.__annotations_cache__ -ast.While.__annotations_cache__ -ast.With.__annotations_cache__ -ast.Yield.__annotations_cache__ -ast.YieldFrom.__annotations_cache__ -ast.alias.__annotations_cache__ -ast.arg.__annotations_cache__ -ast.arguments.__annotations_cache__ -ast.comprehension.__annotations_cache__ -ast.keyword.__annotations_cache__ -ast.match_case.__annotations_cache__ -ast.withitem.__annotations_cache__ asyncio.__all__ asyncio.FrameCallGraphEntry asyncio.FutureCallGraph @@ -293,7 +70,6 @@ builtins.int.__round__ builtins.memoryview.__class_getitem__ builtins.staticmethod.__annotate__ builtins.staticmethod.__class_getitem__ -builtins.type.__annotate__ code.compile_command codeop.compile_command compression @@ -345,8 +121,6 @@ dataclasses.Field.doc dataclasses.field dataclasses.make_dataclass decimal.Decimal.from_number -decimal.DecimalTuple.__annotate_func__ -decimal.DecimalTuple.__annotations_cache__ decimal.IEEE_CONTEXT_MAX_BITS dis.Bytecode.__init__ dis.Instruction.make @@ -384,25 +158,8 @@ importlib.abc.Traversable importlib.abc.TraversableResources importlib.machinery.__all__ importlib.machinery.AppleFrameworkLoader -importlib.metadata.PackageMetadata.__annotate_func__ -importlib.metadata.PackageMetadata.__annotations_cache__ -importlib.metadata._meta.PackageMetadata.__annotate_func__ -importlib.metadata._meta.PackageMetadata.__annotations_cache__ -importlib.metadata._meta.SimplePath.__annotate_func__ -importlib.metadata._meta.SimplePath.__annotations_cache__ -importlib.resources.abc.Traversable.__annotate_func__ -importlib.resources.abc.Traversable.__annotations_cache__ importlib.util.__all__ importlib.util.Loader -inspect.__all__ -inspect.CO_HAS_DOCSTRING -inspect.CO_METHOD -inspect.Signature.format -inspect.Signature.from_callable -inspect.formatannotation -inspect.get_annotations -inspect.ispackage -inspect.signature io.__all__ io.Reader io.Writer @@ -449,10 +206,6 @@ pdb.set_trace pkgutil.__all__ pkgutil.find_loader pkgutil.get_loader -pstats.FunctionProfile.__annotate_func__ -pstats.FunctionProfile.__annotations_cache__ -pstats.StatsProfile.__annotate_func__ -pstats.StatsProfile.__annotations_cache__ pyexpat.errors.XML_ERROR_NOT_STARTED shutil.__all__ socket.__all__ @@ -490,81 +243,6 @@ turtle.poly turtle.save types.CodeType.co_branches types.FrameType.f_generator -types.FunctionType.__annotate__ -types.LambdaType.__annotate__ -types.ModuleType.__annotate__ -types.UnionType.__class_getitem__ -types.UnionType.__mro_entries__ -types.UnionType.__name__ -types.UnionType.__qualname__ -typing.__all__ -typing.ForwardRef.__arg__ -typing.ForwardRef.__ast_node__ -typing.ForwardRef.__cell__ -typing.ForwardRef.__code__ -typing.ForwardRef.__extra_names__ -typing.ForwardRef.__forward_arg__ -typing.ForwardRef.__forward_code__ -typing.ForwardRef.__globals__ -typing.ForwardRef.__init__ -typing.ForwardRef.__init_subclass__ -typing.ForwardRef.__owner__ -typing.ForwardRef.__stringifier_dict__ -typing.ForwardRef.evaluate -typing.ParamSpec.evaluate_default -typing.SupportsAbs.__annotate_func__ -typing.SupportsAbs.__annotations_cache__ -typing.SupportsBytes.__annotate_func__ -typing.SupportsBytes.__annotations_cache__ -typing.SupportsComplex.__annotate_func__ -typing.SupportsComplex.__annotations_cache__ -typing.SupportsFloat.__annotate_func__ -typing.SupportsFloat.__annotations_cache__ -typing.SupportsIndex.__annotate_func__ -typing.SupportsIndex.__annotations_cache__ -typing.SupportsInt.__annotate_func__ -typing.SupportsInt.__annotations_cache__ -typing.SupportsRound.__annotate_func__ -typing.SupportsRound.__annotations_cache__ -typing.TypeAliasType.evaluate_value -typing.TypeVar.evaluate_bound -typing.TypeVar.evaluate_constraints -typing.TypeVar.evaluate_default -typing.TypeVarTuple.evaluate_default -typing.Union -typing.evaluate_forward_ref -typing.get_type_hints -typing_extensions.ForwardRef.__arg__ -typing_extensions.ForwardRef.__ast_node__ -typing_extensions.ForwardRef.__cell__ -typing_extensions.ForwardRef.__code__ -typing_extensions.ForwardRef.__extra_names__ -typing_extensions.ForwardRef.__forward_arg__ -typing_extensions.ForwardRef.__forward_code__ -typing_extensions.ForwardRef.__globals__ -typing_extensions.ForwardRef.__init__ -typing_extensions.ForwardRef.__init_subclass__ -typing_extensions.ForwardRef.__owner__ -typing_extensions.ForwardRef.__stringifier_dict__ -typing_extensions.ForwardRef.evaluate -typing_extensions.SupportsAbs.__annotate_func__ -typing_extensions.SupportsAbs.__annotations_cache__ -typing_extensions.SupportsBytes.__annotate_func__ -typing_extensions.SupportsBytes.__annotations_cache__ -typing_extensions.SupportsComplex.__annotate_func__ -typing_extensions.SupportsComplex.__annotations_cache__ -typing_extensions.SupportsFloat.__annotate_func__ -typing_extensions.SupportsFloat.__annotations_cache__ -typing_extensions.SupportsIndex.__annotate_func__ -typing_extensions.SupportsIndex.__annotations_cache__ -typing_extensions.SupportsInt.__annotate_func__ -typing_extensions.SupportsInt.__annotations_cache__ -typing_extensions.SupportsRound.__annotate_func__ -typing_extensions.SupportsRound.__annotations_cache__ -typing_extensions.TypeAliasType.evaluate_value -typing_extensions.Union -typing_extensions.evaluate_forward_ref -typing_extensions.get_type_hints unittest.TestCase.assertEndsWith unittest.TestCase.assertHasAttr unittest.TestCase.assertIsSubclass @@ -586,21 +264,10 @@ urllib.request.FancyURLopener urllib.request.URLopener urllib.request.pathname2url urllib.request.url2pathname -wsgiref.types.ErrorStream.__annotate_func__ -wsgiref.types.ErrorStream.__annotations_cache__ -wsgiref.types.FileWrapper.__annotate_func__ -wsgiref.types.FileWrapper.__annotations_cache__ -wsgiref.types.InputStream.__annotate_func__ -wsgiref.types.InputStream.__annotations_cache__ -wsgiref.types.StartResponse.__annotate_func__ -wsgiref.types.StartResponse.__annotations_cache__ -wsgiref.types._Readable.__annotate_func__ -wsgiref.types._Readable.__annotations_cache__ xml.parsers.expat.errors.XML_ERROR_NOT_STARTED xml.sax.__all__ xml.sax.InputSource zipfile.ZipFile.data_offset -zipfile._path.glob.Translator.__annotate_func__ # ========================= @@ -608,6 +275,14 @@ zipfile._path.glob.Translator.__annotate_func__ # ========================= +# Union and UnionType are aliases in 3.14 but type checkers need some changes +typing_extensions.Union +typing.Union +types.UnionType.__class_getitem__ +types.UnionType.__mro_entries__ +types.UnionType.__name__ +types.UnionType.__qualname__ + # ==================================== # Pre-existing errors from Python 3.13 # ==================================== @@ -660,6 +335,24 @@ typing(_extensions)?\.IO\.truncate typing(_extensions)?\.IO\.write typing(_extensions)?\.IO\.writelines +# ============================================================= +# Allowlist entries that cannot or should not be fixed; >= 3.14 +# ============================================================= + +# Internal annotations machinery +.*\.__annotate_func__ +.*\.__annotations_cache__ + +# Undocumented private attributes +.*\.ForwardRef\.__arg__ +.*\.ForwardRef\.__ast_node__ +.*\.ForwardRef\.__cell__ +.*\.ForwardRef\.__code__ +.*\.ForwardRef\.__extra_names__ +.*\.ForwardRef\.__globals__ +.*\.ForwardRef\.__init_subclass__ +.*\.ForwardRef\.__owner__ +.*\.ForwardRef\.__stringifier_dict__ # ============================================================= # Allowlist entries that cannot or should not be fixed; >= 3.13 diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index fec56ce59e36..717cf7b4d71a 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -78,6 +78,7 @@ _weakrefset: 3.0- _winapi: 3.3- abc: 3.0- aifc: 3.0-3.12 +annotationlib: 3.14- antigravity: 3.0- argparse: 3.0- array: 3.0- diff --git a/stdlib/_typeshed/__init__.pyi b/stdlib/_typeshed/__init__.pyi index 7ed8a079ea09..c37d55a7d9ec 100644 --- a/stdlib/_typeshed/__init__.pyi +++ b/stdlib/_typeshed/__init__.pyi @@ -367,3 +367,14 @@ else: from enum import Enum class StrEnum(str, Enum): ... + +# Objects that appear in annotations or in type expressions. +# Similar to PEP 747's TypeForm but a little broader. +AnnotationForm: TypeAlias = Any + +if sys.version_info >= (3, 14): + from annotationlib import Format + + # These return annotations, which can be arbitrary objects + AnnotateFunc: TypeAlias = Callable[[Format], dict[str, AnnotationForm]] + EvaluateFunc: TypeAlias = Callable[[Format], AnnotationForm] diff --git a/stdlib/annotationlib.pyi b/stdlib/annotationlib.pyi new file mode 100644 index 000000000000..7590c632d785 --- /dev/null +++ b/stdlib/annotationlib.pyi @@ -0,0 +1,132 @@ +import sys +from typing import Literal + +if sys.version_info >= (3, 14): + import enum + import types + from _typeshed import AnnotateFunc, AnnotationForm, EvaluateFunc, SupportsItems + from collections.abc import Mapping + from typing import Any, ParamSpec, TypeVar, TypeVarTuple, final, overload + from warnings import deprecated + + __all__ = [ + "Format", + "ForwardRef", + "call_annotate_function", + "call_evaluate_function", + "get_annotate_from_class_namespace", + "get_annotations", + "annotations_to_string", + "type_repr", + ] + + class Format(enum.IntEnum): + VALUE = 1 + VALUE_WITH_FAKE_GLOBALS = 2 + FORWARDREF = 3 + STRING = 4 + + @final + class ForwardRef: + __forward_is_argument__: bool + __forward_is_class__: bool + __forward_module__: str | None + def __init__( + self, arg: str, *, module: str | None = None, owner: object = None, is_argument: bool = True, is_class: bool = False + ) -> None: ... + @overload + def evaluate( + self, + *, + globals: dict[str, Any] | None = None, + locals: Mapping[str, Any] | None = None, + type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] | None = None, + owner: object = None, + format: Literal[Format.STRING], + ) -> str: ... + @overload + def evaluate( + self, + *, + globals: dict[str, Any] | None = None, + locals: Mapping[str, Any] | None = None, + type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] | None = None, + owner: object = None, + format: Literal[Format.FORWARDREF], + ) -> AnnotationForm | ForwardRef: ... + @overload + def evaluate( + self, + *, + globals: dict[str, Any] | None = None, + locals: Mapping[str, Any] | None = None, + type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] | None = None, + owner: object = None, + format: Format = Format.VALUE, # noqa: Y011 + ) -> AnnotationForm: ... + @deprecated("Use ForwardRef.evaluate() or typing.evaluate_forward_ref() instead.") + def _evaluate( + self, + globalns: dict[str, Any] | None, + localns: Mapping[str, Any] | None, + type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = ..., + *, + recursive_guard: frozenset[str], + ) -> AnnotationForm: ... + @property + def __forward_arg__(self) -> str: ... + @property + def __forward_code__(self) -> types.CodeType: ... + def __eq__(self, other: object) -> bool: ... + def __hash__(self) -> int: ... + def __or__(self, other: Any) -> types.UnionType: ... + def __ror__(self, other: Any) -> types.UnionType: ... + + @overload + def call_evaluate_function(evaluate: EvaluateFunc, format: Literal[Format.STRING], *, owner: object = None) -> str: ... + @overload + def call_evaluate_function( + evaluate: EvaluateFunc, format: Literal[Format.FORWARDREF], *, owner: object = None + ) -> AnnotationForm | ForwardRef: ... + @overload + def call_evaluate_function(evaluate: EvaluateFunc, format: Format, *, owner: object = None) -> AnnotationForm: ... + @overload + def call_annotate_function( + annotate: AnnotateFunc, format: Literal[Format.STRING], *, owner: object = None + ) -> dict[str, str]: ... + @overload + def call_annotate_function( + annotate: AnnotateFunc, format: Literal[Format.FORWARDREF], *, owner: object = None + ) -> dict[str, AnnotationForm | ForwardRef]: ... + @overload + def call_annotate_function(annotate: AnnotateFunc, format: Format, *, owner: object = None) -> dict[str, AnnotationForm]: ... + def get_annotate_from_class_namespace(obj: Mapping[str, object]) -> AnnotateFunc | None: ... + @overload + def get_annotations( + obj: Any, # any object with __annotations__ or __annotate__ + *, + globals: dict[str, object] | None = None, + locals: Mapping[str, object] | None = None, + eval_str: bool = False, + format: Literal[Format.STRING], + ) -> dict[str, str]: ... + @overload + def get_annotations( + obj: Any, + *, + globals: dict[str, object] | None = None, + locals: Mapping[str, object] | None = None, + eval_str: bool = False, + format: Literal[Format.FORWARDREF], + ) -> dict[str, AnnotationForm | ForwardRef]: ... + @overload + def get_annotations( + obj: Any, + *, + globals: dict[str, object] | None = None, + locals: Mapping[str, object] | None = None, + eval_str: bool = False, + format: Format = Format.VALUE, # noqa: Y011 + ) -> dict[str, AnnotationForm]: ... + def type_repr(value: object) -> str: ... + def annotations_to_string(annotations: SupportsItems[str, object]) -> dict[str, str]: ... diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 2091a76f8da3..5a1d4dd8afb9 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -5,6 +5,7 @@ import sys import types from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import ( + AnnotationForm, AnyStr_co, ConvertibleToFloat, ConvertibleToInt, @@ -72,6 +73,9 @@ from typing_extensions import ( # noqa: Y023 deprecated, ) +if sys.version_info >= (3, 14): + from _typeshed import AnnotateFunc + _T = TypeVar("_T") _I = TypeVar("_I", default=int) _T_co = TypeVar("_T_co", covariant=True) @@ -215,6 +219,9 @@ class type: def __ror__(self, value: Any, /) -> types.UnionType: ... if sys.version_info >= (3, 12): __type_params__: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] + __annotations__: dict[str, AnnotationForm] + if sys.version_info >= (3, 14): + __annotate__: AnnotateFunc | None class super: @overload @@ -1017,7 +1024,9 @@ class function: def __globals__(self) -> dict[str, Any]: ... __name__: str __qualname__: str - __annotations__: dict[str, Any] + __annotations__: dict[str, AnnotationForm] + if sys.version_info >= (3, 14): + __annotate__: AnnotateFunc | None __kwdefaults__: dict[str, Any] if sys.version_info >= (3, 10): @property diff --git a/stdlib/inspect.pyi b/stdlib/inspect.pyi index c525418c104b..e19c2a634aa0 100644 --- a/stdlib/inspect.pyi +++ b/stdlib/inspect.pyi @@ -2,7 +2,7 @@ import dis import enum import sys import types -from _typeshed import StrPath +from _typeshed import AnnotationForm, StrPath from collections import OrderedDict from collections.abc import AsyncGenerator, Awaitable, Callable, Coroutine, Generator, Mapping, Sequence, Set as AbstractSet from types import ( @@ -28,6 +28,9 @@ from types import ( from typing import Any, ClassVar, Final, Literal, NamedTuple, Protocol, TypeVar, overload from typing_extensions import ParamSpec, Self, TypeAlias, TypeGuard, TypeIs +if sys.version_info >= (3, 14): + from annotationlib import Format + if sys.version_info >= (3, 11): __all__ = [ "ArgInfo", @@ -139,6 +142,8 @@ if sys.version_info >= (3, 11): "getasyncgenstate", "BufferFlags", ] + if sys.version_info >= (3, 14): + __all__ += ["CO_HAS_DOCSTRING", "CO_METHOD", "ispackage"] _P = ParamSpec("_P") _T = TypeVar("_T") @@ -172,6 +177,9 @@ CO_COROUTINE: Final = 128 CO_ITERABLE_COROUTINE: Final = 256 CO_ASYNC_GENERATOR: Final = 512 TPFLAGS_IS_ABSTRACT: Final = 1048576 +if sys.version_info >= (3, 14): + CO_HAS_DOCSTRING: Final = 67108864 + CO_METHOD: Final = 134217728 modulesbyfile: dict[str, Any] @@ -199,6 +207,11 @@ def getmodulename(path: StrPath) -> str | None: ... def ismodule(object: object) -> TypeIs[ModuleType]: ... def isclass(object: object) -> TypeIs[type[Any]]: ... def ismethod(object: object) -> TypeIs[MethodType]: ... + +if sys.version_info >= (3, 14): + # Not TypeIs because it does not return True for all modules + def ispackage(object: object) -> TypeGuard[ModuleType]: ... + def isfunction(object: object) -> TypeIs[FunctionType]: ... if sys.version_info >= (3, 12): @@ -294,7 +307,18 @@ _IntrospectableCallable: TypeAlias = Callable[..., Any] # # Introspecting callables with the Signature object # -if sys.version_info >= (3, 10): +if sys.version_info >= (3, 14): + def signature( + obj: _IntrospectableCallable, + *, + follow_wrapped: bool = True, + globals: Mapping[str, Any] | None = None, + locals: Mapping[str, Any] | None = None, + eval_str: bool = False, + annotation_format: Format = Format.VALUE, # noqa: Y011 + ) -> Signature: ... + +elif sys.version_info >= (3, 10): def signature( obj: _IntrospectableCallable, *, @@ -323,7 +347,19 @@ class Signature: def bind_partial(self, *args: Any, **kwargs: Any) -> BoundArguments: ... def replace(self, *, parameters: Sequence[Parameter] | type[_void] | None = ..., return_annotation: Any = ...) -> Self: ... __replace__ = replace - if sys.version_info >= (3, 10): + if sys.version_info >= (3, 14): + @classmethod + def from_callable( + cls, + obj: _IntrospectableCallable, + *, + follow_wrapped: bool = True, + globals: Mapping[str, Any] | None = None, + locals: Mapping[str, Any] | None = None, + eval_str: bool = False, + annotation_format: Format = Format.VALUE, # noqa: Y011 + ) -> Self: ... + elif sys.version_info >= (3, 10): @classmethod def from_callable( cls, @@ -337,20 +373,24 @@ class Signature: else: @classmethod def from_callable(cls, obj: _IntrospectableCallable, *, follow_wrapped: bool = True) -> Self: ... - if sys.version_info >= (3, 13): + if sys.version_info >= (3, 14): + def format(self, *, max_width: int | None = None, quote_annotation_strings: bool = True) -> str: ... + elif sys.version_info >= (3, 13): def format(self, *, max_width: int | None = None) -> str: ... def __eq__(self, other: object) -> bool: ... def __hash__(self) -> int: ... -if sys.version_info >= (3, 10): +if sys.version_info >= (3, 14): + from annotationlib import get_annotations as get_annotations +elif sys.version_info >= (3, 10): def get_annotations( obj: Callable[..., object] | type[object] | ModuleType, # any callable, class, or module *, globals: Mapping[str, Any] | None = None, # value types depend on the key locals: Mapping[str, Any] | None = None, # value types depend on the key eval_str: bool = False, - ) -> dict[str, Any]: ... # values are type expressions + ) -> dict[str, AnnotationForm]: ... # values are type expressions # The name is the same as the enum's name in CPython class _ParameterKind(enum.IntEnum): @@ -461,7 +501,13 @@ class ArgInfo(NamedTuple): locals: dict[str, Any] def getargvalues(frame: FrameType) -> ArgInfo: ... -def formatannotation(annotation: object, base_module: str | None = None) -> str: ... + +if sys.version_info >= (3, 14): + def formatannotation(annotation: object, base_module: str | None = None, *, quote_annotation_strings: bool = True) -> str: ... + +else: + def formatannotation(annotation: object, base_module: str | None = None) -> str: ... + def formatannotationrelativeto(object: object) -> Callable[[object], str]: ... if sys.version_info < (3, 11): diff --git a/stdlib/types.pyi b/stdlib/types.pyi index fe443be27121..1163d71d2c95 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -1,5 +1,5 @@ import sys -from _typeshed import MaybeNone, SupportsKeysAndGetItem +from _typeshed import AnnotationForm, MaybeNone, SupportsKeysAndGetItem from _typeshed.importlib import LoaderProtocol from collections.abc import ( AsyncGenerator, @@ -19,6 +19,9 @@ from importlib.machinery import ModuleSpec from typing import Any, ClassVar, Literal, TypeVar, final, overload from typing_extensions import ParamSpec, Self, TypeAliasType, TypeVarTuple, deprecated +if sys.version_info >= (3, 14): + from _typeshed import AnnotateFunc + __all__ = [ "FunctionType", "LambdaType", @@ -77,7 +80,9 @@ class FunctionType: def __globals__(self) -> dict[str, Any]: ... __name__: str __qualname__: str - __annotations__: dict[str, Any] + __annotations__: dict[str, AnnotationForm] + if sys.version_info >= (3, 14): + __annotate__: AnnotateFunc | None __kwdefaults__: dict[str, Any] | None if sys.version_info >= (3, 10): @property @@ -352,6 +357,10 @@ class ModuleType: # Redeclaring `__doc__` here helps some type checkers understand that `__doc__` is available # as an implicit global in all modules, similar to `__name__`, `__file__`, `__spec__`, etc. __doc__: str | None + __annotations__: dict[str, AnnotationForm] + if sys.version_info >= (3, 14): + __annotate__: AnnotateFunc | None + def __init__(self, name: str, doc: str | None = ...) -> None: ... # __getattr__ doesn't exist at runtime, # but having it here in typeshed makes dynamic imports diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 189ff3e89720..6b6c2654d247 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -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: ... diff --git a/stdlib/typing_extensions.pyi b/stdlib/typing_extensions.pyi index bad5fae880c0..37f8e8ba6a4b 100644 --- a/stdlib/typing_extensions.pyi +++ b/stdlib/typing_extensions.pyi @@ -2,7 +2,7 @@ import abc import enum import sys from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, Incomplete, Unused +from _typeshed import AnnotationForm, IdentityFunction, Incomplete, Unused from collections.abc import ( AsyncGenerator as AsyncGenerator, AsyncIterable as AsyncIterable, @@ -241,7 +241,7 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): __mutable_keys__: ClassVar[frozenset[str]] # PEP 728 __closed__: ClassVar[bool] - __extra_items__: ClassVar[Any] + __extra_items__: ClassVar[AnnotationForm] def copy(self) -> Self: ... # Using Never so that only calls using mypy plugin hook that specialize the signature # can go through. @@ -267,13 +267,14 @@ class _TypedDict(Mapping[str, object], metaclass=abc.ABCMeta): OrderedDict = _Alias() -def get_type_hints( - obj: Callable[..., Any], - 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, 13): + from typing import get_type_hints as get_type_hints +else: + def get_type_hints( + obj: Any, 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 @@ -284,7 +285,7 @@ def get_origin(tp: GenericAlias) -> type: ... @overload def get_origin(tp: ParamSpecArgs | ParamSpecKwargs) -> ParamSpec: ... @overload -def get_origin(tp: Any) -> Any | None: ... +def get_origin(tp: AnnotationForm) -> AnnotationForm | None: ... Annotated: _SpecialForm _AnnotatedAlias: Any # undocumented @@ -340,7 +341,7 @@ else: Never: _SpecialForm 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]]: ... @@ -373,7 +374,7 @@ else: def _replace(self, **kwargs: Any) -> Self: ... class NewType: - def __init__(self, name: str, tp: Any) -> None: ... + def __init__(self, name: str, tp: AnnotationForm) -> None: ... def __call__(self, obj: _T, /) -> _T: ... __supertype__: type | NewType if sys.version_info >= (3, 10): @@ -480,9 +481,9 @@ else: @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 @@ -490,15 +491,15 @@ else: @property def __infer_variance__(self) -> bool: ... @property - def __default__(self) -> Any: ... + def __default__(self) -> AnnotationForm: ... def __init__( self, name: str, - *constraints: Any, - bound: Any | None = None, + *constraints: AnnotationForm, + bound: AnnotationForm | None = None, covariant: bool = False, contravariant: bool = False, - default: Any = ..., + default: AnnotationForm = ..., infer_variance: bool = False, ) -> None: ... def has_default(self) -> bool: ... @@ -514,7 +515,7 @@ else: @property def __name__(self) -> str: ... @property - def __bound__(self) -> Any | None: ... + def __bound__(self) -> AnnotationForm | None: ... @property def __covariant__(self) -> bool: ... @property @@ -522,15 +523,15 @@ else: @property def __infer_variance__(self) -> bool: ... @property - def __default__(self) -> Any: ... + def __default__(self) -> AnnotationForm: ... def __init__( self, name: str, *, - bound: None | type[Any] | str = None, + bound: None | AnnotationForm | str = None, contravariant: bool = False, covariant: bool = False, - default: Any = ..., + default: AnnotationForm = ..., ) -> None: ... @property def args(self) -> ParamSpecArgs: ... @@ -547,8 +548,8 @@ else: @property def __name__(self) -> str: ... @property - def __default__(self) -> Any: ... - def __init__(self, name: str, *, default: Any = ...) -> None: ... + def __default__(self) -> AnnotationForm: ... + def __init__(self, name: str, *, default: AnnotationForm = ...) -> None: ... def __iter__(self) -> Any: ... # Unpack[Self] def has_default(self) -> bool: ... def __typing_prepare_subst__(self, alias: Any, args: Any) -> tuple[Any, ...]: ... @@ -563,23 +564,23 @@ else: @final class TypeAliasType: def __init__( - self, name: str, value: Any, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = () - ) -> None: ... # value is a type expression + self, name: str, value: AnnotationForm, *, type_params: tuple[TypeVar | ParamSpec | TypeVarTuple, ...] = () + ) -> None: ... @property - def __value__(self) -> Any: ... # a type expression + def __value__(self) -> AnnotationForm: ... @property def __type_params__(self) -> tuple[TypeVar | ParamSpec | TypeVarTuple, ...]: ... @property # `__parameters__` can include special forms if a `TypeVarTuple` was # passed as a `type_params` element to the constructor method. - def __parameters__(self) -> tuple[TypeVar | ParamSpec | Any, ...]: ... + def __parameters__(self) -> tuple[TypeVar | ParamSpec | 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] # Returns typing._GenericAlias, which isn't stubbed. - def __getitem__(self, parameters: Incomplete | tuple[Incomplete, ...]) -> Any: ... + def __getitem__(self, parameters: Incomplete | tuple[Incomplete, ...]) -> AnnotationForm: ... def __init_subclass__(cls, *args: Unused, **kwargs: Unused) -> NoReturn: ... if sys.version_info >= (3, 10): def __or__(self, right: Any) -> _SpecialForm: ... @@ -600,27 +601,75 @@ NoExtraItems: _NoExtraItemsType # PEP 747 TypeForm: _SpecialForm -class Format(enum.IntEnum): - VALUE = 1 - FORWARDREF = 2 - STRING = 3 - # PEP 649/749 -def get_annotations( - obj: Callable[..., object] | type[object] | ModuleType, # any callable, class, or module - *, - globals: Mapping[str, Any] | None = None, # value types depend on the key - locals: Mapping[str, Any] | None = None, # value types depend on the key - eval_str: bool = False, - format: Format = Format.VALUE, # noqa: Y011 -) -> dict[str, Any]: ... # values are type expressions -def evaluate_forward_ref( - forward_ref: ForwardRef, - *, - owner: Callable[..., object] | type[object] | ModuleType | None = None, # any callable, class, or module - globals: Mapping[str, Any] | None = None, # value types depend on the key - locals: Mapping[str, Any] | None = None, # value types depend on the key - type_params: Iterable[TypeVar | ParamSpec | TypeVarTuple] | None = None, - format: Format = Format.VALUE, # noqa: Y011 - _recursive_guard: Container[str] = ..., -) -> Any: ... # str if format is Format.STRING, otherwise a type expression +if sys.version_info >= (3, 14): + from typing import evaluate_forward_ref as evaluate_forward_ref + + from annotationlib import Format as Format, get_annotations as get_annotations +else: + class Format(enum.IntEnum): + VALUE = 1 + VALUE_WITH_FAKE_GLOBALS = 2 + FORWARDREF = 3 + STRING = 4 + + @overload + def get_annotations( + obj: Any, # any object with __annotations__ or __annotate__ + *, + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + eval_str: bool = False, + format: Literal[Format.STRING], + ) -> dict[str, str]: ... + @overload + def get_annotations( + obj: Any, # any object with __annotations__ or __annotate__ + *, + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + eval_str: bool = False, + format: Literal[Format.FORWARDREF], + ) -> dict[str, AnnotationForm | ForwardRef]: ... + @overload + def get_annotations( + obj: Any, # any object with __annotations__ or __annotate__ + *, + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + eval_str: bool = False, + format: Format = Format.VALUE, # noqa: Y011 + ) -> dict[str, AnnotationForm]: ... + @overload + def evaluate_forward_ref( + forward_ref: ForwardRef, + *, + owner: Callable[..., object] | type[object] | ModuleType | None = None, # any callable, class, or module + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + type_params: Iterable[TypeVar | ParamSpec | TypeVarTuple] | None = None, + format: Literal[Format.STRING], + _recursive_guard: Container[str] = ..., + ) -> str: ... + @overload + def evaluate_forward_ref( + forward_ref: ForwardRef, + *, + owner: Callable[..., object] | type[object] | ModuleType | None = None, # any callable, class, or module + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + type_params: Iterable[TypeVar | ParamSpec | TypeVarTuple] | None = None, + format: Literal[Format.FORWARDREF], + _recursive_guard: Container[str] = ..., + ) -> AnnotationForm | ForwardRef: ... + @overload + def evaluate_forward_ref( + forward_ref: ForwardRef, + *, + owner: Callable[..., object] | type[object] | ModuleType | None = None, # any callable, class, or module + globals: Mapping[str, Any] | None = None, # value types depend on the key + locals: Mapping[str, Any] | None = None, # value types depend on the key + type_params: Iterable[TypeVar | ParamSpec | TypeVarTuple] | None = None, + format: Format = Format.VALUE, # noqa: Y011 + _recursive_guard: Container[str] = ..., + ) -> AnnotationForm: ... diff --git a/tests/stubtest_stdlib.py b/tests/stubtest_stdlib.py index a2d2afa90ba2..ac794eb4bacd 100755 --- a/tests/stubtest_stdlib.py +++ b/tests/stubtest_stdlib.py @@ -27,6 +27,7 @@ def run_stubtest(typeshed_dir: Path) -> int: "-m", "mypy.stubtest", "--check-typeshed", + "--show-traceback", "--custom-typeshed-dir", str(typeshed_dir), *allowlist_stubtest_arguments("stdlib"),