Skip to content

Commit 073b921

Browse files
committed
Improve documentation of disable_init decorator
The `disable_init` decorator documentation was improved to clarify that the created instance must be explicitly initialized using `super().__init__(self)`. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 2848a0b commit 073b921

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/frequenz/core/typing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def disable_init(
4343
as the class is parsed by the Python interpreter. It will also raise a `TypeError`
4444
when the `__init__` method is called.
4545
46-
To create an instance you must provide a factory method, using `__new__`.
46+
To create an instance you must provide a factory method, using `__new__` to create
47+
the instance and calling `super().__init__(self)` explicitly to initialize it.
4748
4849
Warning:
4950
This decorator will use a custom metaclass to disable the `__init__` constructor
@@ -64,6 +65,7 @@ class MyClass:
6465
@classmethod
6566
def new(cls, value: int = 1) -> Self:
6667
self = cls.__new__(cls)
68+
super().__init__(self)
6769
self.value = value
6870
return self
6971
@@ -100,7 +102,9 @@ def __init__(self) -> None:
100102
class MyClass:
101103
@classmethod
102104
def new(cls) -> Self:
103-
return cls.__new__(cls)
105+
self = cls.__new__(cls)
106+
super().__init__(self)
107+
return self
104108
105109
try:
106110
instance = MyClass()

0 commit comments

Comments
 (0)