Skip to content

Commit d73c381

Browse files
committed
docs: Fix _dispatch.py
1 parent a3016e6 commit d73c381

2 files changed

Lines changed: 50 additions & 46 deletions

File tree

docs/api-reference-plan/ir/misc.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ somewhere less private
4747
members:
4848
- FunctionFlags
4949

50-
<!--
51-
TODO @dangotbanned: Fix formatting
52-
TODO @dangotbanned: Docs contain an idea for rewriting them ffs
53-
-->
5450

5551
::: narwhals._plan._dispatch
5652
options:

src/narwhals/_plan/_dispatch.py

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,29 @@ def name(self) -> str:
105105
"""Compliant-level method name.
106106
107107
They're often the lowercase transform of the class name:
108-
>>> import narwhals as nw
109-
>>> import narwhals._plan as nwp
110-
>>> from narwhals._plan import expressions as ir
111-
>>> expr = nwp.col("a").cast(nw.Int64())._ir
112-
>>> type(expr).__name__, get_dispatch_name(expr)
113-
('Cast', 'cast')
108+
109+
>>> import narwhals as nw
110+
>>> import narwhals._plan as nwp
111+
>>> from narwhals._plan import expressions as ir
112+
>>> expr = nwp.col("a").cast(nw.Int64())._ir
113+
>>> type(expr).__name__, get_dispatch_name(expr)
114+
('Cast', 'cast')
114115
115116
*PascalCase* becomes *snake_case*:
116-
>>> expr = nwp.col("a").over(order_by="b")._ir
117-
>>> type(expr).__name__, get_dispatch_name(expr)
118-
('OverOrdered', 'over_ordered')
117+
118+
>>> expr = nwp.col("a").over(order_by="b")._ir
119+
>>> type(expr).__name__, get_dispatch_name(expr)
120+
('OverOrdered', 'over_ordered')
119121
120122
Accessor methods reflect the full dotted path:
121-
>>> ir.lists.NUnique.__expr_ir_dispatch__.name
122-
'list.n_unique'
123+
124+
>>> ir.lists.NUnique.__expr_ir_dispatch__.name
125+
'list.n_unique'
123126
124127
For functions, generated names can be overridden at class definition time:
125-
>>> ir.boolean.Not.__expr_ir_dispatch__.name
126-
'not_'
128+
129+
>>> ir.boolean.Not.__expr_ir_dispatch__.name
130+
'not_'
127131
"""
128132
return self._name
129133

@@ -195,7 +199,8 @@ def options(self) -> DispatcherOptions:
195199
When a `Function` is subclassed, this property is inherited and reused in a new
196200
`FunctionDispatch` instance.
197201
198-
See `DispatcherOptions` for examples.
202+
See Also:
203+
[`DispatcherOptions`][narwhals._plan._dispatch.DispatcherOptions]
199204
"""
200205
return self._options
201206

@@ -365,40 +370,43 @@ class DispatcherOptions:
365370
Defined via the (optional) `dispatch` parameter at [subclass-definition time].
366371
367372
Many functions simply use the default:
368-
>>> from narwhals._plan import expressions as ir
369-
>>> from narwhals._plan._dispatch import DispatcherOptions
370-
>>> from narwhals._plan.options import ExplodeOptions
371-
>>>
372-
>>> class Explode(ir.Function): ...
373373
374-
>>> Explode.__expr_ir_dispatch__
375-
Dispatch<explode>
374+
>>> from narwhals._plan import expressions as ir
375+
>>> from narwhals._plan._dispatch import DispatcherOptions
376+
>>> from narwhals._plan.options import ExplodeOptions
377+
>>>
378+
>>> class Explode(ir.Function): ...
379+
380+
>>> Explode.__expr_ir_dispatch__
381+
Dispatch<explode>
376382
377-
>>> Explode.__expr_ir_dispatch__.options
378-
DispatcherOptions(<default>)
383+
>>> Explode.__expr_ir_dispatch__.options
384+
DispatcherOptions(<default>)
379385
380386
`dispatch` provides a bit more flexibility when you want it:
381387
382-
>>> class Explode2(Explode, dispatch=DispatcherOptions.renamed("explodier")): ...
388+
>>> class Explode2(Explode, dispatch=DispatcherOptions.renamed("explodier")): ...
383389
384-
>>> Explode2.__expr_ir_dispatch__
385-
Dispatch<explodier>
390+
>>> Explode2.__expr_ir_dispatch__
391+
Dispatch<explodier>
386392
387-
>>> Explode2.__expr_ir_dispatch__.options
388-
DispatcherOptions(override_name='explodier')
393+
>>> Explode2.__expr_ir_dispatch__.options
394+
DispatcherOptions(override_name='explodier')
389395
390396
Keep in mind that `options` are inherited:
391-
>>> class Explode21(Explode2): ...
392-
>>> Explode21.__expr_ir_dispatch__
393-
Dispatch<explodier>
397+
398+
>>> class Explode21(Explode2): ...
399+
>>> Explode21.__expr_ir_dispatch__
400+
Dispatch<explodier>
394401
395402
So we'd need another override to get the default back:
396-
>>> class ExplodeWithOptions(Explode2, dispatch=DispatcherOptions()):
397-
... __slots__ = ("options",)
398-
... options: ExplodeOptions
399403
400-
>>> ExplodeWithOptions.__expr_ir_dispatch__
401-
Dispatch<explode_with_options>
404+
>>> class ExplodeWithOptions(Explode2, dispatch=DispatcherOptions()):
405+
... __slots__ = ("options",)
406+
... options: ExplodeOptions
407+
408+
>>> ExplodeWithOptions.__expr_ir_dispatch__
409+
Dispatch<explode_with_options>
402410
403411
[subclass-definition time]: https://docs.python.org/3/reference/datamodel.html#object.__init_subclass__
404412
"""
@@ -474,12 +482,12 @@ def merge_with(self, child: DispatcherOptions | None) -> DispatcherOptions:
474482
If an `accessor_name` was defined, it is **sticky** and will be merged
475483
with options of `child`:
476484
477-
>>> from narwhals._plan._function import Function
478-
>>> options = DispatcherOptions
479-
>>> class StringFunction(Function, dispatch=options(accessor_name="str")): ...
480-
>>> class ZFill(StringFunction, dispatch=options.renamed("zfill")): ...
481-
>>> get_dispatch_name(ZFill)
482-
'str.zfill'
485+
>>> from narwhals._plan._function import Function
486+
>>> options = DispatcherOptions
487+
>>> class StringFunction(Function, dispatch=options(accessor_name="str")): ...
488+
>>> class ZFill(StringFunction, dispatch=options.renamed("zfill")): ...
489+
>>> get_dispatch_name(ZFill)
490+
'str.zfill'
483491
"""
484492
options = child or self
485493
if accessor_name := (self.accessor_name or options.accessor_name):

0 commit comments

Comments
 (0)