Skip to content

Commit 0a681cc

Browse files
Update documentation
1 parent e0adcf0 commit 0a681cc

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11

2-
# Base class method
3-
def runsource(self, source, filename="<input>", symbol="single"):
4-
... # Definition
2+
class Base:
3+
def runsource(self, source, filename="<input>"):
4+
...
55

66

7-
# Extend base class method
8-
def runsource(self, source):
9-
... # Definition
7+
class Sub(Base):
8+
def runsource(self, source): # BAD: Does not match the signature of overridden method.
9+
...
10+
11+
def run(obj: Base):
12+
obj.runsource("source", filename="foo.txt")

python/ql/src/Functions/SignatureOverriddenMethod.qhelp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,25 @@
55

66

77
<overview>
8-
<p> There are one (or more) legal parameters for an overridden method that are
9-
not legal for an overriding method. This will cause an error when the overriding
10-
method is called with a number of parameters that is legal for the overridden method.
11-
This violates the Liskov substitution principle.
8+
<p> When the signature of a method of a base class and a method of a subclass that overrides it don't match, a call to the base class method
9+
may not be a valid call to the subclass method, and thus raise an exception if an instance of the subclass is passed instead.
10+
If following the Liskov Substitution Principle, in which an instance of a subclass should be usable in every context as though it were a an
11+
instance of the base class, this behavior breaks the principle.
1212
</p>
1313

1414
</overview>
1515
<recommendation>
1616

17-
<p>Ensure that the overriding method accepts all the parameters that are legal for
18-
overridden method.</p>
17+
<p>Ensure that the overriding method in the subclass accepts the same parameters as the base method. </p>
1918

2019
</recommendation>
2120
<example>
22-
<p>In this example there is a mismatch between the legal parameters for the base
23-
class method <code>(self, source, filename, symbol)</code> and the extension method
24-
<code>(self, source)</code>. The extension method can be used to override the base
25-
method as long as values are not specified for the <code>filename</code> and
26-
<code>symbol</code> parameters. If the extension method was passed the additional
27-
parameters accepted by the base method then an error would occur.</p>
21+
<p>In the following example, <code>Base.runsource</code> takes an optional <code>filename</code> argument. However, the overriding method
22+
<code>Sub.runsource</code> does not. This means the <code>run</code> function will fail if passed an instance of <code>Sub</code>.
23+
</p>
2824

2925
<sample src="SignatureOverriddenMethod.py" />
3026

31-
<p>The extension method should be updated to support the <code>filename</code> and
32-
<code>symbol</code> parameters supported by the overridden method.</p>
33-
3427
</example>
3528
<references>
3629

0 commit comments

Comments
 (0)