@@ -49,7 +49,11 @@ class Function extends Function_, Scope, AstNode {
49
49
string getArgName ( int index ) { result = this .getArg ( index ) .( Name ) .getId ( ) }
50
50
51
51
Parameter getArgByName ( string name ) {
52
- result = this .getAnArg ( ) and
52
+ (
53
+ result = this .getAnArg ( )
54
+ or
55
+ result = this .getAKeywordOnlyArg ( )
56
+ ) and
53
57
result .( Name ) .getId ( ) = name
54
58
}
55
59
@@ -90,7 +94,7 @@ class Function extends Function_, Scope, AstNode {
90
94
int getPositionalParameterCount ( ) { result = count ( this .getAnArg ( ) ) }
91
95
92
96
/** Gets the number of keyword-only parameters */
93
- int getKeywordOnlyParameterCount ( ) { result = count ( this .getAKwonlyarg ( ) ) }
97
+ int getKeywordOnlyParameterCount ( ) { result = count ( this .getAKeywordOnlyArg ( ) ) }
94
98
95
99
/** Whether this function accepts a variable number of arguments. That is, whether it has a starred (*arg) parameter. */
96
100
predicate hasVarArg ( ) { exists ( this .getVararg ( ) ) }
@@ -102,6 +106,7 @@ class Function extends Function_, Scope, AstNode {
102
106
result = this .getAStmt ( ) or
103
107
result = this .getAnArg ( ) or
104
108
result = this .getVararg ( ) or
109
+ result = this .getAKeywordOnlyArg ( ) or
105
110
result = this .getKwarg ( )
106
111
}
107
112
@@ -185,6 +190,8 @@ class Parameter extends Parameter_ {
185
190
f .getVararg ( ) = this
186
191
or
187
192
f .getKwarg ( ) = this
193
+ or
194
+ f .getAKeywordOnlyArg ( ) = this
188
195
)
189
196
}
190
197
@@ -202,19 +209,31 @@ class Parameter extends Parameter_ {
202
209
203
210
/** Gets the expression for the default value of this parameter */
204
211
Expr getDefault ( ) {
205
- exists ( Function f , int n , int c , int d , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
206
- f .getArg ( n ) = this and
207
- c = count ( f .getAnArg ( ) ) and
208
- d = count ( args .getADefault ( ) ) and
209
- result = args .getDefault ( d - c + n )
212
+ exists ( Function f , int i , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
213
+ // positional (normal)
214
+ f .getArg ( i ) = this and
215
+ result = args .getDefault ( i )
216
+ )
217
+ or
218
+ exists ( Function f , int i , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
219
+ // keyword-only
220
+ f .getKeywordOnlyArg ( i ) = this and
221
+ result = args .getKwDefault ( i )
210
222
)
211
223
}
212
224
213
225
/** Gets the annotation expression of this parameter */
214
226
Expr getAnnotation ( ) {
215
- exists ( Function f , int n , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
216
- f .getArg ( n ) = this and
217
- result = args .getAnnotation ( n )
227
+ exists ( Function f , int i , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
228
+ // positional (normal)
229
+ f .getArg ( i ) = this and
230
+ result = args .getAnnotation ( i )
231
+ )
232
+ or
233
+ exists ( Function f , int i , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
234
+ // keyword-only
235
+ f .getKeywordOnlyArg ( i ) = this and
236
+ result = args .getKwAnnotation ( i )
218
237
)
219
238
or
220
239
exists ( Function f , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
@@ -228,7 +247,10 @@ class Parameter extends Parameter_ {
228
247
229
248
Variable getVariable ( ) { result .getAnAccess ( ) = this .asName ( ) }
230
249
231
- /** Gets the position of this parameter */
250
+ /**
251
+ * Gets the position of this parameter (if any).
252
+ * No result if this is a "varargs", "kwargs", or keyword-only parameter.
253
+ */
232
254
int getPosition ( ) { exists ( Function f | f .getArg ( result ) = this ) }
233
255
234
256
/** Gets the name of this parameter */
@@ -243,13 +265,13 @@ class Parameter extends Parameter_ {
243
265
}
244
266
245
267
/**
246
- * Holds if this parameter is a ' varargs' parameter.
268
+ * Holds if this parameter is a " varargs" parameter.
247
269
* The `varargs` in `f(a, b, *varargs)`.
248
270
*/
249
271
predicate isVarargs ( ) { exists ( Function func | func .getVararg ( ) = this ) }
250
272
251
273
/**
252
- * Holds if this parameter is a ' kwargs' parameter.
274
+ * Holds if this parameter is a " kwargs" parameter.
253
275
* The `kwargs` in `f(a, b, **kwargs)`.
254
276
*/
255
277
predicate isKwargs ( ) { exists ( Function func | func .getKwarg ( ) = this ) }
@@ -258,7 +280,8 @@ class Parameter extends Parameter_ {
258
280
/** An expression that generates a callable object, either a function expression or a lambda */
259
281
abstract class CallableExpr extends Expr {
260
282
/**
261
- * Gets the parameters of this callable.
283
+ * Gets The default values and annotations (type-hints) for the arguments of this callable.
284
+ *
262
285
* This predicate is called getArgs(), rather than getParameters() for compatibility with Python's AST module.
263
286
*/
264
287
abstract Arguments getArgs ( ) ;
@@ -295,7 +318,7 @@ class FunctionExpr extends FunctionExpr_, CallableExpr {
295
318
override Arguments getArgs ( ) { result = FunctionExpr_ .super .getArgs ( ) }
296
319
}
297
320
298
- /** A lambda expression, such as lambda x:x*x */
321
+ /** A lambda expression, such as ` lambda x: x+1` */
299
322
class Lambda extends Lambda_ , CallableExpr {
300
323
/** Gets the expression to the right of the colon in this lambda expression */
301
324
Expr getExpression ( ) {
@@ -314,13 +337,36 @@ class Lambda extends Lambda_, CallableExpr {
314
337
override Arguments getArgs ( ) { result = Lambda_ .super .getArgs ( ) }
315
338
}
316
339
317
- /** The arguments in a function definition */
340
+ /**
341
+ * The default values and annotations (type hints) for the arguments in a function definition.
342
+ *
343
+ * Annotations (PEP 3107) is a general mechanism for providing annotations for a function,
344
+ * that is generally only used for type hints today (PEP 484).
345
+ */
318
346
class Arguments extends Arguments_ {
347
+
319
348
Expr getASubExpression ( ) {
349
+ result = this .getADefault ( ) or
320
350
result = this .getAKwDefault ( ) or
351
+ //
321
352
result = this .getAnAnnotation ( ) or
322
- result = this .getKwargannotation ( ) or
323
353
result = this .getVarargannotation ( ) or
324
- result = this .getADefault ( )
354
+ result = this .getAKwAnnotation ( ) or
355
+ result = this .getKwargannotation ( )
325
356
}
357
+
358
+ // The following 4 methods are overwritten to provide better QLdoc. Since the
359
+ // Arguments_ is auto-generated, we can't change the poor auto-generated docs there :(
360
+
361
+ /** Gets the default value for the `index`'th positional parameter. */
362
+ override Expr getDefault ( int index ) { result = super .getDefault ( index ) }
363
+
364
+ /** Gets the default value for the `index`'th keyword-only parameter. */
365
+ override Expr getKwDefault ( int index ) { result = super .getKwDefault ( index ) }
366
+
367
+ /** Gets the annotation for the `index`'th positional parameter. */
368
+ override Expr getAnnotation ( int index ) { result = super .getAnnotation ( index ) }
369
+
370
+ /** Gets the annotation for the `index`'th keyword-only parameter. */
371
+ override Expr getKwAnnotation ( int index ) { result = super .getKwAnnotation ( index ) }
326
372
}
0 commit comments