seandstewart / typical

Typical: Fast, simple, & correct data-validation using Python 3 typing.
https://python-typical.org
MIT License
183 stars 9 forks source link

Support new type union operator `|` in Python3.10 #160

Closed songzhi closed 3 years ago

songzhi commented 3 years ago

Description

It seems that typical doesn't support new type union operator | in Python3.10.

import typic

@typic.al
def foo(bar: str | int):
    pass

Output

Traceback (most recent call last):
  File "/home/lsongzhi/code/python/pydemo/main.py", line 5, in <module>
    def foo(bar: str | int):
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/api.py", line 389, in typed
    return _typed(_cls_or_callable) if _cls_or_callable is not None else _typed
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/api.py", line 383, in _typed
    return wrap(obj, delay=delay, strict=strict)  # type: ignore
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/api.py", line 170, in wrap
    protocols(func)
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/serde/resolver.py", line 752, in protocols
    resolved = self.resolve(
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/serde/resolver.py", line 670, in resolve
    anno = self.annotation(
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/serde/resolver.py", line 484, in annotation
    self._get_configuration(util.origin(use), flags)
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/serde/resolver.py", line 307, in _get_configuration
    hints = util.cached_type_hints(origin)
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/site-packages/typic/util.py", line 460, in get_type_hints
    return _get_type_hints(obj)
  File "/home/lsongzhi/.pyenv/versions/3.10.0b2/lib/python3.10/typing.py", line 1749, in get_type_hints
    raise TypeError('{!r} is not a module, class, method, '
TypeError: str | int is not a module, class, method, or function.