Update documentation

This commit is contained in:
Joe Farebrother
2025-08-19 12:31:45 +01:00
parent 502ea82c91
commit 900a5cd9d7
2 changed files with 17 additions and 21 deletions

View File

@@ -1,9 +1,12 @@
# Base class method class Base:
def runsource(self, source, filename="<input>", symbol="single"): def runsource(self, source, filename="<input>"):
... # Definition ...
# Extend base class method class Sub(Base):
def runsource(self, source): def runsource(self, source): # BAD: Does not match the signature of overridden method.
... # Definition ...
def run(obj: Base):
obj.runsource("source", filename="foo.txt")

View File

@@ -5,32 +5,25 @@
<overview> <overview>
<p> There are one (or more) legal parameters for an overridden method that are <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
not legal for an overriding method. This will cause an error when the overriding may not be a valid call to the subclass method, and thus raise an exception if an instance of the subclass is passed instead.
method is called with a number of parameters that is legal for the overridden method. 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
This violates the Liskov substitution principle. instance of the base class, this behavior breaks the principle.
</p> </p>
</overview> </overview>
<recommendation> <recommendation>
<p>Ensure that the overriding method accepts all the parameters that are legal for <p>Ensure that the overriding method in the subclass accepts the same parameters as the base method. </p>
overridden method.</p>
</recommendation> </recommendation>
<example> <example>
<p>In this example there is a mismatch between the legal parameters for the base <p>In the following example, <code>Base.runsource</code> takes an optional <code>filename</code> argument. However, the overriding method
class method <code>(self, source, filename, symbol)</code> and the extension method <code>Sub.runsource</code> does not. This means the <code>run</code> function will fail if passed an instance of <code>Sub</code>.
<code>(self, source)</code>. The extension method can be used to override the base </p>
method as long as values are not specified for the <code>filename</code> and
<code>symbol</code> parameters. If the extension method was passed the additional
parameters accepted by the base method then an error would occur.</p>
<sample src="SignatureOverriddenMethod.py" /> <sample src="SignatureOverriddenMethod.py" />
<p>The extension method should be updated to support the <code>filename</code> and
<code>symbol</code> parameters supported by the overridden method.</p>
</example> </example>
<references> <references>