Skip to content
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

Exclude classes in create_tranformer by user provided pedicate #1378

Open
vityaman opened this issue Dec 10, 2023 · 0 comments
Open

Exclude classes in create_tranformer by user provided pedicate #1378

vityaman opened this issue Dec 10, 2023 · 0 comments

Comments

@vityaman
Copy link

Suggestion

I suggest to add an option for user to exclude classes from being handled by transformer produced by ast_utils.create_transformer not only by underscoring class name, but with custom predicate.
Here in lark/ast_utils.py we have hardcoded condition for exclusion.

Why

It seems to me that its quite unpleasant to do such things as we may wish to use such underscored classes as normal in our program, so why we has to make their name ugly in some way.

I faced with this fact during the development of such AST hierarchy:
https://github.com/vityaman-edu/sleepy/pull/8/files#diff-62348556570d26da9f0e70903e81ee752f97a3ea6cf48bbfe9c138f080a8d329R15

There I don't really want to have names like _Expression, so my only option is to do something like

Expression = _Expression
Expression.__name__ = "Expression"

after calling to create_transformer. Other way is converting this like LarkAST to MyOwnAST after parsing looks like overengineering. But it seems to me that the solution of this problem is nearly trivial.

Proposal

My proposal is

  1. Add another parameter predicate excluded to create_transformer. Then the signature would be
def create_transformer(
  ast_module: types.ModuleType,
  transformer: Optional[Transformer]=None,
  decorator_factory: Callable=v_args,
  excluded: Callable[[str], bool]=lambda name: name.startswith('_'),
) -> Transformer:

Then add it to that condition.

for name, obj in inspect.getmembers(ast_module):
  if not name.startswith('_') and not excluded(name) and inspect.isclass(obj):
    if issubclass(obj, Ast):

I left not name.startswith('_') to ensure that the statement "Classes starting with an underscore (_) will be skipped." remains true in general. But maybe it can be removed, so it needs to be discussed.

Alternatives

  • @ignored annotaion.
  • Ignored tag interface like AsList or WithMeta.

User effect

  • Performance penalty for using lambda (?).
  • More complex interface.

Actions

  1. Edit ast_utils.py:create_transformer, update docstring.
  2. Update project docs?
  3. Make an MR linked to this issue.

I can make an MR and solve this issue myself if you let me.

Discuss

  • Parameter name or alternative way to provide such functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant