Skip to content

Commit be00d71

Browse files
committed
Python: ObjectAPI to ValueAPI: IncorrectlyOverriddenMethod: Adds preliminary modernization
1 parent 9f18a15 commit be00d71

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

python/ql/src/Functions/IncorrectlyOverriddenMethod.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
import python
1313
import Expressions.CallArgs
1414

15-
from Call call, FunctionObject func, FunctionObject overridden, string problem
15+
from Call call, FunctionValue func, FunctionValue overridden, string problem
1616
where
1717
func.overrides(overridden) and
1818
(
19-
wrong_args_objectapi(call, func, _, problem) and
20-
correct_args_if_called_as_method_objectapi(call, overridden)
19+
wrong_args(call, func, _, problem) and
20+
correct_args_if_called_as_method(call, overridden)
2121
or
2222
exists(string name |
23-
illegally_named_parameter_objectapi(call, func, name) and
23+
illegally_named_parameter(call, func, name) and
2424
problem = "an argument named '" + name + "'" and
25-
overridden.getFunction().getAnArg().(Name).getId() = name
25+
overridden.getScope().getAnArg().(Name).getId() = name
2626
)
2727
)
2828
select func,

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ class ClassValue extends Value {
573573
abstract class FunctionValue extends CallableValue {
574574
abstract string getQualifiedName();
575575

576+
/** Gets a longer, more descriptive version of toString() */
577+
abstract string descriptiveString();
578+
576579
/** Gets the minimum number of parameters that can be correctly passed to this function */
577580
abstract int minParameters();
578581

@@ -617,6 +620,15 @@ class PythonFunctionValue extends FunctionValue {
617620
result = this.(PythonFunctionObjectInternal).getScope().getQualifiedName()
618621
}
619622

623+
override string descriptiveString() {
624+
if this.getScope().isMethod()
625+
then
626+
exists(Class cls | this.getScope().getScope() = cls |
627+
result = "method " + this.getQualifiedName()
628+
)
629+
else result = "function " + this.getQualifiedName()
630+
}
631+
620632
override int minParameters() {
621633
exists(Function f |
622634
f = this.getScope() and
@@ -650,6 +662,8 @@ class BuiltinFunctionValue extends FunctionValue {
650662

651663
override string getQualifiedName() { result = this.(BuiltinFunctionObjectInternal).getName() }
652664

665+
override string descriptiveString() { result = "builtin-function " + this.getName() }
666+
653667
override int minParameters() { none() }
654668

655669
override int maxParameters() { none() }
@@ -674,6 +688,8 @@ class BuiltinMethodValue extends FunctionValue {
674688
)
675689
}
676690

691+
override string descriptiveString() { result = "builtin-method " + this.getQualifiedName() }
692+
677693
override int minParameters() { none() }
678694

679695
override int maxParameters() { none() }
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| test.py:24:5:24:26 | Function meth1 | Overriding method signature does not match $@, where it is passed too few. Overridden method $@ is correctly specified. | test.py:15:9:15:20 | Attribute() | here | test.py:5:5:5:20 | Function meth1 | method Base.meth1 |
2-
| test.py:24:5:24:26 | Function meth1 | Overriding method signature does not match $@, where it is passed too few. Overridden method $@ is correctly specified. | test.py:34:9:34:20 | Attribute() | here | test.py:5:5:5:20 | Function meth1 | method Base.meth1 |
3-
| test.py:27:5:27:20 | Function meth2 | Overriding method signature does not match $@, where it is passed an argument named 'spam'. Overridden method $@ is correctly specified. | test.py:20:9:20:31 | Attribute() | here | test.py:8:5:8:26 | Function meth2 | method Base.meth2 |
4-
| test.py:27:5:27:20 | Function meth2 | Overriding method signature does not match $@, where it is passed an argument named 'spam'. Overridden method $@ is correctly specified. | test.py:39:9:39:31 | Attribute() | here | test.py:8:5:8:26 | Function meth2 | method Base.meth2 |
5-
| test.py:27:5:27:20 | Function meth2 | Overriding method signature does not match $@, where it is passed too many. Overridden method $@ is correctly specified. | test.py:18:9:18:21 | Attribute() | here | test.py:8:5:8:26 | Function meth2 | method Base.meth2 |
6-
| test.py:27:5:27:20 | Function meth2 | Overriding method signature does not match $@, where it is passed too many. Overridden method $@ is correctly specified. | test.py:37:9:37:21 | Attribute() | here | test.py:8:5:8:26 | Function meth2 | method Base.meth2 |
1+
| test.py:24:5:24:26 | Function Derived.meth1 | Overriding method signature does not match $@, where it is passed too few. Overridden method $@ is correctly specified. | test.py:15:9:15:20 | Attribute() | here | test.py:5:5:5:20 | Function Base.meth1 | method Base.meth1 |
2+
| test.py:24:5:24:26 | Function Derived.meth1 | Overriding method signature does not match $@, where it is passed too few. Overridden method $@ is correctly specified. | test.py:34:9:34:20 | Attribute() | here | test.py:5:5:5:20 | Function Base.meth1 | method Base.meth1 |
3+
| test.py:27:5:27:20 | Function Derived.meth2 | Overriding method signature does not match $@, where it is passed an argument named 'spam'. Overridden method $@ is correctly specified. | test.py:20:9:20:31 | Attribute() | here | test.py:8:5:8:26 | Function Base.meth2 | method Base.meth2 |
4+
| test.py:27:5:27:20 | Function Derived.meth2 | Overriding method signature does not match $@, where it is passed an argument named 'spam'. Overridden method $@ is correctly specified. | test.py:39:9:39:31 | Attribute() | here | test.py:8:5:8:26 | Function Base.meth2 | method Base.meth2 |
5+
| test.py:27:5:27:20 | Function Derived.meth2 | Overriding method signature does not match $@, where it is passed too many. Overridden method $@ is correctly specified. | test.py:18:9:18:21 | Attribute() | here | test.py:8:5:8:26 | Function Base.meth2 | method Base.meth2 |
6+
| test.py:27:5:27:20 | Function Derived.meth2 | Overriding method signature does not match $@, where it is passed too many. Overridden method $@ is correctly specified. | test.py:37:9:37:21 | Attribute() | here | test.py:8:5:8:26 | Function Base.meth2 | method Base.meth2 |

0 commit comments

Comments
 (0)