-
Notifications
You must be signed in to change notification settings - Fork 25
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
Plum 2 #68
Comments
Awesome! I'll have to go through a bit more later As for promotion, isn't that just a baked-in dispatched function? I was checking the code for hard dependencies on a So theoretically, all of the tree search stuff should still apply? Plum provides some decent default promotions, which the underlying dispatcher can do a lookup on, and users can extend with their own types. We don't expect a situation where the promotion function should know what to do with a type that neither the devs or the users have told it about... right? |
Yes, I would agree! My comment in the initial post is more a practical one. Namely, currently the code relies on a function
Yeah, I think you're right! |
Brilliance personified! As an addendum to #53, I hereby promise that I'll try to remember to post an update here when We now return to your regularly scheduled brilliance. I'm so impressed my mouth is agape. 😮 |
Oh hell yeah, that would be absolutely fantastic! As always, @leycec, thank you so much for your time and effort spent on @beartype. @beartype is making everyone's lifes nicer. :)
Will do! 🍅 |
If I may add something to the whishlist of v2.0 of this amazing project, would be to be able to use automated documentaton (either via sphinx or mkdocstrings). I have been experimenting a bit bit haven't found a way that doesn't seem like an hack (and maybe there is not one?) |
@astanziola I fully agree that making multiple function definitions play nicely with automated documentation is a very necessary feature that's been lacking for a long time. I currently have something in the works that functions in the following way. Consider the following file: from numbers import Number
from plum import dispatch
@dispatch.abstract
def f(x: Number, y: Number):
"""Multiply two numbers."""
@dispatch
def f(x: float, y: float):
"""Multiply two floats."""
return x * y
@dispatch
def f(x: int, y: int):
"""Multiply two ints."""
return x * y Then calling
The result for automated documentation would be similar. I agree that this isn't ideal, because the other function definitions are embedded as text in the docstring, but at least all methods appear in the documentation. A much nicer solution, of course, would be to write a plugin for Sphinx that produces a separate entry for every method of a function. Baby steps first I suppose 😅 |
Yes, and probably it makes more sense to develop it separately from At some point I wrote this to work with mkdocs and plum and at least shows the methods, but is one piece of code that I'm particoularly not proud of (it is really, really hacky) and I don't know much about the bits underlying automatic documentation to do it properly. Anyhow, this is probably not the best place to talk about this, I just wanted to raise the issue :) |
Thought I'd add my 2c here... I'm pretty sure this might be somewhat addressed by the various docstring groups already, via I think, if Plum 2 can somehow automatically imply that calling |
@beartype crew: combine all your powers! There are three API generators of general interest in Python. Technically, there are more (e.g., Twisted's
from typing import TYPE_CHECKING, overload
# If somebody is statically analyzing us, reduce Plum's
# @dispatch decorator to the standard @overload decorator.
if TYPE_CHECKING:
dispatch = overload
# Else, somebody is running us at runtime. In this case,
# define Plum's @dispatch decorator in the standard way.
else:
from collections.abc import Callable
from typing import TypeVar
T = TypeVar('T', bound=Callable)
def dispatch(T) -> T: ... If either |
Ahhh, this is nice! Even though it is hacky, the result looks nice and it would be exactly what we're looking for.
Thanks to you for raising the issue! :) You're totally right, and IMO this is something that should be addressed on the short term, if at all possible. @tbsexton @leycec
Ahhh, this is very clever! I've yet to try this, but when I get around to freeing up some time to more closely look into the documentation isssue, this will be the first item on my to-try list. :) It would be very nice if there were a solution that doesn't require writing plugins. |
What is This?
This issue proposes possible major improvements for a version two of the package. The goal is to start a discussion around these proposals. If you agree or disagree with some or all of changes, or think that other improvements are missing, then please do share your thoughts by commenting below. :) Tagging @PhilipVinc, @tbsexton, @leycec, and @seeM here since they have been involved in recent relevant discussions.
None of the proposed changes should change current behaviour, thus maintaining backwards compatibility. I'll be continuously updating and adding to this issue over time.
The list of proposed improvements is as follows:
Instance-Check-Based Dispatch
Instead of performing dispatch based on the types of the arguments, perform dispatch entirely using
isinstance
, hence fully eliminating the need to usetype_of
. See this proposal by @tbsexton. The massive benefit of doing things this way is it becomes much easier to support types likeLiteral
s (#66),Protocol
s (#28),numpy
arrays with a specific number of dimensions (#10), andPyTree
s (Stheno #21).The big downside of this change is that performance will take a hit, since instance checks will need to happen every time dispatch happens. However, by using @beartype (@leycec), instance checks will be blazingly fast, so the performance hit should be minimal. Moreover, caching is still possible for functions with a so-called faithful dispatch tree (see here), so we should get the best of both worlds!
Although dispatch will not rely on
type_of
anymore, other functionality of the package still does, such as promotion. Hence, atype_of
function is still necessary.Postponed Evaluation of Types
@beartype now includes
beartype.peps.resolve_pep538()
, which should handle PEP538-style postponed types for us. A massive thanks to @leycec for this!! This allows us to get rid of all use ofinspect
, which I believe currently is causing performance issues (Stheno #25). Deferring the responsibility of handling postponed evaluation of types to @beartype should significantly simplify the internals of the package.Wrapping of Types
Internally Plum currently wraps types in its own types. We should get rid of those entirely and use types from
typing
. This does pose a challenge for some more advanced custom types, such asPromisedType
.Compatibility with Modern Tooling
Whatever Plum does should be compatible with modern tooling. Think
mypy
,pylance
(#51), and possible others.The text was updated successfully, but these errors were encountered: