forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubclass_shadowing.py
More file actions
51 lines (36 loc) · 1010 Bytes
/
subclass_shadowing.py
File metadata and controls
51 lines (36 loc) · 1010 Bytes
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
#Subclass shadowing
# BAD: `shadow` method shadows attribute
class Base:
def __init__(self):
self.shadow = 4
class Derived(Base):
def shadow(self): # $ Alert
pass
# OK: Allow if superclass also shadows its own method, as this is likely intended.
# Example: stdlib JSONEncoder.default uses this pattern.
class Base2:
def __init__(self, default=None):
if default:
self.default = default
def default(self):
pass
class Derived2(Base2):
def default(self): # No alert
return 0
# Properties
class Base3:
def __init__(self):
self.foo = 1
self.bar = 2
class Derived3(Base3):
# BAD: Write to foo in superclass init raises an error.
@property
def foo(self): # $ Alert
return 2
# OK: This property has a setter, so the write is OK.
@property
def bar(self): # No alert
return self._bar
@bar.setter
def bar(self, val):
self._bar = val