mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
gh-105499: Merge typing.Union and types.UnionType (#105511)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Ken Jin <kenjin@python.org> Co-authored-by: Carl Meyer <carl@oddbird.net>
This commit is contained in:
parent
e091520fdb
commit
dc6d66f44c
20 changed files with 562 additions and 327 deletions
|
@ -926,16 +926,11 @@ def singledispatch(func):
|
|||
dispatch_cache[cls] = impl
|
||||
return impl
|
||||
|
||||
def _is_union_type(cls):
|
||||
from typing import get_origin, Union
|
||||
return get_origin(cls) in {Union, UnionType}
|
||||
|
||||
def _is_valid_dispatch_type(cls):
|
||||
if isinstance(cls, type):
|
||||
return True
|
||||
from typing import get_args
|
||||
return (_is_union_type(cls) and
|
||||
all(isinstance(arg, type) for arg in get_args(cls)))
|
||||
return (isinstance(cls, UnionType) and
|
||||
all(isinstance(arg, type) for arg in cls.__args__))
|
||||
|
||||
def register(cls, func=None):
|
||||
"""generic_func.register(cls, func) -> func
|
||||
|
@ -967,7 +962,7 @@ def singledispatch(func):
|
|||
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):
|
||||
if isinstance(cls, UnionType):
|
||||
raise TypeError(
|
||||
f"Invalid annotation for {argname!r}. "
|
||||
f"{cls!r} not all arguments are classes."
|
||||
|
@ -983,10 +978,8 @@ def singledispatch(func):
|
|||
f"{cls!r} is not a class."
|
||||
)
|
||||
|
||||
if _is_union_type(cls):
|
||||
from typing import get_args
|
||||
|
||||
for arg in get_args(cls):
|
||||
if isinstance(cls, UnionType):
|
||||
for arg in cls.__args__:
|
||||
registry[arg] = func
|
||||
else:
|
||||
registry[cls] = func
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue