Open leycec opened 2 years ago
Thanks! Yes indeed @beartype is part of the plan, also for the O(1)-ness. I'll get on this just after I get the jaxtyping loveliness which triggered all this..
And of course I'm totally happy if this ends up in @beartype :)
Wunderbar! Take your languorous time and soak up those last rays of summer. They are precious – especially in Canada, where summer is a distant phenomena that mostly happens to other people.
And thanks for being so shockingly inventive. I never even knew about the visit_AnnAssign()
method – which, in hindsight, kinda seems like it exists for the express purpose of supporting PEP 526 at runtime. We now give thanks. \o/
Extremely impressive.
@awfutils.typecheck
is the first practical attempt I've seen at performing static type-checking at runtime. Take all my thunderous clapping! :clap: :clap:The current approach is outrageously useful, but appears to currently only support
isinstance()
-able classes rather than PEP-compliant type hints: e.g.,Is that right? If so, that's still impressive tech for a several hundred-line decorator. Still, it'd be even more outrageously useful if we could generalize your
@typecheck
decorator to support arbitrary PEP-compliant type hints. Can we? Yes, we can.You Know What @leycec Is Gonna Suggest Next...
That's right. We're talkin' @beartype, because of course we are. Implementing full-blown type-checking for all PEP standards past and present probably isn't somewhere you want to willingly go. Thankfully, @beartype already went there for you. :partying_face:
The core issue appears to be the current usage of the
isinstance()
builtin in theTypeCheckVisitor.visit_FunctionDef()
method. Specifically, this AST transform:I'm fairly certain (...but technically uncertain, because lazy and thus untested) that replacing the above with the below should generalize
@typecheck
to support PEP-compliant type hints:The
visit_Module()
implementation is copied almost verbatim from a similar AST transform in the@beartype
codebase itself. So, possibly working?Regardless of where you choose to take this awesomeness, this has been a considerable inspiration.
@beartype
will probably end up assimilating this into itself, because everyone over at beartype/beartype#105 really wants this to happen.In short, you're amazing.