forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_calls_subclass.py
More file actions
75 lines (59 loc) · 2.06 KB
/
init_calls_subclass.py
File metadata and controls
75 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#Superclass __init__ calls subclass method
def bad1():
class Super:
def __init__(self, arg):
self._state = "Not OK"
self.set_up(arg) # BAD: set_up is overriden.
self._state = "OK"
def set_up(self, arg):
"Do some set up"
class Sub(Super):
def __init__(self, arg):
super().__init__(arg)
self.important_state = "OK"
def set_up(self, arg):
super().set_up(arg)
"Do some more set up" # `self` is partially initialized
if self.important_state == "OK":
pass
def bad2():
class Super:
def __init__(self, arg):
self.a = arg
# BAD: postproc is called after initialization. This is still an issue
# since it may still occur before all initialization on a subclass is complete.
self.postproc()
def postproc(self):
if self.a == 1:
pass
class Sub(Super):
def __init__(self, arg):
super().__init__(arg)
self.b = 3
def postproc(self):
if self.a == 2 and self.b == 3:
pass
def good3():
class Super:
def __init__(self, arg):
self.a = arg
self.set_b() # OK: Here `set_b` is used for initialization, but does not read the partially initialized state of `self`.
self.c = 1
def set_b(self):
self.b = 3
class Sub(Super):
def set_b(self):
self.b = 4
def good4():
class Super:
def __init__(self, arg):
self.a = arg
# OK: Here `_set_b` is likely an internal method (as indicated by the _ prefix).
# We assume thus that regular consumers of the library will not override it, and classes that do are internal and account for `self`'s partially initialized state.
self._set_b()
self.c = 1
def _set_b(self):
self.b = 3
class Sub(Super):
def _set_b(self):
self.b = self.a+1