Move to subfolder

This commit is contained in:
Joe Farebrother
2025-07-30 15:17:19 +01:00
parent 34317d2d4a
commit 2516f9452e
4 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
When an object has an attribute that shares the same name a method on the object's class (or another class attribute), the instance attribute is
prioritized during attribute lookup, shadowing the method.
If a method on a subclass is shadowed by an attribute on a superclass in this way, this may lead to unexpected results or errors, as this
shadowing behavior is nonlocal and may be unintended.
</p>
</overview>
<recommendation>
<p>
Ensure method names on subclasses don't conflict with attribute names on superclasses, and rename one.
If the shadowing behavior is intended, ensure this is explicit in the superclass.
</p>
</recommendation>
<example>
<p>
In the following example, the <code>_foo</code> attribute of class <code>A</code> shadows the method <code>_foo</code> of class <code>B</code>.
Calls to <code>B()._foo()</code> will result in a <code>TypeError</code>, as <code>3</code> will be called instead.
</p>
<sample src="examples/SubclassShadowingGood.py" />
<p>
In the following example...
</p>
</example>
</qhelp>

View File

@@ -0,0 +1,71 @@
/**
* @name Superclass attribute shadows subclass method
* @description Defining an attribute in a superclass method with a name that matches a subclass
* method, hides the method in the subclass.
* @kind problem
* @problem.severity error
* @tags quality
* reliability
* correctness
* @sub-severity low
* @precision high
* @id py/attribute-shadows-method
*/
import python
import semmle.python.ApiGraphs
import semmle.python.dataflow.new.internal.DataFlowDispatch
predicate isSettableProperty(Function prop) {
isProperty(prop) and
exists(Function setter |
setter.getScope() = prop.getScope() and
setter.getName() = prop.getName() and
isSetter(setter)
)
}
predicate isSetter(Function f) {
exists(DataFlow::AttrRead attr |
f.getADecorator() = attr.asExpr() and
attr.getAttributeName() = "setter"
)
}
predicate isProperty(Function prop) {
prop.getADecorator() = API::builtin("property").asSource().asExpr()
}
predicate shadowedBySuperclass(
Class cls, Class superclass, DataFlow::AttrWrite write, Function shadowed
) {
getADirectSuperclass+(cls) = superclass and
shadowed = cls.getAMethod() and
exists(Function init |
init = superclass.getInitMethod() and
DataFlow::parameterNode(init.getArg(0)).(DataFlow::LocalSourceNode).flowsTo(write.getObject()) and
write.getAttributeName() = shadowed.getName()
) and
// Allow cases in which the super class defines the method as well.
// We assume that the original method must have been defined for a reason.
not exists(Function superShadowed |
superShadowed = superclass.getAMethod() and
superShadowed.getName() = shadowed.getName()
) and
// Allow properties if they have setters, as the write in the superclass will call the setter.
not isSettableProperty(shadowed) and
not isSetter(shadowed)
}
from Class cls, Class superclass, DataFlow::AttrWrite write, Function shadowed, string extra
where
shadowedBySuperclass(cls, superclass, write, shadowed) and
(
if isProperty(shadowed)
then
// it's not a setter, so it's a read-only property
extra = " (read-only property may cause an error if written to in the superclass.)"
else extra = ""
)
select shadowed, "This method is shadowed by $@ in superclass $@." + extra, write,
"attribute " + write.getAttributeName(), superclass, superclass.getName()

View File

@@ -0,0 +1,9 @@
class A:
def __init__(self):
self._foo = 3
class B:
# BAD: _foo is shadowed by attribute A._foo
def _foo(self):
return 2