Skip to content

Commit 0509ec6

Browse files
committed
Python: Port WrongNumberArgumentsInClassInstantiation.ql
Included test changes are trivial `toString` changes.
1 parent 0094271 commit 0509ec6

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

python/ql/src/Classes/WrongNumberArgumentsInClassInstantiation.ql

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,60 @@
1414
*/
1515

1616
import python
17-
import Expressions.CallArgs
18-
private import LegacyPointsTo
17+
private import semmle.python.dataflow.new.internal.DataFlowDispatch
1918

20-
from Call call, ClassValue cls, string too, string should, int limit, FunctionValue init
19+
/**
20+
* Gets the number of positional arguments in `call`, including elements of any
21+
* literal list passed as `*args`, plus keyword arguments that don't match
22+
* keyword-only parameters (when the function doesn't accept `**kwargs`).
23+
*/
24+
int positional_arg_count(Call call, Class cls, Function init) {
25+
resolveClassCall(call.getAFlowNode(), cls) and
26+
init = DuckTyping::getInit(cls) and
27+
exists(int positional_keywords |
28+
if init.hasKwArg()
29+
then positional_keywords = 0
30+
else
31+
positional_keywords =
32+
count(Keyword kw |
33+
kw = call.getAKeyword() and
34+
not init.getAKeywordOnlyArg().getId() = kw.getArg()
35+
)
36+
|
37+
result =
38+
count(call.getAnArg()) + count(call.getStarargs().(List).getAnElt()) + positional_keywords
39+
)
40+
}
41+
42+
/**
43+
* Holds if `call` constructs `cls` with too many arguments, where `limit` is the maximum.
44+
*/
45+
predicate too_many_args(Call call, Class cls, int limit) {
46+
exists(Function init |
47+
not init.hasVarArg() and
48+
// Subtract 1 from max to account for `self` parameter
49+
limit = init.getMaxPositionalArguments() - 1 and
50+
limit >= 0 and
51+
positional_arg_count(call, cls, init) > limit
52+
)
53+
}
54+
55+
/**
56+
* Holds if `call` constructs `cls` with too few arguments, where `limit` is the minimum.
57+
*/
58+
predicate too_few_args(Call call, Class cls, int limit) {
59+
resolveClassCall(call.getAFlowNode(), cls) and
60+
exists(Function init |
61+
init = DuckTyping::getInit(cls) and
62+
not exists(call.getStarargs()) and
63+
not exists(call.getKwargs()) and
64+
// Subtract 1 from min to account for `self` parameter
65+
limit = init.getMinPositionalArguments() - 1 and
66+
count(call.getAnArg()) + count(call.getAKeyword()) < limit
67+
)
68+
}
69+
70+
from Call call, Class cls, string too, string should, int limit, Function init
2171
where
2272
(
2373
too_many_args(call, cls, limit) and
@@ -28,6 +78,6 @@ where
2878
too = "too few arguments" and
2979
should = "no fewer than "
3080
) and
31-
init = get_function_or_initializer(cls)
81+
init = DuckTyping::getInit(cls)
3282
select call, "Call to $@ with " + too + "; should be " + should + limit.toString() + ".", init,
3383
init.getQualifiedName()
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
| wrong_arguments.py:37:1:37:4 | F0() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:4:5:4:26 | Function F0.__init__ | F0.__init__ |
2-
| wrong_arguments.py:38:1:38:4 | F1() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:8:5:8:36 | Function F1.__init__ | F1.__init__ |
3-
| wrong_arguments.py:39:1:39:4 | F2() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:12:5:12:30 | Function F2.__init__ | F2.__init__ |
4-
| wrong_arguments.py:40:1:40:4 | F3() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:16:5:16:40 | Function F3.__init__ | F3.__init__ |
5-
| wrong_arguments.py:41:1:41:4 | F4() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:20:5:20:31 | Function F4.__init__ | F4.__init__ |
6-
| wrong_arguments.py:42:1:42:4 | F5() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:24:5:24:42 | Function F5.__init__ | F5.__init__ |
7-
| wrong_arguments.py:43:1:43:5 | F6() | Call to $@ with too few arguments; should be no fewer than 2. | wrong_arguments.py:28:5:28:30 | Function F6.__init__ | F6.__init__ |
8-
| wrong_arguments.py:44:1:44:7 | F7() | Call to $@ with too few arguments; should be no fewer than 3. | wrong_arguments.py:32:5:32:33 | Function F7.__init__ | F7.__init__ |
9-
| wrong_arguments.py:48:1:48:7 | F0() | Call to $@ with too many arguments; should be no more than 1. | wrong_arguments.py:4:5:4:26 | Function F0.__init__ | F0.__init__ |
10-
| wrong_arguments.py:49:1:49:9 | F1() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:8:5:8:36 | Function F1.__init__ | F1.__init__ |
11-
| wrong_arguments.py:50:1:50:9 | F5() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:24:5:24:42 | Function F5.__init__ | F5.__init__ |
12-
| wrong_arguments.py:51:1:51:9 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function F6.__init__ | F6.__init__ |
13-
| wrong_arguments.py:52:1:52:11 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function F6.__init__ | F6.__init__ |
141
| wrong_arguments.py:85:1:85:12 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function F6.__init__ | F6.__init__ |
152
| wrong_arguments.py:86:1:86:7 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function F6.__init__ | F6.__init__ |
3+
| wrong_arguments.py:37:1:37:4 | F0() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:4:5:4:26 | Function __init__ | F0.__init__ |
4+
| wrong_arguments.py:38:1:38:4 | F1() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:8:5:8:36 | Function __init__ | F1.__init__ |
5+
| wrong_arguments.py:39:1:39:4 | F2() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:12:5:12:30 | Function __init__ | F2.__init__ |
6+
| wrong_arguments.py:40:1:40:4 | F3() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:16:5:16:40 | Function __init__ | F3.__init__ |
7+
| wrong_arguments.py:41:1:41:4 | F4() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:20:5:20:31 | Function __init__ | F4.__init__ |
8+
| wrong_arguments.py:42:1:42:4 | F5() | Call to $@ with too few arguments; should be no fewer than 1. | wrong_arguments.py:24:5:24:42 | Function __init__ | F5.__init__ |
9+
| wrong_arguments.py:43:1:43:5 | F6() | Call to $@ with too few arguments; should be no fewer than 2. | wrong_arguments.py:28:5:28:30 | Function __init__ | F6.__init__ |
10+
| wrong_arguments.py:44:1:44:7 | F7() | Call to $@ with too few arguments; should be no fewer than 3. | wrong_arguments.py:32:5:32:33 | Function __init__ | F7.__init__ |
11+
| wrong_arguments.py:48:1:48:7 | F0() | Call to $@ with too many arguments; should be no more than 1. | wrong_arguments.py:4:5:4:26 | Function __init__ | F0.__init__ |
12+
| wrong_arguments.py:49:1:49:9 | F1() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:8:5:8:36 | Function __init__ | F1.__init__ |
13+
| wrong_arguments.py:50:1:50:9 | F5() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:24:5:24:42 | Function __init__ | F5.__init__ |
14+
| wrong_arguments.py:51:1:51:9 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function __init__ | F6.__init__ |
15+
| wrong_arguments.py:52:1:52:11 | F6() | Call to $@ with too many arguments; should be no more than 2. | wrong_arguments.py:28:5:28:30 | Function __init__ | F6.__init__ |

0 commit comments

Comments
 (0)