Skip to content

cattrs 1.7.0 fails to import _LiteralGenericAlias on Python 3.9.0 #150

Closed

Description

@ferraith
  • cattrs version: 1.7.0
  • Python version: 3.9.0
  • Operating System: Ubuntu Linux

Description

cattrs 1.7.0 introduced support for Literals. Therefore it imports on Python 3.9 some classes part of the typing module. For instance the _LiteralGenericAlias is imported:
https://github.com/Tinche/cattrs/blob/71a7ed2b4c72558af77487ae991f19cc5401c378/src/cattr/_compat.py#L195

These classes weren't introduced with the first release of Python 3.9.0 but with Python 3.9.1. See comparison between Python 3.9.0 and 3.9.1: python/cpython@v3.9.0...v3.9.1

The conditional import in the _compat.py module doesn't check the patch version of Python 3.9.x:
https://github.com/Tinche/cattrs/blob/71a7ed2b4c72558af77487ae991f19cc5401c378/src/cattr/_compat.py#L88
and:
https://github.com/Tinche/cattrs/blob/71a7ed2b4c72558af77487ae991f19cc5401c378/src/cattr/_compat.py#L181

What I Did

Import cattrs in user code on Python 3.9.0.

  File \"mycode.py\", line 7, in <module>
    import cattr
  File \"venv/lib/python3.9/site-packages/cattr/__init__.py\", line 1, in <module>
    from .converters import Converter, GenConverter, UnstructureStrategy
  File \"venv/lib/python3.9/site-packages/cattr/converters.py\", line 10, in <module>
    from ._compat import (
  File \"venv/lib/python3.9/site-packages/cattr/_compat.py\", line 191, in <module>
    from typing import (
ImportError: cannot import name '_LiteralGenericAlias' from 'typing' (/usr/lib/python3.9/typing.py)

Activity

  1. Tinche commented on May 27, 2021

    @Tinche
    Member

    Bleh, you have to be kidding me.

    Ok, I need to conditionally import this :/

  2. Tinche commented on May 27, 2021

    @Tinche
    Member

    @ferraith Can you import _LiteralGenericAlias from typing_extensions if you pip install it? We could fall back to that.

  3. ferraith commented on May 27, 2021

    @ferraith
    Author

    @Tinche
    I installed typing_extensions==3.10.0.0
    Successfully installed typing-extensions-3.10.0.0

    But if you have a look in the typing_extensions module there is no _LiteralGenericAlias. Only a Literal class is available:
    https://github.com/python/typing/blob/3.10.0.0/typing_extensions/src_py3/typing_extensions.py

    Because of that also importing the _LiteralGenericAlias class is failing:

        from typing_extensions import _LiteralGenericAlias
    ImportError: cannot import name '_LiteralGenericAlias' from 'typing_extensions' (/workspace/venv/lib/python3.9/site-packages/typing_extensions.py)
    
  4. Tinche commented on May 27, 2021

    @Tinche
    Member

    I took a look at the internal implementation of the module, but it looks like there are several implementations of the Literal alias. In the interest of simplicity I'll just change it so literals don't work if they aren't present in typing.

  5. ferraith commented on May 27, 2021

    @ferraith
    Author

    I took a look at the internal implementation of the module, but it looks like there are several implementations of the Literal alias. In the interest of simplicity I'll just change it so literals don't work if they aren't present in typing.

    Very nice! Do you plan a bugfix release like 1.7.1 for that fix or will the change be part of the next upcoming release?

  6. Tinche commented on May 27, 2021

    @Tinche
    Member

    I can put out a hotfix tonight!

  7. ferraith commented on May 27, 2021

    @ferraith
    Author

    @Tinche A hotfix would be excellent. Thanks a lot for your work!

  8. Tinche commented on May 28, 2021

    @Tinche
    Member

    I pushed a fix to master, can you install from git and verify it works for you? I can release then.

  9. ferraith commented on May 28, 2021

    @ferraith
    Author

    @Tinche I tested your fix in my Python 3.9.0 environment and the import is now working like expected. Thanks for that fix!

  10. Tinche commented on May 28, 2021

    @Tinche
    Member

    1.7.1 released!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions