python 3.8 raises warning in from beartype import beartype
See original GitHub issuePython 3.8 on Win10 raises warnings when using PYTHONDEVMODE=1
:
C:\Program Files\Python39\lib\site-packages\beartype\_util\hint\pep\utilhintpeptest.py:367: BeartypeDecorHintPepDeprecatedWarning: PEP type hint typing.List[__main__.DbEntry] 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)
C:\Program Files\Python39\lib\site-packages\beartype\_util\hint\pep\utilhintpeptest.py:367: BeartypeDecorHintPepDeprecatedWarning: PEP type hint typing.Tuple[__main__.HintsHeader, typing.List[__main__.Hint]] 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)
C:\Program Files\Python39\lib\site-packages\beartype\_util\hint\pep\utilhintpeptest.py:367: BeartypeDecorHintPepDeprecatedWarning: PEP type hint typing.List[__main__.Hint] 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)
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Beartype: Unbearably fast O(1) runtime type-checking in pure ...
Beartype is an open-source pure-Python PEP-compliant constant-time runtime type checker emphasizing efficiency, portability, and thrilling puns.
Read more >Python Type Checking (Guide) - Real Python
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit...
Read more >beartype: Documentation - Openbase
from beartype.abby import is_bearable from beartype.typing import List, ... when these optional runtime dependencies are *all* satisfied: * Python ≥ 3.8.0.
Read more >Beartype: Fast runtime type checking in Python - Hacker News
So for example a third party can import your code and call functions with the wrong type. Because of duck typing, sometimes this...
Read more >Annotation issues at runtime - mypy 0.991 documentation
Use of from __future__ import annotations (PEP 563) (this behaviour may ... raise an error when evaluated (say by using PEP 604 syntax...
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
Thank you. No, really. You’ve taken precious time out of your Friday and Saturday for our humble project. I appreciate it. 👍
You are, of course, correct. That was a copy-and-paste PEBKAC due to me being up so late that Friday night became Saturday morning. Gentoo’s Python3.9 is thankfully working as intended; here’s the actual output:
phew
I’m so glad you asked, but you might not be in about five seconds. Gentoo has its own homegrown solution for installing multiple versions of the same underlying software. We call this stupefying technology slots.
Slots means that Python developers can easily install and update multiple versions of multiple Python interpreters and multiple versions of multiple Python packages for those multiple interpreters – all with a single command. Here is me installing mypy under CPython 3.7, 3.8, and 3.9 with a single invocation of
emerge
, the Gentoo package manager:Gentoo thus installs, updates, and manages
python3.7
,python3.8
,python3.9
, andpypy3
commands and Python packages isolated to each of those commands for me on my behalf. It’s likepyenv
+venv
, only automated at the OS level without any manual intervention from me.Command-line wizardry gets no hotter than that.
…back to reality.
Yes! I love(d) Cygwin! I was reasonably active in the Babun community, until I realized that the Big List Of Dodgy Apps (BLODA) made Cywin and thus Babun effectively unusable for me. Then the Windows Subsystem for Linux (WSL) landed, which mostly solved everything despite being really slow, which admittedly is a blocker.
But I commend you for sticking with Cygwin. I certainly have fond memories of it – up until the End.
That looks sane! So far, so good.
That looks distinctly less sane.
beartype
contains conditional logic dependent on the current Python minor version (e.g., the9
in3.9.1
), because type hinting syntax and semantics change across minor versions.beartype
does not contain conditional logic dependent on the current Python patch version (e.g., the1
in3.9.1
), because nothing is supposed to change across patch versions.I’m also fairly sure that the behaviour of the Python Development Mode (PDM) does not change across patch versions (e.g., going from 3.8.7 to 3.8.8).
What you’re seeing is inexplicable. That troubles me. We’re probably to blame, because we usually are. Still… I’m having trouble thinking of a plausible explanation for that behaviour. Urgh!
Oh. We seem to have confusion, which means I’m probably to blame. I’m awful at explanation, which is why I’m an engineer.
The deprecation warning you’re seeing isn’t about our code; it’s about your code.
beartype
is intentionally warning you that your code will break in four years, because most of thetyping
module is going away in four years. In four years, this is what is going to happen:Or maybe you were asking whether substituting
List = list
in your own code would work? If so, the answer is sadly “Nope.” Why? Because that code would then break under Python 3.8, which doesn’t support PEP 585 type hints (e.g.,list[str]
). Static type checkers like mypy also won’t be fooled by those sorts of shenanigans. It’s best just to do the Right Thing™.Now that you’re duly apprised and aware of PEP 585 deprecations, my advice as annotation guru would be to either:
list[str]
) aren’t usable under Python 3.8, but they’re the future. Migrating from PEP 484 type hints (e.g.,typing.List[str]
) is non-trivial, because you have to stop importing fromtyping
. It makes sense to pay that price now (when it doesn’t cost as much, because you haven’t written as much code) rather than later (when it will cost a lot more, because you’ll have written four years worth of code). Urgh!<rant>
There are millions of lines of working Python code that will break when most of the
typing
module goes away in four years. Official Python developers will be breaking backward compatibility in a huge way… yet again. We all remember how well that Python 2 → 3 transition went, right?But we can’t do anything about that except yell at official Python developers on their mailing lists and bug trackers. Of course, they won’t like that, so I advise not doing that. They may be objectively wrong here, but they still make the rules.
</rant>
The only issue for
beartype
here is that you’re seeing a deprecation warning under Python 3.8 that you shouldn’t. You should only be seeing that deprecation warning under Python 3.9 and 3.10. But… that’s hardly the worst thing to ever happen. We can’t replicate this and the fact that you see different behaviour between CPython 3.8.7 and 3.8.8 is a red flag of weirdness beyond our control.I love fielding questions like this. Thanks for asking. 😄
The simple answer is: “Nope, but a related package called
bearcall
will let you do that soon!”The complex answer is that variable annotations are out-of-scope for
beartype
itself. Variable annotations were standardized by PEP 526, whichbeartype
explicitly does not support.beartype
operates at the coarse-grained level of functions and methods rather than the fine-grained level of individual statements in functions and methods.Instead, we’re making a new Python project later this year called
bearcall
. Unlikebeartype
,bearcall
will operate at the fine-grained level of individual statements in functions and methods – including assignment statements with variable annotations. Specifically,bearcall
will let you type-check any arbitrary object at any arbitrary time against any PEP-compliant type hint by passing that object and hint to a simplebearcall()
validation function: e.g.,The
bearcall()
API intentionally resembles theisinstance()
builtin. Anything you can pass toisinstance()
, you can also pass tobearcall()
. The reverse is naturally not true, becausebearcall()
will also check arbitrary objects against arbitrarily complex type hints inO(1)
time.bearcall
will be internally implemented withbeartype
– giving you the full power, efficiency, and expressiveness ofbeartype
anywhere.There’s a bit of a DRY violation above, because we repeat the
list[int]
type hint twice. That’s unavoidable within functions, because CPython doesn’t actually store local variable annotations anywhere; they’re just stripped and ignored at runtime.Module and class variables are a different story. CPython does store those somewhere (i.e., in an
__annotations__
dunder dictionary), sobearcall
could also offer abearcall_nonlocal()
validation function accepting either a module or class variable and automatically looking up that variable’s type hint for you: e.g.,Is that something you’d be interested in? If so, I’d be happy to reprioritize. I was holding off on
bearcall
until I’d releasedbeartype 1.0.0
, which wouldn’t be until the end of this year. If there’s interest, I’d consider pushingbearcall
forward into the earlier part of this year. Just let me know, eh?Thanks again for all the interest, Doug! I sure hope you’re having a warmer weekend over there than we are here in Ontario. Brrrrrrrr. ❄️
Wow. I just checked out pywikitree, your impressive Python-based WikiTree API. That’s just wizardly! Tracking my wife’s genealogy would be equally fascinating and depressing, because her mother’s family fled Lithuania during the German-Soviet invasion of World War II on horseback over sickeningly tall mountain ranges that I’d be lucky to even stagger up today. 🗻