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

fix(components): make all possible attributes private #162

Merged
merged 2 commits into from
Nov 11, 2024

Conversation

domire8
Copy link
Member

@domire8 domire8 commented Oct 28, 2024

Description

This PR improves the robustness of the Python component interfaces. It is possible that a user overwrites some of these attributes and completely breaks the components (for example self._period = 0.0 already completely breaks a components).

In this PR I made all possible attributes of components "private" and the necessary "protected" ones a bit more verbose to minimize chances of overriding. Two main disadvantages:

  1. the lifecycle component now also needs to keep track of outputs, its kind of a duplicated dict but the only way to avoid having the _outputs "only protected"
  2. A lot of the tests would have to disappear since they rely heavily on the fact that we can do stuff like assert "test" in component._outputs.keys()

What do you think? A valuable improvement or not?

Review guidelines

Estimated Time of Review: x minutes

Checklist before merging:

  • Confirm that the relevant changelog(s) are up-to-date in case of any user-facing changes

@eeberhard
Copy link
Member

Overall I think it's a reasonable change, but I don't really like the behavior of the lifecycle component to keep an associated map of outputs. It's quite confusing and that makes it seem brittle and hard to maintain.

the lifecycle component now also needs to keep track of outputs, its kind of a duplicated dict but the only way to avoid having the _outputs "only protected"

A lot of the tests would have to disappear

Not entirely true on both counts, as you could access them within LifecycleComponent as self._ComponentInterface__outputs, or from tests as my_component._ComponentInterface__outputs. The mangling of members with the double underscore (which we associate with a "private" visibility) is really just there to prevent accidental overriding or hiding of base members from derived classes. Using it functionally and intentionally in the library and in tests is not problematic at all in my opinion.

On a related note, some guides say not to unit test private anyway, only parts of the interface that can be accessed externally. I do generally agree with this when it comes to methods, but testing the value or state of private attributes such as __outputs is still reasonable to do in the test cases.

@domire8 domire8 force-pushed the feat/private-attributes branch from b5c3fa8 to 7fe32d2 Compare November 6, 2024 16:06
@domire8
Copy link
Member Author

domire8 commented Nov 6, 2024

Thanks for your inputs, I made the necessary changes

Copy link
Member

@bpapaspyros bpapaspyros left a comment

Choose a reason for hiding this comment

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

Thanks @domire8 for the changes. I would also prefer private attributes, but I was skeptical of the outputs. I wasn't really aware of the python details. This seems nice now.

@domire8 domire8 mentioned this pull request Nov 11, 2024
1 task
@domire8 domire8 merged commit 9c4e93c into main Nov 11, 2024
4 checks passed
@domire8 domire8 deleted the feat/private-attributes branch November 11, 2024 16:16
@github-actions github-actions bot locked and limited conversation to collaborators Nov 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants