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

Allow components to have __init__ functions #327

Closed
wants to merge 3 commits into from
Closed

Allow components to have __init__ functions #327

wants to merge 3 commits into from

Conversation

jneeven
Copy link
Contributor

@jneeven jneeven commented Jul 15, 2022

Currently, zookeeper will throw an error if a component defines an __init__. This is annoying, because it makes it impossible to use zookeeper components that subclass existing classes that have one, e.g. tf.keras.models.Model.

This PR changes that by:

  • Allowing the user to define an __init__ function, which will be called directly after configuration. This makes its behavior identical to __post_configure__, which is therefore deprecated. Neither function supports any arguments.
  • Not allowing the user to access any of the fields on the component before it has been configured. This closes Throw error if component has __post_configure__ but is not configured #280. The reason I've implemented this is that it would be very easy for bugs to appear if the user expects their __init__ to have run, but the component was never configured. This means that users are now effectively forced to configure any and all zookeeper components they use, which may be overly strict in case all Fields already have default values. Before making this an official release, we could therefore consider automatically calling configure on a component if all its fields have default values. This is tricky, however, because the user may then still configure it from the command line, and we wouldn't want the init functions to be called multiple times.

@jneeven jneeven requested a review from a team July 15, 2022 15:21
@jneeven jneeven added the breaking-change Changes that will break user code label Jul 18, 2022
@jneeven
Copy link
Contributor Author

jneeven commented Jul 18, 2022

Unit tests were failing because my context manager did not correctly handle exceptions, and some of the tests expect one (using pytest.raises. I've fixed this, let's see what happens.

@jneeven
Copy link
Contributor Author

jneeven commented Jul 18, 2022

Looking at this code again, I think it would actually be cleaner to make the configuration_mode context manager modify a flag on a specific component instance rather than globally.

Copy link

@DzedCPT DzedCPT left a comment

Choose a reason for hiding this comment

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

Take my approval with a pinch of salt because I have only just kinda figured out how zookeeper works by looking at the code in this PR. But changes look good to me, assuming you decide to ignore my comment about using __init__ as a post_configure method being strange because I understand changing that would be a pain.

zookeeper/core/component.py Show resolved Hide resolved
zookeeper/core/component.py Show resolved Hide resolved
zookeeper/core/component.py Show resolved Hide resolved
zookeeper/core/component.py Show resolved Hide resolved
@DzedCPT
Copy link

DzedCPT commented Jul 19, 2022

Looking at this code again, I think it would actually be cleaner to make the configuration_mode context manager modify a flag on a specific component instance rather than globally.

I agree. If it's easy to do.

@lgeiger lgeiger self-requested a review July 19, 2022 15:38
@jneeven jneeven marked this pull request as draft July 22, 2022 08:40
@jneeven jneeven closed this by deleting the head repository Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Changes that will break user code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Throw error if component has __post_configure__ but is not configured
2 participants