Skip to content

Better transformer API#489

Draft
tilk wants to merge 6 commits intomasterfrom
tilk/better-transformers
Draft

Better transformer API#489
tilk wants to merge 6 commits intomasterfrom
tilk/better-transformers

Conversation

@tilk
Copy link
Copy Markdown
Member

@tilk tilk commented Oct 25, 2023

This PR utilizes def_helper mechanisms to create a nicer API for using transformers. Following changes in interpreting the function arguments of the transformer classes are made:

  • The module parameter is now optional.
  • Instead of taking a record of method inputs (arg) the functions can take individual record fields, just like methods defined via def_method.

Also, specializations of def_helper were moved closer to their use sites, so that the dependencies on types would resolve correctly. If someone has a better idea, I'd like to hear it.

To do:

  • Update docstrings.

@tilk tilk added the refactor Doesn't change functionality, but makes stuff nicer label Oct 25, 2023
Copy link
Copy Markdown
Contributor

@lekcyjna123 lekcyjna123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have mixed feelings. I like the simplicity of interface and the reduction of boilerplate. But we are loosing consistency. Earlier everywhere first argument had to be TModule. Now in transformer API it wouldn't be needed, but e.g. in method calls it will be still required. Nevertheless I am ready to approve such changes.

Comment thread transactron/_utils.py
return func # type: ignore


def def_helper(description, func: Callable[..., T], tp: type[U], arg: U, /, **kwargs) -> T:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be func annotated as CallableOptParam? I have the same question regarding mock_def_helper and method_def_helper.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but it basically changes nothing because of the ... parameter specification.

@tilk
Copy link
Copy Markdown
Member Author

tilk commented Oct 29, 2023

But we are loosing consistency.

I think we are actually gaining consistency, as:

  • Keyword arguments allows to define functions just like method definitions.
  • The filter function can now be defined consistently with your validate_arguments feature. The validating function cannot get a TModule argument, because it's called outside of the transactional circuit (in the transaction scheduler).

Also:

  • The TModule argument was basically a hack to allow the use of methods as functions. I expect this argument will not be needed in most use cases.
  • The presence or lack of the TModule argument actually signals something meaningful: it says that the function can do method calls in the context of the transformer. (There is unfortunately no way to guarantee "purity", as a function might use an external TModule to do some stuff. I sure hope this use will be avoided though.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Doesn't change functionality, but makes stuff nicer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants