-
Notifications
You must be signed in to change notification settings - Fork 176
refactor: Replace ad-hoc dispatch with custom @singledispatch
#3410
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
base: dtypes/supertyping
Are you sure you want to change the base?
Conversation
This is more generally useful and a LOT easier to read from the outside
| # Default implementation serves as a fallback for subclasses of `upper_bound` | ||
| @just_dispatch(upper_bound=DType) | ||
| def dtype_repr_code(dtype: DType) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these comments are an attempt at a literate style 🙂
Initially I wanted to have some examples in the docstrings (in _dispatch.py), but:
- Anything non-trivial requires a very large docstring
- I couldn't think of anything simple that captures the strengths of the concept
| def register( # noqa: D417 | ||
| self, tp: type[Any], *tps: type[Any] | ||
| ) -> Callable[[Passthrough], Passthrough]: | ||
| """Register types to dispatch via the decorated function. | ||
|
|
||
| Arguments: | ||
| *tps: One or more **concrete** types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left out validating the types for now.
3.9:@functools.singledispatchsupportstype[Any]3.10: Same support, but that's the version which introducestypes.UnionType3.11:@functools.singledispatchsupportstyping.Union | types.UnionType | type[Any]
Since #3204 is not resolved, I'd rather leave this runtime check until then
…s/supertyping-dispatch
Description
Adds a slimmed-down version of
@functools.singledispatch, with the initial use replacing_supertyping._same_supertypein #3396.I can see a few other places (particularly
DType-related) that could later benefit.So, I went ahead and added more tests + docs than I would normally do for an internal tool.
Why not
@functools.singledispatch?Most of the stdlib code is dedicated to two features I don't want to use.
mro-based dispatch
That allows you to register
ABCs (likeMapping,Iterable, etc) or any regular class and have it's subclasses match.Definitely pretty clever stuff, but I'd prefer to have a simpler version that needs every class to be added explicitly
Registration via annotation
Forward references are resolved eagerly1 and only classes and unions of classes are supported.
IMO, that's just a complicated way of accepting
*types: type[Any]in the decorator - so I did that instead 😅Related issues
get_supertype#3396Footnotes
ibishas@lazy_singledispatchthat appears to solve this issue ↩