Skip to content

Commit a230877

Browse files
authored
Merge pull request github#3489 from yoff/DeprecateObject
Python: Modernise `py/missing-equals`.
2 parents b9ecf1a + 3e712be commit a230877

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

python/ql/src/Classes/DefineEqualsWhenAddingAttributes.ql

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,38 @@ import python
1414
import semmle.python.SelfAttribute
1515
import Equality
1616

17-
predicate class_stores_to_attribute(ClassObject cls, SelfAttributeStore store, string name) {
18-
exists(FunctionObject f |
19-
f = cls.declaredAttribute(_) and store.getScope() = f.getFunction() and store.getName() = name
17+
predicate class_stores_to_attribute(ClassValue cls, SelfAttributeStore store, string name) {
18+
exists(FunctionValue f |
19+
f = cls.declaredAttribute(_) and store.getScope() = f.getScope() and store.getName() = name
2020
) and
2121
/* Exclude classes used as metaclasses */
22-
not cls.getASuperType() = theTypeType()
22+
not cls.getASuperType() = ClassValue::type()
2323
}
2424

25-
predicate should_override_eq(ClassObject cls, Object base_eq) {
25+
predicate should_override_eq(ClassValue cls, Value base_eq) {
2626
not cls.declaresAttribute("__eq__") and
27-
exists(ClassObject sup | sup = cls.getABaseType() and sup.declaredAttribute("__eq__") = base_eq |
28-
not exists(GenericEqMethod eq | eq.getScope() = sup.getPyClass()) and
29-
not exists(IdentityEqMethod eq | eq.getScope() = sup.getPyClass()) and
30-
not base_eq.(FunctionObject).getFunction() instanceof IdentityEqMethod and
31-
not base_eq = theObjectType().declaredAttribute("__eq__")
27+
exists(ClassValue sup | sup = cls.getABaseType() and sup.declaredAttribute("__eq__") = base_eq |
28+
not exists(GenericEqMethod eq | eq.getScope() = sup.getScope()) and
29+
not exists(IdentityEqMethod eq | eq.getScope() = sup.getScope()) and
30+
not base_eq.(FunctionValue).getScope() instanceof IdentityEqMethod and
31+
not base_eq = ClassValue::object().declaredAttribute("__eq__")
3232
)
3333
}
3434

3535
/**
3636
* Does the non-overridden __eq__ method access the attribute,
3737
* which implies that the __eq__ method does not need to be overridden.
3838
*/
39-
predicate superclassEqExpectsAttribute(ClassObject cls, PyFunctionObject base_eq, string attrname) {
39+
predicate superclassEqExpectsAttribute(ClassValue cls, FunctionValue base_eq, string attrname) {
4040
not cls.declaresAttribute("__eq__") and
41-
exists(ClassObject sup | sup = cls.getABaseType() and sup.declaredAttribute("__eq__") = base_eq |
41+
exists(ClassValue sup | sup = cls.getABaseType() and sup.declaredAttribute("__eq__") = base_eq |
4242
exists(SelfAttributeRead store | store.getName() = attrname |
43-
store.getScope() = base_eq.getFunction()
43+
store.getScope() = base_eq.getScope()
4444
)
4545
)
4646
}
4747

48-
from ClassObject cls, SelfAttributeStore store, Object base_eq
48+
from ClassValue cls, SelfAttributeStore store, Value base_eq
4949
where
5050
class_stores_to_attribute(cls, store, _) and
5151
should_override_eq(cls, base_eq) and
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| test.py:12:1:12:24 | class C | The class 'C' does not override $@, but adds the new attribute $@. | test.py:9:5:9:28 | Function __eq__ | '__eq__' | test.py:15:9:15:14 | Attribute | a |
2-
| test.py:12:1:12:24 | class C | The class 'C' does not override $@, but adds the new attribute $@. | test.py:9:5:9:28 | Function __eq__ | '__eq__' | test.py:15:17:15:22 | Attribute | b |
1+
| test.py:12:1:12:24 | class C | The class 'C' does not override $@, but adds the new attribute $@. | test.py:9:5:9:28 | Function RedefineEquals.__eq__ | '__eq__' | test.py:15:9:15:14 | Attribute | a |
2+
| test.py:12:1:12:24 | class C | The class 'C' does not override $@, but adds the new attribute $@. | test.py:9:5:9:28 | Function RedefineEquals.__eq__ | '__eq__' | test.py:15:17:15:22 | Attribute | b |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| attr_eq_test.py:21:1:21:27 | class BadColorPoint | The class 'BadColorPoint' does not override $@, but adds the new attribute $@. | attr_eq_test.py:10:5:10:28 | Function __eq__ | '__eq__' | attr_eq_test.py:25:9:25:19 | Attribute | _color |
1+
| attr_eq_test.py:21:1:21:27 | class BadColorPoint | The class 'BadColorPoint' does not override $@, but adds the new attribute $@. | attr_eq_test.py:10:5:10:28 | Function Point.__eq__ | '__eq__' | attr_eq_test.py:25:9:25:19 | Attribute | _color |

0 commit comments

Comments
 (0)