You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/ql/src/Classes/InitCallsSubclass/InitCallsSubclassMethod.qhelp
+13-7Lines changed: 13 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,9 @@
6
6
<p>
7
7
When initializing an instance of the class in the class' <code>__init__</code> method, calls tha are made using the instance may receive an instance of the class that is not
8
8
yet fully initialized. When a method called in an initializer is overridden in a subclass, the subclass method receives the instance
9
-
in a potentially unexpected state, which may lead to runtime errors from accessing uninitialized fields, and generally makes the code
10
-
more difficult to maintain.
9
+
in a potentially unexpected state. Fields that would be initialized after the call, including potentially in the subclass' <code>__init__</code> method,
10
+
will not be initialized. This may lead to runtime errors, as well as make the code more difficult to maintain, as future changes may not
11
+
be aware of which fields would not be initialized.
11
12
</p>
12
13
13
14
</overview>
@@ -16,14 +17,19 @@ more difficult to maintain.
16
17
<p>If possible, refactor the initializer method such that initialization is complete before calling any overridden methods.
17
18
For helper methods used as part of initialization, avoid overriding them, and instead call any additional logic required
18
19
in the subclass' <code>__init__</code> method.
19
-
</p><p>
20
-
If calling an overridden method is required, consider marking it as an internal method (by using an <code>_</code> prefix) to
21
-
discourage external users of the library from overriding it and observing partially initialized state.
20
+
</p>
21
+
<p>
22
+
If the overridden method does not depend on the instance <code>self</code>, and only on its class, consider making it a <code>@classmethod</code> or <code>@staticmethod</code> instead.
23
+
</p>
24
+
<p>
25
+
If calling an overridden method is absolutely required, consider marking it as an internal method (by using an <code>_</code> prefix) to
26
+
discourage external users of the library from overriding it and observing partially initialized state, and ensure that the fact it is called during initialization
27
+
is mentioned in the documentation.
22
28
</p>
23
29
24
30
</recommendation>
25
31
<example>
26
-
<p>In the following case, the `__init__` method of `Super` calls the `set_up` method that is overriden by `Sub`.
32
+
<p>In the following case, the <code>__init__</code> method of <code>Super</code> calls the <code>set_up</code> method that is overridden by <code>Sub</code>.
27
33
This results in `Sun.set_up` being called with a partially initialized instance of `Super` which may be unexpected. </p>
0 commit comments