Skip to content

Commit f30ebf1

Browse files
authored
Merge pull request #18871 from github/tausbn/python-modernise-special-method-signature-query
Python: Move min/maxParameter methods to `Function` class
2 parents 1636abb + bf3d9ee commit f30ebf1

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
5+
- Added the methods `getMinArguments` and `getMaxArguments` to the `Function` class. These return the minimum and maximum positional arguments that the given function accepts.

python/ql/lib/semmle/python/Function.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,24 @@ class Function extends Function_, Scope, AstNode {
163163
ret.getValue() = result.getNode()
164164
)
165165
}
166+
167+
/** Gets the minimum number of positional arguments that can be correctly passed to this function. */
168+
int getMinPositionalArguments() {
169+
result = count(this.getAnArg()) - count(this.getDefinition().getArgs().getADefault())
170+
}
171+
172+
/**
173+
* Gets the maximum number of positional arguments that can be correctly passed to this function.
174+
*
175+
* If the function has a `*vararg` parameter, there is no upper limit on the number of positional
176+
* arguments that can be passed to the function. In this case, this method returns a very large
177+
* number (currently `INT_MAX`, 2147483647, but this may change in the future).
178+
*/
179+
int getMaxPositionalArguments() {
180+
if exists(this.getVararg())
181+
then result = 2147483647 // INT_MAX
182+
else result = count(this.getAnArg())
183+
}
166184
}
167185

168186
/** A def statement. Note that FunctionDef extends Assign as a function definition binds the newly created function */

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -738,21 +738,9 @@ class PythonFunctionValue extends FunctionValue {
738738
else result = "function " + this.getQualifiedName()
739739
}
740740

741-
override int minParameters() {
742-
exists(Function f |
743-
f = this.getScope() and
744-
result = count(f.getAnArg()) - count(f.getDefinition().getArgs().getADefault())
745-
)
746-
}
741+
override int minParameters() { result = this.getScope().getMinPositionalArguments() }
747742

748-
override int maxParameters() {
749-
exists(Function f |
750-
f = this.getScope() and
751-
if exists(f.getVararg())
752-
then result = 2147483647 // INT_MAX
753-
else result = count(f.getAnArg())
754-
)
755-
}
743+
override int maxParameters() { result = this.getScope().getMaxPositionalArguments() }
756744

757745
/** Gets a control flow node corresponding to a return statement in this function */
758746
ControlFlowNode getAReturnedNode() { result = this.getScope().getAReturnValueFlowNode() }

0 commit comments

Comments
 (0)