Skip to content

Conversation

@shsms
Copy link
Contributor

@shsms shsms commented Jun 26, 2023

No description provided.

@shsms shsms added this to the v0.22.0 milestone Jun 26, 2023
@shsms shsms requested a review from a team as a code owner June 26, 2023 12:14
@shsms shsms requested a review from Marenz June 26, 2023 12:14
@github-actions github-actions bot added part:data-pipeline Affects the data pipeline part:tests Affects the unit, integration and performance (benchmarks) tests labels Jun 26, 2023
@shsms shsms force-pushed the quantity-improvements branch from 892ba52 to d7272fa Compare June 26, 2023 12:15
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

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

Nice! Only 1 (serious) comment.

Comment on lines 285 to 310
def disable_default_constructor(cls: type[_T]) -> type[_T]:
"""Disable the default constructor for a class.
Check that the class does not already have a default constructor, and add one that
raises a TypeError if called.
Args:
cls: The class to disable the default constructor for.
Returns:
The class with the default constructor disabled.
Raises:
TypeError: If the class already has a default constructor.
"""
if "__init__" in cls.__dict__:
raise TypeError(f"No default constructor allowed for {cls.__name__}")

def __init__(self: Any, *args: Any, **kwargs: Any) -> None:
raise TypeError(
f"Use of default constructor NOT allowed for {cls.__name__}. "
f"Use one of the `{cls.__name__}.from_*()` methods instead."
)

cls.__init__ = __init__ # type: ignore[method-assign]
return cls
Copy link
Contributor

@llucax llucax Jun 26, 2023

Choose a reason for hiding this comment

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

I'm a bit scared about patching classes via a decorator now, after the headaches this brought with the @actor decorator. I guess for this case where you are only adding a __init__ that probably won't confuse mypy it should be OK, but I fear in the future someone might think it is a good idea to add some other common method here and things go south again.

Other approaches that might be more mypy friendly are just defining a base type _NoDefaultConstructible and inherit from it (but that won't catch if the error if someone adds an __init__ to it, although it would be weird to inherit from _NoDefaultConstructible and add an __init__. But another alternative would be metaclasses, I'm not sure how mypy feels about those, but I think is the most widely method used to manipulate classes, so my guess is that probably better than decorators.

For example: https://stackoverflow.com/questions/8212053/private-constructor-in-python/64682734#64682734

This also adds the _create() method that could actually be useful to abstract sub-classes from dealing with __new__() directly.

Copy link
Contributor Author

@shsms shsms Jun 27, 2023

Choose a reason for hiding this comment

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

I've replaced it with a meta class. Didn't add a private constructor, because it has to be typed with Any, whereas with __new__, we get proper typing.

@llucax llucax removed this from the v0.22.0 milestone Jun 27, 2023
@llucax llucax linked an issue Jun 27, 2023 that may be closed by this pull request
@shsms shsms force-pushed the quantity-improvements branch 3 times, most recently from a40db61 to d4dc2cb Compare June 28, 2023 09:28
llucax
llucax previously approved these changes Jun 28, 2023
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

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

🎉 LGTM.

I noticed there is no RELEASE_NOTES.md entry for this (I mean the whole quantities new feature), so maybe you can add some in this PR? We should have some notes before the release :)

@shsms shsms force-pushed the quantity-improvements branch from d4dc2cb to d566feb Compare June 28, 2023 16:08
@github-actions github-actions bot added the part:docs Affects the documentation label Jun 28, 2023
shsms added 7 commits June 28, 2023 18:12
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This is because support for default constructors for derived types is
going away in a subsequent commit.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This commit also updates the FormulaBuilders to take custom
constructors as arguments.

FormulaEngines were previously taking the type of the quantity, with
which, it could use only the default constructors to create objects.

Because support for using default constructors is going away,
FormulaEngines and FormulaBuilders need to know the exact constructor
to use for creating output quantities.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This is because the default constructors for specialized quantity
types will stop working in a subsequent commit.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This is so that there are no remaining dependencies on the default
quantity constructor, before it is disabled for specialized
quantites.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Specialized quantities can only be instantiated through custom
constructors after this.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@shsms shsms force-pushed the quantity-improvements branch from d566feb to 7ac01de Compare June 28, 2023 16:12
@shsms
Copy link
Contributor Author

shsms commented Jun 28, 2023

I noticed there is no RELEASE_NOTES.md entry for this (I mean the whole quantities new feature), so maybe you can add some in this PR? We should have some notes before the release :)

added for all my recently merged PRs.

@shsms shsms added this pull request to the merge queue Jun 29, 2023
Merged via the queue into frequenz-floss:v0.x.x with commit 1a6d3fd Jun 29, 2023
@shsms shsms deleted the quantity-improvements branch June 29, 2023 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disable default constructor for derived Quantity types

2 participants