Repository navigation
Some improvements for Python 3.14 / PEP 649 support - #440
JelleZijlstra wants to merge 1 commit into
Conversation
It's still very early for Python 3.14, but I decided to try running beartype's test suite to see how well it works with all the annotations changes that have landed in CPython. There were a few test failures for boring reasons (mostly, the fact that typing.ForwardRef is now in the annotationlib module). I fixed some but not all of those. I also tried to make beartype work well with code that relies on new PEP 649 functionality, such as: from beartype import beartype @beartype def f(x: notyetdefined) -> notyetdefined: return x class notyetdefined: pass f(notyetdefined()) f(1) # error To support such code, I made beartype use annotationlib.get_annotations() with the FORWARDREF format, which appears to work fine. I will leave this as a draft for now since it's still early in the PEP 649 stage, but I feel this is a promising indicator that it shouldn't be too hard to adapt beartype to the PEP 649 world.
|
Wondrousness! Thanks so much for this incredible show of strength, Typing Master @JelleZijlstra. I didn't even know It's wonderful to see that PEP 649 is now mostly trivially supportable, too. That's a tremendous relief for all of us on the runtime side of the aisle. You even generalized @beartype's low-level All of your changes are literally perfect. I have nothing meaningful to add. When you're ready, I'll happily merge this. Let me know when you'd like me to push that giant glowing red "MERGE DIS!" button. I do have a foolish question:
I recall that being a bit... complex in the initial PEP 649 writeup. To avoid destroying Python, @beartype tries (as hard as possible, anyway) to avoid modifying dunder attributes like Should @beartype reconsider that? I should probably just re-read the new PEP 649 spec, huh? Oh, precious free time! Where art thou, free time? 🥹 |
|
One thing that might still change is that
This is something we're still talking about. What I'd currently like is written up at file:///Users/jelle/py/peps/build/pep-0749.html#effect-of-setting-annotations, but PEP 649 proposed something more complex, and we may go back to a version of that. There's two kinds of modifying that we need to be concerned about:
Modifying annotations will almost certainly continue to work in the sense that you can run |
|
Guh! I just wrote a doctoral thesis on this. Then GitHub ate my thesis. This truncated snippet of my former glory is all that remains. You should've seen the original post. It was a thing of wonder. Interestingly, the @beartype codebase performs both kinds of PEP 563 Resolution: Who Didn't See That One Coming?@beartype generally prefers to only modify # In the public "beartype._peps.pep563" submodule:
def resolve_pep563(...):
...
# Attempt to...
try:
# Atomically (i.e., all-at-once) replace that callable's postponed
# annotations with these resolved annotations for safety and efficiency.
#
# While the @beartype decorator goes to great lengths to preserve the
# originating "__annotations__" dictionary as is, PEP 563 is
# sufficiently expensive, non-trivial, and general-purpose to support
# that generically resolving postponed annotations for all downstream
# third-party callers is justified. Everyone benefits from replacing
# useless postponed annotations with useful real annotations; so, do so.
func.__annotations__ = arg_name_to_hint
# If doing so fails with an exception resembling the following, then that
# callable is *NOT* a pure-Python callable but rather a C-based decorator
# object of some sort (e.g., class, property, or static method descriptor):
# AttributeError: 'method' object has no attribute '__annotations__'
#
# C-based decorator objects define a read-only "__annotations__" dunder
# attribute that proxies an original writeable "__annotations__" dunder
# attribute of the pure-Python callables they originally decorated. Ergo,
# detecting this edge case is non-trivial and most easily deferred to
# this late time. While non-ideal, simplicity >>>> idealism in this case.
except AttributeError:
# For the name of each annotated parameter and return of that callable
# and the destringified type hint annotating this parameter or return,
# overwrite the stringified type hint originally annotating this
# parameter or return with this destringified type hint.
#
# Note that:
# * The above assignment is an efficient O(1) operation and thus
# intentionally performed first.
# * This iteration-based assignment is an inefficient O(n) operation
# (where "n" is the number of annotated parameters and returns of that
# callable) and thus intentionally performed last here.
for arg_name, arg_hint in arg_name_to_hint.items():
func.__annotations__[arg_name] = arg_hint
Type Hint Deduplication: UHH. WUT.Most PEP 484-compliant type hints deduplicate (i.e., memoize) themselves, reducing space and time consumption. Since @beartype internally memoizes itself on the basis of type hints, deduplicated type hints improve the speed of @beartype: e.g., $ python3.8
>>> from typing import List
>>> List[str] is List[str]
True # <-- good. this is good.Most PEP 585-compliant type hints do not deduplicate themselves, increasing space and time consumption. For all the reasons that deduplication is good in the modern era, the lack of deduplication is likewise bad: e.g., $ python3.9
>>> list[str] is list[str]
False # <-- ...uhh, wut? i shake my fist at you, python 3.9!@beartype resolves this discrepancy between PEP 484 and 585 by forcefully deduplicating all PEP 585 type hints. Why? Because everybody wants deduplication. So, @beartype deduplicates on behalf of humanity. For example, if the
As expected, @beartype achieves this saintly goal by simply replacing each specific key-value of the But... yeah. It'd be kinda nice to continue to support both use cases in a backward-compatible manner – even if the internal CPython implementation required to do so is a little inefficient or cumbersome or whatevahs. But... yeah. You know best, Typing Master @JelleZijlstra. I leave this in your capable paws. Which leads us to...
|
|
Thank you! I'm going to have to think about whether we could make FORWARDREF the default; I do agree it's what many users should want. Regarding caching of generic aliases, there have been some attempts to change CPython's behavior there, but getting the caching right has been tricky because some users want union ordering to be preserved: python/cpython#101859 (comment). We may have to think more about how to achieve caching. |
|
We talked about this at the CPython core sprint and the consensus is that it's less surprising for users to keep VALUE the default. It keeps behavior that is more like what people are used to on previous Python versions. |
|
Guh! Users hate change, huh? It's undeniably true. Change is painful. Butterflies know this in their guts. But users are really gonna hate unexpected and unreadable PEP 649 may have bigger issues, though. These issues are mostly not of PEP 649's making. They live at the intersection between PEP 649 and other PEPs and static type-checkers and linters. This is actually why I haven't immediately replied. I wasn't ghosting you, @JelleZijlstra! I swears it. I was deeply cogitating about all this stuff. Without further ado... @leycec's Big List of PEP 649 MiseryYou've probably already hashed out all of this stuff with the other CPython devs – but a bulleted list never hurt no one:
And now we get to the real biggie. Everything above was just chump change. Because... Ruff Hates PEP 695. There, I Said It.If I asked you for the name of Python's most popular static linter or type-checker as of today, you might admit that "Okay. It's Ruff." But you'd probably prefer to suggest that it's ...yeah. It's Ruff. Mostly just because Ruff is (by far and away) the fastest static linter, mostly just because Ruff is implemented in Rust. This means that what Rust thinks about Python is now a Big Deal™. Unfortunately for us all, Rust hates PEP 695, PEP 563, and runtime # Instead of this:
from __future__ import annotations
from pathlib import Path
def func(path: Path) -> str:
return str(path)
# Use instead:
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
def func(path: Path) -> str:
return str(path)Rust calls this "error" and corresponding "advice"
Rust's def func(path: Path) -> str:
return str(path)That code now violates PEP 649 as well as PEP 563 and all runtime type-checkers and type hint introspectors. Why? Because PEP 649 coerces the Rust's Uhh... That Sounds Kinda Bad.It's kinda bed. This is why PEP 563 explicitly advises use of absolute rather than relative forward references to resolve circular imports: import typing
if typing.TYPE_CHECKING:
import expensive_mod
def a_func(arg: expensive_mod.SomeClass) -> None:
a_var: expensive_mod.SomeClass = arg
...For the same reason, # File foo.py:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import bar
def listify(arg: 'bar.BarClass') -> 'list[bar.BarClass]':
return [arg]
# File bar.py:
from foo import listify
class BarClass:
def listifyme(self) -> 'list[BarClass]':
return listify(self)@beartype loves and adores what PEP 563 and B-b-but Surely Nobody Is Doing What Ruff Is Saying!?Everybody is doing what Ruff is saying. Everybody includes:
So many downstream third-party packages are now doing what Ruff is saying that our upcoming @beartype 0.19.1 patch release internally blacklists both Pydantic and @beartype 0.19.1 resolves this discrepancy by pretending that Pydantic and @beartype 0.19.2 will tighten things up even further by:
@beartype Ain't Havin' None of It, RuffBut PEP 649 shouldn't have any of it either. It would probably be wise to get out ahead of this antipattern before it proliferates like a malignant tumour through the Python ecosystem any further. Somebody who is not me should probably consider adding a new subsection to PEP 649 entitled "Circular Imports" or "Import Cycles" or [whatevah]. Clarity and unambiguity would do wonders to putting this beast to rest. To preserve both backward compatibility and sanity going forward, PEP 649 should ideally:
I beg many things of you, @JelleZijlstra. But this, I beg of you the most. 🙇♂️ |
What happens in your example is that the assignment goes through fine, because the annotations are not evaluated when the assignment happens. When the module execution is done, the annotations are gathered up in an How does beartype check such assignments at runtime, while the module is executing? That might be a use case that is difficult to preserve, but I also don't see how you can currently do that without fairly invasive hacks.
Base classes are not annotations. Nothing is changing here.
The syntax is not changing here in Python 3.14. Forward references work, but they must be defined when you actually evaluate the type alias. However, in Python 3.14 we're planning to add a way to retrieve the value of a type alias in the annotations FORWARDREF format:
Where do you see that? I would recommend against looking too much at the PEPs only; the type system is now specified in https://typing.readthedocs.io/en/latest/spec/aliases.html#type-statement, which doesn't say anything about strings. Since the value of type aliases is lazily evaluated, you shouldn't need to use strings in them.
Ruff has a number of rules of questionable value, because it pulled in all the rules it could find from various popular(ish) flake8 plugins. That doesn't mean that everybody who is running Ruff enables all these rules. People who use runtime typing are obviously going to have a hard time if they use this TCH003 rule; that means they shouldn't use that rule if they care about runtime typing. |
|
Gah! So sorry for the extreme delay... and for shamefully breaking this PR. I knew I should have merged this immediately sight unseen. Instead, I was dumb. Now one of must suffer by manually resolving the growing laundry list of merge conflicts. Pain intensifies.
...heh. @beartype doesn't. That's just it. That's where all the cleverness and madness lies. Modern runtime type-checkers like @beartype and In @beartype's case, @beartype performs a preliminary phase of static type-checking on each module importation before CPython executes that module. How? Specifically, # PEP 526-compliant annotated variable assignments like this...
muh_attr: MuhTypeHint = muh_value
# ...into runtime type-checking validation like this.
from beartype.door import die_if_unbearable
muh_attr = muh_value
die_if_unbearable(muh_attr, MuhTypeHint)Works flawlessly without issue or edge cases in both @beartype and Most of us in this problem space are similar. The techbros who write fragile code? They're no longer with us. Their type-checkers were unmaintainable. They broke and nobody bothered fixing them. They died and then got replaced with type-checkers that actually worked. @beartype is hard enough to maintain without hacks. 😆
Hard disagree with that. The whole point of PEP 484-, 544-, and 585-compliant generics is to subclass a type hint, which typing systems then use to validate those generics. The hints that generics subclass absolutely satisfy the PEP 747-compliant For example: from collections.abc import Iterable, Container
class IterableContainer[T](Iterable[T], Container[T]): ...
Still, at least semantically, they're "base classes." When # This type hint...
IterableContainerStr = IterableContainer[str]
# ...is effectively equivalent to this type.
class IterableContainerStr(Iterable[str], Container[str]): ...All of the above only works because Anyway, arguments about semantics are entirely besides the point. The point is that real-world generics should be able to subclass type hints subscripted by forward references to types that are currently undefined: e.g., class IterableContainerStr(Iterable[WhatTheHeck], Container[WhatTheHeck]): ...
class WhatTheHeck(object): ...The Does PEP 649 have something to say about this? Probably not, huh? D users just have to continue manually stringifying forward references in generics? Is the following really the only way to reference undefined types in generics? class IterableContainerStr(Iterable['WhatTheHeck'], Container['WhatTheHeck']): ...
class WhatTheHeck(object): ...Kinda wierd that users can use unquoted forward references in some syntactic contexts but not in others. Ideally, users would be able to globally use unquoted forward references in all possible valid syntactic contexts for a type hint, right? The alternative is mass confusion and frustration. Oh, well. Guess we're going with mass confusion and frustration. 😮💨
So... what you're saying is that forward references don't work in For efficiency, @beartype dynamically generates type-checking code at decoration time. If a user annotates a function parameter with a Thankfully... annotationlib.call_evaluate_functions(Animals.__evaluate_value__, Format.FORWARDREF)Superb! That's exactly what @beartype needs. I mean, the syntax is pretty awful. It'd be nice if Still, I'll take it. @beartype takes whatever scraps it can get. 🥲
I mean, it's right there in PEP 695: # A type alias that includes a forward reference
type AnimalOrVegetable = Animal | "Vegetable"
It's like nobody even peer-reviewed that thing. I sigh.
Respectfully disagree. Standards matter. In fact, standards are pretty much the only thing that matter to @beartype. For type-checkers, everything else is just gossip and hearsay. We're here to enforce uniform standards – whatever those standards might be. All other documentation (although occasionally valuable) is ultimately pretty incidental to the rigorous work performed by type-checkers.
Under Python ≤ 3.13, Thankfully, that seems to be a moot point under Python ≥ 3.14. @beartype gives thanks and praise. 🙇 And... Now We're Both TiredThis is what happens when @leycec writes essays on a Friday night. Everyone ends up passed out in the futon in a puddle of their sputum. 🤤 🛌 |
|
...finally. With the recent release of @beartype Python 3.14 + PEP 649 is now up! Let us do this. Thanks so much yet again for this phenomenal pull request, @JelleZijlstra. I sincerely apologize for not merging this immediately. Since a giant pile of non-trivial merge conflicts has erupted, I'll now be merging this manually. I'm rolling up my sleeves. This is gonna get greasy. Without further ado, Leonardo DiCaprio will now announce what is about to happen. |
This commit is the first in a commit chain adding official support for both Python 3.14 *and* PEP 649- and 749-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit begins detecting `annotationlib.ForwardRef`-style forward references supported under Python ≥ 3.14. (*Libidinous dinosaurs do not have livid lipids!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649- and 749-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit negligibly revises docstrings to pretend that I actually did something meaningful with Monday evening rather than get repeatedly rickrolled by `conda-forge` (which is what happened instead). Conda-forge: This is why I don't make meaningful progress on a Monday. (*Trepidatious traps are mendacious, men!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649- and 749-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit begins generalizing our existing private `get_func_annotations()` getter to internally defer to the PEP 649-compliant `annotationslib.get_annotations()` getter. (*Grand AND, Mandy!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649- and 749-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit begins defining a new private `get_pep649_hintable_annotations()` getter internally deferring to the PEP 649-compliant `annotationslib.get_annotations()` getter. (*Rough stuff!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the first in a commit subchain refactoring the @beartype codebase to guaranteeably modify annotations only anatomically (i.e., all-at-once). Although @beartype sometimes modifies annotations anatomically, @beartype also occasionally modifies annotations iteratively in a piecemeal (i.e., individual type hint by individual type hint) fashion. PEP 649 supports the former but *not* the latter workflow. Consequently, this commit subchain refactors all instances of the latter into the former workflow. Doing so is non-trivial, requiring @beartype to store additional metadata context at decoration-time so as to reconstruct the desired new annotations atomically when finalizing the decoration of a callable or class. Brutal, in other words. Simply brutal. (*Brute lute!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a commit subchain defining a new private `get_pep649_hintable_annotations()` getter internally deferring to the PEP 649-compliant `annotationslib.get_annotations()` getter. (*Crude altitude atop a rude attitude!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a commit subchain defining a new private `get_pep649_hintable_annotations()` getter internally deferring to the PEP 649-compliant `annotationslib.get_annotations()` getter. (*Demonstrable monstrosity demons!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a commit subchain defining a new private `get_pep649_hintable_annotations()` getter internally deferring to the PEP 649-compliant `annotationslib.get_annotations()` getter. (*Rain of brainiacs or maniacal maniacs!?*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit defines a new private `clear_pep649_caches()` function clearing all internal data structures caching `__annotations__` dunder dictionaries returned by the probably notoriously slow PEP 649-compliant `annotationslib.get_annotations()` getter. (*Notorious toroids or not n-ary notaries!?*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the first in a subcommit chain generalizing the existing public `set_pep649_hintable_annotations()` function to support Python 3.14 and PEP 649. Needless to say, the road ahead remains long, dirty, thankless, and filled with potholes. (*Rowdy roadie!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a subcommit chain generalizing the existing public `set_pep649_hintable_annotations()` function to support Python 3.14 and PEP 649. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Wholly holes! Holy holly!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a subcommit chain generalizing the existing public `set_pep649_hintable_annotations()` function to support Python 3.14 and PEP 649. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Crustaceous mustiness ate curvaceousness!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a subcommit chain generalizing the existing public `set_pep649_hintable_annotations()` function to support Python 3.14 and PEP 649. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Act, rectilinear delicatessen in a delicate tesseract!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the last in a subcommit chain generalizing the existing public `set_pep649_hintable_annotations()` function to support Python 3.14 and PEP 649. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Phenomenal phenomenology!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the first in a subcommit chain exhaustively unit-testing both: * The PEP 649-compliant implementation of our private `beartype._util.hint.pep.proposal.pep649.set_pep649_hintable_annotations()` getter under Python >= 3.14. * The PEP 649-noncompliant implementation of that same getter under Python <= 3.13. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*The first thirst!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit documents significant improvements to the existing API and implementation of our private `beartype._util.hint.pep.proposal.pep649` submodule required to fully address the tangentially related feature request #534 (i.e., `beartype.door.is_subhint()` support for PEP 589-compliant `TypedDict` subclasses). Although seemingly unrelated, comparing `TypedDict` subclasses according to the subhint relation interestingly requires efficient memoized access to the PEP 649-compliant `Format.STRING` rendition of the annotations dictionaries for `TypedDict` subclasses containing one or more unquoted forward references under Python >= 3.14. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Diffuse suffusion diffidently suffices indifference!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the first in a commit subchain resolving internal issues in the existing API and implementation of our private `beartype._util.hint.pep.proposal.pep649` submodule documented by the 16th commit in this commit chain. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*No somber Novembers or novel members!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the next in a commit subchain resolving internal issues in the existing API and implementation of our private `beartype._util.hint.pep.proposal.pep649` submodule documented by the 16th commit in this commit chain. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Too rigid didgeridoo!*)
This commit is the next in a commit chain adding official support for both Python 3.14 *and* PEP 649-compliant unquoted forward references, en-route to manually merging long-standing pull request #440 by wondrous CPython core dev @JelleZijlstra (Jelle Zijlstra). Specifically, this commit is the last in a commit subchain resolving internal issues in the existing API and implementation of our private `beartype._util.hint.pep.proposal.pep649` submodule documented by the 16th commit in this commit chain. Needless to say, the road ahead remains long, dirty, thankless, and filled with QA potholes. (*Soothingly onward, wayward soothsayer!*)
|
Merged by 2b44dbf! Thanks to your astounding volunteerism (and gentle prodding in the back), @beartype now purportedly supports PEP 649. PEP 749 support has yet to happen – but hopefully will over the course of the summer. Which will be just in time for... Full-blown Python 3.14 support! That still has yet to happen, too. According to impressive Fedora dev @gotmax23 over at issue #537, the @beartype test suite currently has 80 failing tests under Python 3.14. Win some, lose some. Some GitHub commits are like that. 😂 Further Commentary: "Will @leycec not stop talking?"Because I am annoyingly loquacious, I have a bucket-load to say about all of this on the CPython issue tracker at some future date. Surprisingly, most of it is good. I have much praise. PEP 649 is a substantial real-world improvement over PEP 563, which was literally unusable at runtime. PEP 649 purports to work and then does actually work. I applaud both you and @larryhastings. 👏 👏 Not all that glitters is gold, though. I hit a few genuine bugs, performance regressions, and usability concerns with respect to PEP 649. I should document them before I forget them. Sadly, I am tired and so are you. Let us just close this and pretend I documented everything. Thanks yet again for being so forthright, @JelleZijlstra. This PR closure is for you. 👍 |
|
Thanks for your work! Please let us know on the CPython issue tracker if you'd like to see anything changed while 3.14 is still in beta. |
|
Ho, ho... That's a dangerous yet welcome request. I have much that is boring to say, yet sadly no time with which to bore you with it. Supporting Python 3.14 has proven... non-trivial. I tried to make @beartype robust against porting issues. Clearly, I failed. Once I've finally nailed Python 3.14 down to the ground, I'll happily chat with everybody on the CPython issue tracker. I suspect that some sort of PEP 849 follow-up to PEP 749 will probably be eventually required. There's a prominent edge case that PEP 563 supports that neither PEP 649 nor 749 appear to support. Authoring and implementing a new PEP probably can't happen by this October. So, my prevaricating and doing absolutely nothing is probably fine... probably. There sure is a lot of probably happening here, isn't there? Thanks again for your immense life-long work with Python. Have a great summer in Orinda, too. That place looks amazing. What a super-fun ecotourism place to live. 😄 |
Python 3.14's deferred annotation evaluation (PEP 649) causes `_Stringifier.__format__` to raise a TypeError when beartype inspects function signatures at test collection time. This is a workaround until beartype 0.23 is released with full Python 3.14 support. See: - beartype/beartype#594 - beartype/beartype#440 Co-Authored-By: Claude Opus 4.5 <[email protected]>
* Bump sphinx from 8.2.3 to 9.1.0 Bumps [sphinx](https://github.com/sphinx-doc/sphinx) from 8.2.3 to 9.1.0. - [Release notes](https://github.com/sphinx-doc/sphinx/releases) - [Changelog](https://github.com/sphinx-doc/sphinx/blob/master/CHANGES.rst) - [Commits](sphinx-doc/sphinx@v8.2.3...v9.1.0) --- updated-dependencies: - dependency-name: sphinx dependency-version: 9.1.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <[email protected]> * Bump doc8 * Support Python 3.14 * Don't bump Sphinx * Fix secrets file lookup for multi-Python matrix Use modulo arithmetic to map job index to secrets file index, ensuring that adding Python versions to the matrix doesn't break the secrets file lookup. Each test pattern maps to the same secrets file regardless of Python version. The number of secrets files is determined dynamically from the extracted tarball. * Support only 3.14 * Fix ruff * Revert "Fix ruff" This reverts commit 9a342f4. * Ignore ruff TC rules until beartype supports TYPE_CHECKING Beartype requires imports to be available at runtime, not just under `if TYPE_CHECKING`. See beartype/beartype#594 for when beartype 0.23 will add support for TYPE_CHECKING imports. Co-Authored-By: Claude Opus 4.5 <[email protected]> * Remove runtime beartype application to test functions Python 3.14's deferred annotation evaluation (PEP 649) causes `_Stringifier.__format__` to raise a TypeError when beartype inspects function signatures at test collection time. This is a workaround until beartype 0.23 is released with full Python 3.14 support. See: - beartype/beartype#594 - beartype/beartype#440 Co-Authored-By: Claude Opus 4.5 <[email protected]> * Drop pytest-beartype-tests due to Python 3.14 annotation issue * [pre-commit.ci lite] apply automatic fixes * Apply ruff format (PEP 758 except syntax) * Restore pytest-beartype-tests; disable plugin in repeated pytest.main calls * Ignore .claude/scheduled_tasks.lock * Add 'stringify' to spelling dict * Use real subprocess for pytest collection to avoid plugin state accumulation * Add new tests to CI matrix; exclude meta-tests from collected-once check * Restore Python 3.14 in test workflow matrix after merge * Use real subprocess in test_custom_linters to avoid beartype state accumulation * Add 'subprocess' to spelling dict * Re-apply subprocess collection: in-process approach fails on Python 3.14 full-suite runs * Bump pytest-beartype-tests to 2026.4.26; revert subprocess workaround in ci/test_custom_linters.py * Run coverage tool on Python 3.14 to parse PEP 758 except syntax * Set Python 3.14 via setup-uv python-version on coverage job * Drop comment on coverage job python-version --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>

It's still very early for Python 3.14, but I decided to try running
beartype's test suite to see how well it works with all the annotations
changes that have landed in CPython.
There were a few test failures for boring reasons (mostly, the fact
that typing.ForwardRef is now in the annotationlib module). I fixed
some but not all of those.
I also tried to make beartype work well with code that relies on
new PEP 649 functionality, such as:
from beartype import beartype
@beartype
def f(x: notyetdefined) -> notyetdefined:
return x
class notyetdefined:
pass
f(notyetdefined())
f(1) # error
To support such code, I made beartype use annotationlib.get_annotations()
with the FORWARDREF format, which appears to work fine.
I will leave this as a draft for now since it's still early in the PEP 649
stage, but I feel this is a promising indicator that it shouldn't be too
hard to adapt beartype to the PEP 649 world.