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.

AttributeError for Literal from typing_extensions package on Python 3.6

See original GitHub issue

We’re using beartype on Python 3.6 with some Literal hints. We’ve run into an error with code hinted using this flavour of Literal.

(env) PS C:\Users\pbourke\Documents\projects\beartype_literal_error> python --version
Python 3.6.8

(env) PS C:\Users\pbourke\Documents\projects\beartype_literal_error> pip list
Package           Version
----------------- --------
beartype          0.8.1
pip               18.1
setuptools        40.6.2
typing-extensions 3.10.0.2
You are using pip version 18.1, however version 21.2.4 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

(env) PS C:\Users\pbourke\Documents\projects\beartype_literal_error> cat .\test.py
from beartype import beartype
from typing_extensions import Literal


@beartype
def example(a: Literal["a"]) -> None:
    pass


if __name__ == '__main__':
    example("a")

(env) PS C:\Users\pbourke\Documents\projects\beartype_literal_error> python .\test.py
Traceback (most recent call last):
  File ".\test.py", line 6, in <module>
    def example(a: Literal["a"]) -> None:
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\main.py", line 159, in beartype
    func_wrapper_code = generate_code(func_data)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\_code\codemain.py", line 162, in generate_code
    code_params = _code_check_params(data)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\_code\codemain.py", line 355, in _code_check_params
    param_index=param_index,
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\_code\_pep\pepcode.py", line 157, in pep_code_check_param
    func=data.func, param_name=param.name),
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_util\cache\utilcacheerror.py", line 200, in reraise_exception_cached
    raise exception.with_traceback(exception.__traceback__)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\_code\_pep\pepcode.py", line 137, in pep_code_check_param
    ) = pep_code_check_hint(hint)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_util\cache\utilcachecall.py", line 351, in _callable_cached
    raise exception
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_util\cache\utilcachecall.py", line 344, in _callable_cached
    *args, **kwargs)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_decor\_code\_pep\_pephint.py", line 1883, in pep_code_check_hint
    die_unless_hint_pep586(hint_curr)
  File "C:\Users\pbourke\Documents\projects\beartype_literal_error\env\lib\site-packages\beartype\_util\hint\pep\proposal\utilpep586.py", line 89, in die_unless_hint_pep586
    hint_literals = hint.__args__
AttributeError: '_Literal' object has no attribute '__args__'

I think the root issue is that the typing package version stores arguments in __args__, but typing_extensions uses __values__:

https://github.com/python/typing/blob/5cc1d2c840d9a99aeeb442ea6bf71d7ce2dee523/typing_extensions/src_py3/typing_extensions.py#L682

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
daxprycecommented, Sep 30, 2021

@leycec I may not be able to rip out an incredibly comprehensive and dense usage guide filled with bearpuns, but I still tried a little in our latest release notes: https://graspologic.readthedocs.io/en/latest/release.html#graspologic-0-3-0

1reaction
leyceccommented, Sep 29, 2021

Fascinating… and horrifying. I’ve been hoping without evidence that typing_extensions would routinely rebase its runtime implementation against typing from the official Python repo, but… those hopes and more have now been dashed. I may be crying inside.

It could be argued that any divergence from typing is an upstream bug in typing_extensions. In this case, __values__ is surprisingly non-standard; popular annotation standards like PEP 484 and 585 institutionalized the use of __args__, __parameters__, and __origin__ for runtime introspection. Even NumPy’s third-party numpy.typing API sticks to that nomenclature. I may be grumbling inside.

It could also be argued that the utmost satisfaction of our glorious userbase comes before my own long-standing laziness and desire to continually shift blame elsewhere – and that is the better argument to make.

I was moments away from releasing beartype 0.9.0 when this and #51 delightfully landed. Naturally, the happiness of you personally and Microsoft takes precedence. I’ll get this immediately resolved for you, Pat.

Oh – and thanks so much for the detailed issue complete with nostalgic PowerShell logs and low-level diagnostic analysis. Have some fireworks!

✨ 🎇 🎆 🎇 ✨

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImportError: cannot import name 'Literal' from 'typing'
I have recently started using PEP 484 and PEP 586 to make my code clearer and more accessible. So far everything was ok,...
Read more >
typing-extensions - PyPI
Overview. The typing_extensions module serves two related purposes: Enable use of new type system features on older Python versions. For example, typing.
Read more >
Issues with the typing Library in Python 3.7 and AWS Lambda
In this post I take a quick look at issues with the typing library and it's inclusion in Python 3.7.
Read more >
What's New In Python 3.8 — Python 3.11.1 documentation
Editor, Raymond Hettinger,. This article explains the new features in Python 3.8, compared to 3.7. Python 3.8 was released on October 14, 2019....
Read more >
Trying to import TypedDict from typing of tpying_extensions fails
I have code that runs on Python 3.8 and Python 3.7, but I'd rather not install typing_extensions when it isn't need.
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