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

Better transformer API #489

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Better transformer API #489

wants to merge 6 commits into from

Conversation

tilk
Copy link
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
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.

else:
return func # type: ignore


def def_helper(description, func: Callable[..., T], tp: type[U], arg: U, /, **kwargs) -> T:
Copy link
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
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
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