Skip to content

Commit

Permalink
Improve documentation of disable_init decorator (#48)
Browse files Browse the repository at this point in the history
The `disable_init` decorator documentation was improved to clarify that
the created instance must be explicitly initialized using
`super().__init__(self)`.
  • Loading branch information
llucax authored Jan 5, 2025
2 parents 2848a0b + 073b921 commit 6f9e0da
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/frequenz/core/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def disable_init(
as the class is parsed by the Python interpreter. It will also raise a `TypeError`
when the `__init__` method is called.
To create an instance you must provide a factory method, using `__new__`.
To create an instance you must provide a factory method, using `__new__` to create
the instance and calling `super().__init__(self)` explicitly to initialize it.
Warning:
This decorator will use a custom metaclass to disable the `__init__` constructor
Expand All @@ -64,6 +65,7 @@ class MyClass:
@classmethod
def new(cls, value: int = 1) -> Self:
self = cls.__new__(cls)
super().__init__(self)
self.value = value
return self
Expand Down Expand Up @@ -100,7 +102,9 @@ def __init__(self) -> None:
class MyClass:
@classmethod
def new(cls) -> Self:
return cls.__new__(cls)
self = cls.__new__(cls)
super().__init__(self)
return self
try:
instance = MyClass()
Expand Down

0 comments on commit 6f9e0da

Please sign in to comment.