question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Try to use typing_extensions for Literal in < python 3.8

See original GitHub issue

in dacite/types.py:66 it is directly importing from typing - could it also try importing from typing_extensions?

def is_literal(type_: Type) -> bool:
    try:
        from typing import Literal  # type: ignore

        return is_generic(type_) and type_.__origin__ == Literal
    except ImportError:
        return False


def is_literal(type_: Type) -> bool:
    try:
        from typing import Literal  # type: ignore

        return is_generic(type_) and type_.__origin__ == Literal
    except ImportError:
        try:
            from typing_extensions import Literal # type: ignore

            return is_generic(type_) and type_.__origin__ == Literal
       except ImportError:
            return False

I’m writing a parsing library for internal use that needs to work for both 3.7 and 3.8 - this would be super helpful.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
c-ghignycommented, Sep 8, 2020

For anybody else walking on this issue, if you really can’t wait for an official fix, you can “hack” (monkey-patch) dacite to handle typing_extensions.Literal.

Do this, first thing in your main.py before using dacite for anything else

from dacite import types
from typing_extensions import Literal
setattr(types, 'is_literal', lambda type_: types.is_generic(type_) and type_.__origin__ == Literal)

Or, instead of the lambda, you can use the function you defined in #101


from dacite import types
from types import is_generic

def is_literal(type_: Type) -> bool:
    try:
        from typing import Literal  # type: ignore

        return is_generic(type_) and type_.__origin__ == Literal
    except ImportError:
        try:
            from typing_extensions import Literal  # type: ignore

            return is_generic(type_) and type_.__origin__ == Literal
        except ImportError:
            return False

setattr(types, 'is_literal', is_literal)
2reactions
Agalincommented, Feb 8, 2021

Ok. Never mind. Both solutions fail on python3.6 as typing_extensions.Literal is not a generic there so is_generic returns False and there is no __origin__.

It should be something like:

def is_literal(type_: Type) -> bool:
    try:
        from typing import Literal  # type: ignore

        return is_generic(type_) and type_.__origin__ == Literal
    except ImportError:
        try:
            from typing_extensions import Literal  # type: ignore

            return type(type_) == type(Literal)
        except ImportError:
            return False
Read more comments on GitHub >

github_iconTop Results From Across the Web

Literal types and Enums - mypy 0.991 documentation
These types were added to typing in Python 3.8, but are also available for use in Python 3.4 - 3.7 via the typing_extensions...
Read more >
ImportError: cannot import name 'Literal' from 'typing'
Literal was added to typing.py in 3.8, but you can use Literal in older versions anyway. First install typing_extensions ( pip install ...
Read more >
PEP 586 – Literal Types - Python Enhancement Proposals
We plan to do this by adding Literal to the typing_extensions 3rd party module, which contains a variety of other backported types.
Read more >
typing-extensions - PyPI
Overview · Enable use of new type system features on older Python versions. For example, typing.TypeGuard is new in Python 3.10, but typing_extensions...
Read more >
Python Type Hints - How to Split Types by Python Version
Literal was only introduced in Python 3.8. For this code to work on Python 3.7 or earlier, you can conditionally avoid using Literal...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found