lark icon indicating copy to clipboard operation
lark copied to clipboard

Alternative to @v_args()

Open makukha opened this issue 1 year ago • 2 comments

Suggestion

  1. Use explicitly named decorators: @wrap_every (class level only), @inline_children (method level only)
  2. Let Transformer decide which arguments to pass based on method signature:
    • If method has single argument, pass children regardless of its name (this will preserve backwards compatibility)
    • If args are subset of {meta, tree, children} — pass respective values.
  3. v_args is left unchanged for backwards compatibility.

This may make API more explicit and sound. The second approach is used in e.g. Pydantic (see validators in v.1).

I'm not aware of all possible use cases for v_args, but at least some of them are covered.

Describe alternatives you've considered @v_args is the only alternative.

Additional context Usage examples:

Class level only @wrap_every

@wrap_every(inline_children)
class Demo(Transformer):
    def rule(self, *items): ...

Method level only @inline_children

class Demo(Transformer):
    def rule1(self, children): ...
    @inline_children
    def rule2(self, *items): ...

Method signature auto detection

class Demo(Transformer):
    def rule1(self, items): ...  # children passed
    def rule2(self, tree): ...  # tree passed
    def rule3(self, meta, children): ...  # pass meta, children
    def rule4(self, meta, items): ...  # to be decided: raise error or pass children to items
    @inline_children
    def rule5(self, tree, meta, *items): ...

If this change sounds reasonable, I'm ready to work on PR.

makukha avatar Sep 12 '24 16:09 makukha