-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is the protocol caching still needed? #20
Comments
Thanks, @jorenham! As one might surmise, updates from the various metropoli of performant runtime type checking are slow to reach these backwaters of Appalachia, so we very much appreciate it when kind travelers bring news as they pass through. And please do not worry yourself on the matter of provoking offense. I try to reserve that for much more serious matters which largely but tragically not absolutely exist outside of software development. And now for something
|
Wow, that must be the best response I've ever gotten; you have my thanks 🏆 .
This is my experience as well: Seemingly simple things like binary operators, or iterations, are ridiculously to correctly type, often resulting in a mess of type aliases, protocols, overloads, typeshed PR's that get rejected for fallacious reasons, and angry neighbors telling you to stop screaming at your screen at 4 a.m...
I think that I have encountered this buggy endboss a couple of times as well. It's one of their special attacks that gets me, usually its either "dogma beam" or "fallacy cannon", or when they're at 1HP, they exploit a glitch that replaces them with an another, almost identical, boss.
Yes, it is! Plus, there are more that one type of number; e.g. Relying on typeshed alone won't do you much good either, as they're just typed versions of the runtime.
How about something like this? from __future__ import annotations
from typing import Any, Generic, TypeVar, overload
from optype import CanMul, CanRMul
_T_co = TypeVar('_T_co', covariant=True)
_R = TypeVar('_R')
_RHS = TypeVar('_RHS')
class SomeThingThatDoesMathLikeThingsWithOtherThings(Generic[_T_co]):
def __init__(self, thing: _T_co) -> None:
self._thing = thing
@overload
def do_the_multiplication_thing(
self,
other: CanRMul[_T_co, _R],
) -> _R: ...
@overload
def do_the_multiplication_thing(
self,
other: CanMul[_T_co, _R],
) -> _R: ...
@overload
def do_the_multiplication_thing(
self: SomeThingThatDoesMathLikeThingsWithOtherThings[CanMul[_RHS, _R]],
other: _RHS,
) -> _R: ...
def do_the_multiplication_thing(
self,
other: CanRMul[_T_co, _R] | CanMul[_T_co, _R] | Any,
) -> _R:
if isinstance(other, CanRMul):
return self._thing * other
if isinstance(other, CanMul):
return other * self._thing
return self._thing * other
Purely in terms of So consider the following standard boilerplate code for some regular business application: from typing import SupportsIndex, Self
class SomethingPositiveYouCanCountOn:
"""
>>> _ = SomethingPositiveYouCanCountOn()
>>> +_
+_
>>> ++++_
++++_
"""
def __init__(self, /, *, _start: SupportsIndex = 0) -> None:
self._value = int(_start)
def __pos__(self) -> Self:
return type(self)(_start=self._value + 1)
def __repr__(self) -> str:
return '+' * self._value + '_'
class SomethingThatLooksPositiveButIsNot:
__pos__ = NotImplemented How can we want to predict whether some Runtime-protocols are the way to go here: >>> from optype import CanPos
>>> isinstance(SomethingPositiveYouCanCountOn(), CanPos)
True Now let's check >>> isinstance(SomethingThatLooksPositiveButIsNot(), CanPos)
True Ouch. So let's check what every 3-year old python developer knows: that even if >>> isinstance(SomethingThatLooksPositiveButIsNot, CanPos)
True So yes, a better |
So this was kind of rad (cc @leycec; hat tip to @AlexWaygood for the
I also missed this:
If I'm reading that right, using However, based on your examples, @jorenham, (which are unsurprising to those who know how I'll try to tinker some more once I can carve out some time. |
Yeah, would definitely encourage y'all to do detailed perf testing before making any decisions. We made a ton of optimisations to
There's a benchmark script in the PR description for python/cpython#103280 (comment) that you might find useful |
Since Lithuanian breakdancer Dominika Banevič just got robbed in the B-Girls Olympics gold medal Breaking battle, I must now console myself with some fast-acting GitHubbing. I drain my sorrows in keyboard warrioring. Thanks so much, @AlexWaygood! That PR benchmark script is especially awesome. If CPython's official
Will @leycec actually do something? Will @posita save the bitter day? Stay tuned as we all just watch more caustic Olympics. |
Since Python 3.12 runtime-checkable
typing.Protocol
's use caching and some other tricks to achieve a ~20x speedup: python/cpython#102433For older Python versions,
typing-extensions
provides aProtocol
variant that includes (most of) these speedups (docs).For what it's worth: It is used in the
optype
library, which I use for several of my (other) open- and closed-source projects, but I haven't had any problems with it at all (even though I tend to break everything typing related (not a threat BTW)) 🤷🏻.I haven't done a benchmark, but regardless of the outcome, this could perhaps be a nice opportunity to make
numerary
more focused towards what all the cool kids are talking about these days: typing in Python 😎. Even fusion-powered quantum-blockchain AI has got nothing on us!~
Hoping this banal participial closing causes no offense, I am,
Typically,
Joren S. Hammudoglu,
Typer of Things
The text was updated successfully, but these errors were encountered: