mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
gh-119180: Add annotationlib module to support PEP 649 (#119891)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
64e221d7ad
commit
7b7b90d1ce
15 changed files with 1815 additions and 510 deletions
|
|
@ -32,7 +32,7 @@ GenericAlias = type(list[int])
|
|||
# wrapper functions that can handle naive introspection
|
||||
|
||||
WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__',
|
||||
'__annotations__', '__type_params__')
|
||||
'__annotate__', '__type_params__')
|
||||
WRAPPER_UPDATES = ('__dict__',)
|
||||
def update_wrapper(wrapper,
|
||||
wrapped,
|
||||
|
|
@ -882,8 +882,8 @@ def singledispatch(func):
|
|||
f"Invalid first argument to `register()`. "
|
||||
f"{cls!r} is not a class or union type."
|
||||
)
|
||||
ann = getattr(cls, '__annotations__', {})
|
||||
if not ann:
|
||||
ann = getattr(cls, '__annotate__', None)
|
||||
if ann is None:
|
||||
raise TypeError(
|
||||
f"Invalid first argument to `register()`: {cls!r}. "
|
||||
f"Use either `@register(some_class)` or plain `@register` "
|
||||
|
|
@ -893,13 +893,19 @@ def singledispatch(func):
|
|||
|
||||
# only import typing if annotation parsing is necessary
|
||||
from typing import get_type_hints
|
||||
argname, cls = next(iter(get_type_hints(func).items()))
|
||||
from annotationlib import Format, ForwardRef
|
||||
argname, cls = next(iter(get_type_hints(func, format=Format.FORWARDREF).items()))
|
||||
if not _is_valid_dispatch_type(cls):
|
||||
if _is_union_type(cls):
|
||||
raise TypeError(
|
||||
f"Invalid annotation for {argname!r}. "
|
||||
f"{cls!r} not all arguments are classes."
|
||||
)
|
||||
elif isinstance(cls, ForwardRef):
|
||||
raise TypeError(
|
||||
f"Invalid annotation for {argname!r}. "
|
||||
f"{cls!r} is an unresolved forward reference."
|
||||
)
|
||||
else:
|
||||
raise TypeError(
|
||||
f"Invalid annotation for {argname!r}. "
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue