refactor: Replace ad-hoc dispatch with custom @singledispatch#3410
refactor: Replace ad-hoc dispatch with custom @singledispatch#3410dangotbanned wants to merge 22 commits intodtypes/supertypingfrom
@singledispatch#3410Conversation
This is more generally useful and a LOT easier to read from the outside
tests/dispatch_test.py
Outdated
| # 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.
All of these comments are were 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
narwhals/_dispatch.py
Outdated
| 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.
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
…s/supertyping-dispatch
…s/supertyping-dispatch
|
@FBruzzesi
I think this was a documentation issue on my part. Do you still have the opinion that (https://github.com/narwhals-dev/narwhals/blob/308f389f2e68079053e9c659394cdd0d4207410d/narwhals/_dispatch.py) is intense to maintain?
I thought replacing lots of other non-supertyping stuff wouldn't be a good idea in this PR, since it was based on Here's three unrelated use-cases (dtypes/supertyping-dispatch...@just_dispatch-use-cases).
I do feel the need to stress that these are not the only places that can use it! 😅 |
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 ↩