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.

Update type hinting to follow PEP 585

See original GitHub issue

Is your feature request related to a problem? Please describe.

I am getting the following warning

/Users/bpedigo/JHU_code/bilateral/.venv/lib/python3.9/site-packages/beartype/_util/hint/pep/utilpeptest.py:396: 
BeartypeDecorHintPepDeprecatedWarning: Type hint typing.List[scipy.sparse.csr.csr_matrix] deprecated by PEP 585. To 
resolve this, globally replace this hint by the equivalent PEP 585 type hint (e.g., "typing.List[int]" by "list[int]"). 
See also:
    https://www.python.org/dev/peps/pep-0585
  warn(warning_message, BeartypeDecorHintPepDeprecatedWarning)

Describe the solution you’d like

Should update anywhere in the code we use typing.List to be just list[], I think? That’s just what it sounds like from a cursory glance at https://www.python.org/dev/peps/pep-0585/ but I could be wrong.

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
leyceccommented, Sep 30, 2021

While perambulating in an exhausted late-night stupor through this exciting project, I couldn’t help but notice this issue. As Mr. Beartype, I am the culprit responsible for everyone’s pain here. 😞

The core issue is that PEP 585 deprecates most of PEP 484. Specifically, most of the typing module (including typing.List) will be going away by 2024. You have two options here:

  • Easy way. The easy way is, as @bdpedigo suggests, to just unilaterally squelch this warning with an ignore warning filter on BeartypeDecorHintPepDeprecatedWarning. Since this is currently the only type of deprecated type hint, this is a decent temporary solution. Of course, this solution will still fail sometime in 2024 with fiery explosions. Thanks alot, Guido! If that wasn’t easy enough, here are two even easier ways to do this with differing tradeoffs depending on who you want to suffer the most – you or your awesome userbase:
# Do it globally for everyone, whether they want you to or not!
# This is the "Make Users Suffer" option.
from beartype.roar import BeartypeDecorHintPepDeprecatedWarning
from warnings import filterwarnings
filterwarnings("ignore", category=BeartypeDecorHintPepDeprecatedWarning)
...

# Do it locally only for you! (Hope you like globally increasing your
# indentation level in every single package module.)
# This is the "Make Yourself Suffer" option.
from beartype.roar import BeartypeDecorHintPepDeprecatedWarning
from warnings import catch_warnings, filterwarnings
with catch_warnings():
    filterwarnings("ignore", category=BeartypeDecorHintPepDeprecatedWarning)
    ...
  • Hard way. The hard way is to use type aliases to conditionally annotate callables with either PEP 484 or 585 type hints depending on the major version of the current Python interpreter. Since this is life, the hard way is also the best way – but it’s also hard. For example, you might define a new private graspologic._typing submodule resembling:
from sys import version_info

if version_info >= (3, 9):
    List = list
    Tuple = tuple
    ...
else:
    from typing import List, Tuple, ...

Then instead of importing List and Tuple from typing when annotating callables, you import List and Tuple from your private graspologic._typing submodule instead: e.g.,

# Instead of this...
from typing import List, Tuple

# ...just do this.
from graspologic._typing import List, Tuple

What could be simpler? gagging sounds faintly heard

And… I should just write this up as a FAQ entry already. May your pain be felt by no one else.

2reactions
leyceccommented, Oct 22, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

PEP 585 – Type Hinting Generics In Standard Collections
This PEP proposes to enable support for the generics syntax in all standard collections currently available in the typing module. Rationale and ...
Read more >
typing.Any in Python 3.9 and PEP 585 - Stack Overflow
Starting with Python 3.9 , the following collections become generic and importing those from typing is deprecated: tuple # typing.
Read more >
snok/pep585-upgrade: Pre-commit hook for ... - GitHub
This is a pre-commit hook configured to automatically upgrade your type hints to the new native types implemented in PEP 585. This will...
Read more >
Another kind of clean up comes in PEP 585 ("Type Hinting ...
It will allow the removal of a parallel set of type aliases maintained in the typing module in order to support generic types....
Read more >
Support for type hints — Python 3.9.2 documentation
The Python runtime does not enforce function and variable type annotations. ... See details in PEP 585—Type Hinting Generics In Standard ...
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