cattrs
cattrs copied to clipboard
cannot import name '_Union'
- cattrs version: 0.9.0
- Python version: 3.5.2
- Operating System: ubuntu xenial64
Description
On a clean ubuntu VM, I pip3 install cattrs, and import cattr doesn't seem to work.
Is something funny with the typing module in python 3.5.2 or something? It looks like travis tested Python 3.5, so I don't understand how this happened.
What I Did
> vagrant init ubuntu/xenial64
...
> vagrant up
...
> vagrant ssh
...
vagrant@ubuntu-xenial:~$ wget https://bootstrap.pypa.io/get-pip.py
...
vagrant@ubuntu-xenial:~$ sudo python3 get-pip.py
...
vagrant@ubuntu-xenial:~$ pip3 install cattr
...
Successfully installed attrs-18.2.0 cattrs-0.9.0
vagrant@ubuntu-xenial:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cattr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/cattr/__init__.py", line 2, in <module>
from .converters import Converter, UnstructureStrategy
File "/usr/local/lib/python3.5/dist-packages/cattr/converters.py", line 3, in
<module>
from ._compat import (
File "/usr/local/lib/python3.5/dist-packages/cattr/_compat.py", line 86, in <module>
from typing import _Union
ImportError: cannot import name '_Union'
>>> import typing
>>> dir(typing)
['AbstractSet', 'Any', 'AnyMeta', 'AnyStr', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'BinaryIO', 'ByteString', 'CT', 'Callable', 'CallableMeta', 'Container', 'DefaultDict', 'Dict', 'Final', 'FrozenSet', 'Generator', 'Generic', 'GenericMeta', 'Hashable', 'IO', 'ItemsView', 'Iterable', 'Iterator', 'KT', 'KeysView', 'List', 'Mapping', 'MappingView', 'Match', 'MutableMapping', 'MutableSequence', 'MutableSet', 'NamedTuple', 'NewType', 'Optional', 'OptionalMeta', 'Pattern', 'Reversible', 'Sequence', 'Set', 'Sized', 'SupportsAbs', 'SupportsBytes', 'SupportsComplex', 'SupportsFloat', 'SupportsInt', 'SupportsRound', 'T', 'TYPE_CHECKING', 'T_co', 'T_contra', 'Text', 'TextIO', 'Tuple', 'TupleMeta', 'Type', 'TypeVar', 'TypingMeta', 'Union', 'UnionMeta', 'VT', 'VT_co', 'V_co', 'ValuesView', '_ForwardRef', '_FrozenSetMeta', '_G_base', '_Protocol', '_ProtocolMeta', '_TypeAlias', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_eval_type', '_geqv', '_get_defaults',
'_get_type_vars', '_gorg', '_next_in_mro', '_overload_dummy', '_qualname', '_type_check', '_type_repr', '_type_vars', 'abc', 'abstractmethod', 'abstractproperty', 'cast', 'collections', 'collections_abc', 'contextlib', 'functools', 'get_type_hints', 'io', 'no_type_check', 'no_type_check_decorator', 'overload', 're', 'stdlib_re', 'sys', 'types']
there is no '_Union' in there
on my local system, I have python 3.6.4
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing
>>> dir(typing)
['AbstractSet', 'Any', 'AnyStr', 'AsyncContextManager', 'AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'BinaryIO', 'ByteString', 'CT_co', 'Callable', 'CallableMeta', 'ChainMap', 'ClassVar', 'Collection', 'Container', 'ContextManager', 'Coroutine', 'Counter', 'DefaultDict', 'Deque', 'Dict', 'FrozenSet', 'Generator', 'Generic', 'GenericMeta', 'Hashable', 'IO', 'ItemsView', 'Iterable', 'Iterator', 'KT', 'KeysView', 'List', 'Mapping', 'MappingView', 'Match', 'MethodDescriptorType', 'MethodWrapperType', 'MutableMapping', 'MutableSequence', 'MutableSet', 'NamedTuple', 'NamedTupleMeta', 'NewType', 'NoReturn', 'Optional',
'Pattern', 'Reversible', 'Sequence', 'Set', 'Sized', 'SupportsAbs', 'SupportsBytes', 'SupportsComplex', 'SupportsFloat', 'SupportsInt', 'SupportsRound', 'T', 'TYPE_CHECKING', 'T_co', 'T_contra', 'Text', 'TextIO', 'Tuple', 'TupleMeta', 'Type', 'TypeVar', 'TypingMeta', 'Union', 'VT', 'VT_co', 'V_co', 'ValuesView', 'WrapperDescriptorType', '_Any', '_ClassVar', '_FinalTypingBase', '_ForwardRef', '_G_base', '_NoReturn', '_Optional', '_PY36', '_Protocol', '_ProtocolMeta', '_TypeAlias', '_TypingBase', '_TypingEllipsis', '_TypingEmpty', '_Union', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_allowed_types', '_check_generic', '_cleanups', '_collections_abc', '_eval_type', '_generic_new', '_get_defaults', '_get_type_vars', '_make_nmtuple', '_make_subclasshook', '_next_in_mro', '_no_slots_copy', '_overload_dummy', '_prohibited', '_qualname', '_remove_dups_flatten', '_replace_arg', '_special', '_subs_tree', '_tp_cache', '_trim_name', '_type_check', '_type_repr', '_type_vars', 'abc', 'abstractmethod', 'abstractproperty', 'cast', 'collections',
'collections_abc', 'contextlib', 'functools', 'get_type_hints', 'io', 'no_type_check', 'no_type_check_decorator', 'overload', 're', 'stdlib_re', 'sys', 'types']
That one has a '_Union'
Could you try 3.5.3 maybe?
Hi,
3.5.2 is the LTS on ubuntu, I was about to open the same issue:
- cattrs version: 0.9.0
- Python version: 3.5.2
- Operating System: ubuntu
Description
I wanted to try this package, so i installed it with pip but the import throws an exception
What I Did
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import attr, cattr
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/cattr/__init__.py", line 2, in <module>
from .converters import Converter, UnstructureStrategy
File "/usr/local/lib/python3.5/dist-packages/cattr/converters.py", line 3, in <module>
from ._compat import (
File "/usr/local/lib/python3.5/dist-packages/cattr/_compat.py", line 86, in <module>
from typing import _Union
ImportError: cannot import name '_Union'
FYI 3.8 has the same symptom, but the fix is to change the is_py37 check to something like
is_py37 = version_info[:2] >= (3, 7)
rather than the current strict equality.
Or better yet, find a different way to do those checks without reaching into private implementation details? Do we need to request additional functions from the typing/mypy guys?
@Tinche 3.5.2 looks like the option for lot of people. Is there any other way we can do the check in https://github.com/Tinche/cattrs/blob/master/src/cattr/_compat.py#L90 so that we can stop relying on '_Union' type?
@Tinche you might consider using https://github.com/ilevkivskyi/typing_inspect/blob/master/typing_inspect.py#L143 for your convenience...
Now that Python 3.8 is the default in Arch this breaks cattr for a larger amount of users. Is there any argument against the fixes proposed by @diefans and/or @llchan ?
This was in fact implemented in bb48897a7f389332d6111a239dcf3bf4a11b526f so this issue does not concern 3.8 anymore if running from master. The fix doesn't seem to be in PyPI yet and considering that some distros now ship 3.8 by default I would urge to update the package.
The question is: when it gets released? As of https://github.com/Tinche/cattrs/issues/74 people start to release their forks on pypi already :( which indicates, that this project is more used than obvious...
If/when this gets attention, I think @diefans's solution using typing_inspect is the way to go. I wasn't aware that that package existed but that's a better long-term fix than my bandaid that just just kicks the can down the road.
Bump?
This needs to be fixed, can't run airflow!
Has this been fixed? I got the same error when running https://pypi.org/project/reformat-gherkin/ with Python3.9 (which relies on Cattrs).
File "/Users/jordan.hoover/.cache/pre-commit/repoaku2szup/py_env-python3/lib/python3.9/site-packages/cattr/__init__.py", line 2, in <module>
from .converters import Converter, UnstructureStrategy
File "/Users/jordan.hoover/.cache/pre-commit/repoaku2szup/py_env-python3/lib/python3.9/site-packages/cattr/converters.py", line 15, in <module>
from ._compat import (
File "/Users/jordan.hoover/.cache/pre-commit/repoaku2szup/py_env-python3/lib/python3.9/site-packages/cattr/_compat.py", line 87, in <module>
from typing import _Union
ImportError: cannot import name '_Union' from 'typing' (/usr/local/Cellar/[email protected]/3.9.2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py)
@jhoover4 Update to the latest cattrs?
@jhoover4 Update to the latest cattrs?
Thanks for the quick reply! I'm not certain, but I think the package is using the latest Cattrs based on their Poetry file. I can check with the maintainer though.
https://github.com/ducminh-phan/reformat-gherkin/blob/master/pyproject.toml#L34
@jhoover4 https://github.com/ducminh-phan/reformat-gherkin/blob/master/poetry.lock#L48 this might be the issue. Just run poetry update in the repo. I can assure you cattrs 1.2.0 imports correctly on 3.9 :)