Try to use typing_extensions for Literal in < python 3.8
See original GitHub issuein 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For anybody else walking on this issue, if you really can’t wait for an official fix, you can “hack” (monkey-patch)
dacite
to handletyping_extensions.Literal
.Do this, first thing in your
main.py
before usingdacite
for anything elseOr, instead of the
lambda
, you can use the function you defined in #101Ok. Never mind. Both solutions fail on python3.6 as
typing_extensions.Literal
is not a generic there sois_generic
returnsFalse
and there is no__origin__
.It should be something like: