Skip to content

cattrs 1.7.0 fails to import _LiteralGenericAlias on Python 3.9.0 #150

@ferraith

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)

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