-
Notifications
You must be signed in to change notification settings - Fork 24
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
multimeta
-like metaclass for plum
#134
Comments
Hey @sylvorg! Thanks for opening an issue. :) This sounds very reasonable. I’m currently away, but should be back soon. I will get back to you some time next week. |
Got it; no worries, and I look forward to your return! |
Hey @sylvorg! I'm back again. I think such a metaclass could be useful and would be a very sensible addition to the library. :) Unfortunately, I don't have the capacity to do this myself on the short term. I am a little overloaded at the moment. :( However, if you would like to have a stab at this, contributions are very welcome! Otherwise, I'll put it on the TODO list and will implement this at a later point in time. |
I can probably put together a PR, but I'm not exactly sure where to put the class; should I just put it in |
@sylvorg, very sorry for the super late reply. Work has been very busy, meaning that I have less-than-usual capacity for side projects. :( I think a separate file would make sense. Perhaps we can call the class
A clear remark in the docstring would suffice, I think. Pinging them in this issue might be nice too. :) |
I'll ask @coady now; hopefully they aren't too irritated by this request, considering I disturbed them quite a bit before coming here! 😅 How does something like the following look? from collections import namedtuple
from plum import Dispatcher
Plume = namedtuple("Plume", "args,kwargs")
def plume(*args, **kwargs):
def wrapper(func):
func.__plume__ = Plume(args, kwargs)
return func
return wrapper
# Adapted From: https://github.com/coady/multimethod/blob/main/multimethod/__init__.py#L488-L498
class DispatchMeta(type):
"""Convert all callables in namespace to Dispatchers."""
class __prepare__(dict):
def __init__(self, *args):
self.__dispatcher__ = Dispatcher()
super().__setitem__("__dispatcher__", self.__dispatcher__)
def __setitem__(self, key, value):
if callable(value):
args, kwargs = getattr(value, "__plume__", (tuple(), dict()))
if not kwargs.get("disabled", False):
value = getattr(self.get(key), "dispatch", self.__dispatcher__)(
value, *args, **kwargs
)
super().__setitem__(key, value) The |
@sylvorg I think that looks great!! What would you think of something like this: from plum import Dispatcher, configure_dispatch
dispatch = Dispatcher()
class MyClass(metaclass=dispatch.meta):
@configure_dispatch(precedence=1)
def method(self, x):
return x Here |
Oh, y'all are no fun. 😹 I love the |
@sylvorg, haha feel free to export an alias
Maybe use a class Dispatch:
...
@property
def meta(self):
class DispatchMeta(type):
...
return meta How would that look?
Hmm, I do agree, though the user can always do
Ah, this is clever! How about we get rid of from plum import Dispatcher, configure_dispatch
dispatch = Dispatcher()
class MyClass(metaclass=dispatch.meta):
@dispatch(precedence=1)
def method(self, x):
return x Then, in the metaclass, we can check whether the function is already dispatched (in that case it's an instance of |
Got it on the class creation! When checking a function in the metaclass, then, should we just ignore the already dispatched methods, since they've already been added to the specified dispatcher? |
Also, I have a very special request to use a different base class for the metaclass if needed; there is a package which provides a metaclass for automatically creating slots from the |
Yes, that sounds like a good approach. :)
We could do something like this: from plum import Dispatcher, configure_dispatch
dispatch = Dispatcher()
class MyClass(metaclass=dispatch.meta(ExistingMetaClass)):
@dispatch(precedence=1)
def method(self, x):
return x where |
Hello!
Would it be possible to add a
multimeta
-like metaclass for plum, pulled directly from the multimethod source code?I have a working example below:
Of course, I'm assuming there are other tests I'd have to run to ensure the same result, but otherwise, would this work?
Thank you kindly for your consideration on the matter!
The text was updated successfully, but these errors were encountered: