cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

cannot import name '_Union'

Open nturley opened this issue 7 years ago • 15 comments

  • 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'

nturley avatar Oct 18 '18 21:10 nturley

Could you try 3.5.3 maybe?

Tinche avatar Oct 19 '18 10:10 Tinche

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'

thomaxxl avatar Dec 14 '18 09:12 thomaxxl

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?

llchan avatar Jul 17 '19 21:07 llchan

@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?

kretes avatar Aug 05 '19 14:08 kretes

@Tinche you might consider using https://github.com/ilevkivskyi/typing_inspect/blob/master/typing_inspect.py#L143 for your convenience...

diefans avatar Oct 30 '19 12:10 diefans

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 ?

fmagin avatar Nov 21 '19 11:11 fmagin

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.

fmagin avatar Nov 21 '19 11:11 fmagin

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...

diefans avatar Nov 21 '19 13:11 diefans

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.

llchan avatar Nov 21 '19 20:11 llchan

Bump?

malthe avatar Mar 05 '20 08:03 malthe

This needs to be fixed, can't run airflow!

shreyashag avatar Mar 16 '20 15:03 shreyashag

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 avatar Feb 23 '21 14:02 jhoover4

@jhoover4 Update to the latest cattrs?

Tinche avatar Feb 23 '21 15:02 Tinche

@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 avatar Feb 23 '21 15:02 jhoover4

@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 :)

Tinche avatar Feb 23 '21 16:02 Tinche