|
4 | 4 | 'null', 'tonull', 'get_class', 'mk_class', 'wrap_class', 'ignore_exceptions', 'exec_local', 'risinstance',
|
5 | 5 | 'Inf', 'in_', 'lt', 'gt', 'le', 'ge', 'eq', 'ne', 'add', 'sub', 'mul', 'truediv', 'is_', 'is_not', 'in_',
|
6 | 6 | 'true', 'stop', 'gen', 'chunked', 'otherwise', 'custom_dir', 'AttrDict', 'get_annotations_ex', 'eval_type',
|
7 |
| - 'type_hints', 'annotations', 'anno_ret', 'argnames', 'with_cast', 'store_attr', 'attrdict', 'properties', |
8 |
| - 'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs', 'hasattrs', 'setattrs', |
9 |
| - 'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', 'Float', 'flatten', |
10 |
| - 'concat', 'strcat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', 'last_index', |
11 |
| - 'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', 'argwhere', |
12 |
| - 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_callable', 'nested_idx', |
| 7 | + 'type_hints', 'annotations', 'anno_ret', 'union2tuple', 'argnames', 'with_cast', 'store_attr', 'attrdict', |
| 8 | + 'properties', 'camel2words', 'camel2snake', 'snake2camel', 'class2attr', 'getcallable', 'getattrs', |
| 9 | + 'hasattrs', 'setattrs', 'try_attrs', 'GetAttrBase', 'GetAttr', 'delegate_attr', 'ShowPrint', 'Int', 'Str', |
| 10 | + 'Float', 'flatten', 'concat', 'strcat', 'detuplify', 'replicate', 'setify', 'merge', 'range_of', 'groupby', |
| 11 | + 'last_index', 'filter_dict', 'filter_keys', 'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'not_', |
| 12 | + 'argwhere', 'filter_ex', 'range_of', 'renumerate', 'first', 'nested_attr', 'nested_callable', 'nested_idx', |
13 | 13 | 'set_nested_idx', 'val2idx', 'uniqueify', 'loop_first_last', 'loop_first', 'loop_last', 'num_methods',
|
14 | 14 | 'rnum_methods', 'inum_methods', 'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'mapt', 'map_ex',
|
15 | 15 | 'compose', 'maps', 'partialler', 'instantiate', 'using_attr', 'Self', 'Self', 'copy_func', 'patch_to',
|
|
20 | 20 | from .imports import *
|
21 | 21 | import builtins,types
|
22 | 22 | import pprint
|
| 23 | +try: from types import UnionType |
| 24 | +except ImportError: UnionType = None |
23 | 25 |
|
24 | 26 | # Cell
|
25 | 27 | defaults = SimpleNamespace()
|
@@ -289,7 +291,7 @@ def get_annotations_ex(obj, *, globals=None, locals=None):
|
289 | 291 | def eval_type(t, glb, loc):
|
290 | 292 | "`eval` a type or collection of types, if needed, for annotations in py3.10+"
|
291 | 293 | if isinstance(t,str):
|
292 |
| - if '|' in t: return eval_type(tuple(t.split('|')), glb, loc) |
| 294 | + if '|' in t: return Union[eval_type(tuple(t.split('|')), glb, loc)] |
293 | 295 | return eval(t, glb, loc)
|
294 | 296 | if isinstance(t,(tuple,list)): return type(t)([eval_type(c, glb, loc) for c in t])
|
295 | 297 | return t
|
@@ -320,6 +322,12 @@ def anno_ret(func):
|
320 | 322 | "Get the return annotation of `func`"
|
321 | 323 | return annotations(func).get('return', None) if func else None
|
322 | 324 |
|
| 325 | +# Cell |
| 326 | +def union2tuple(t): |
| 327 | + if (getattr(t, '__origin__', None) is Union |
| 328 | + or (UnionType and isinstance(t, UnionType))): return t.__args__ |
| 329 | + return t |
| 330 | + |
323 | 331 | # Cell
|
324 | 332 | def argnames(f, frame=False):
|
325 | 333 | "Names of arguments to function or frame `f`"
|
@@ -906,7 +914,7 @@ def patch(f=None, *, as_prop=False, cls_method=False):
|
906 | 914 | "Decorator: add `f` to the first parameter's class (based on f's type annotations)"
|
907 | 915 | if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)
|
908 | 916 | ann,glb,loc = get_annotations_ex(f)
|
909 |
| - cls = eval_type(ann.pop('cls') if cls_method else next(iter(ann.values())), glb, loc) |
| 917 | + cls = union2tuple(eval_type(ann.pop('cls') if cls_method else next(iter(ann.values())), glb, loc)) |
910 | 918 | return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)
|
911 | 919 |
|
912 | 920 | # Cell
|
|
0 commit comments