@@ -49,7 +49,11 @@ class Function extends Function_, Scope, AstNode {
4949 string getArgName ( int index ) { result = this .getArg ( index ) .( Name ) .getId ( ) }
5050
5151 Parameter getArgByName ( string name ) {
52- result = this .getAnArg ( ) and
52+ (
53+ result = this .getAnArg ( )
54+ or
55+ result = this .getAKeywordOnlyArg ( )
56+ ) and
5357 result .( Name ) .getId ( ) = name
5458 }
5559
@@ -90,7 +94,7 @@ class Function extends Function_, Scope, AstNode {
9094 int getPositionalParameterCount ( ) { result = count ( this .getAnArg ( ) ) }
9195
9296 /** Gets the number of keyword-only parameters */
93- int getKeywordOnlyParameterCount ( ) { result = count ( this .getAKwonlyarg ( ) ) }
97+ int getKeywordOnlyParameterCount ( ) { result = count ( this .getAKeywordOnlyArg ( ) ) }
9498
9599 /** Whether this function accepts a variable number of arguments. That is, whether it has a starred (*arg) parameter. */
96100 predicate hasVarArg ( ) { exists ( this .getVararg ( ) ) }
@@ -102,6 +106,7 @@ class Function extends Function_, Scope, AstNode {
102106 result = this .getAStmt ( ) or
103107 result = this .getAnArg ( ) or
104108 result = this .getVararg ( ) or
109+ result = this .getAKeywordOnlyArg ( ) or
105110 result = this .getKwarg ( )
106111 }
107112
@@ -185,6 +190,8 @@ class Parameter extends Parameter_ {
185190 f .getVararg ( ) = this
186191 or
187192 f .getKwarg ( ) = this
193+ or
194+ f .getAKeywordOnlyArg ( ) = this
188195 )
189196 }
190197
@@ -202,19 +209,31 @@ class Parameter extends Parameter_ {
202209
203210 /** Gets the expression for the default value of this parameter */
204211 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 )
210222 )
211223 }
212224
213225 /** Gets the annotation expression of this parameter */
214226 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 )
218237 )
219238 or
220239 exists ( Function f , Arguments args | args = f .getDefinition ( ) .getArgs ( ) |
@@ -228,7 +247,10 @@ class Parameter extends Parameter_ {
228247
229248 Variable getVariable ( ) { result .getAnAccess ( ) = this .asName ( ) }
230249
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+ */
232254 int getPosition ( ) { exists ( Function f | f .getArg ( result ) = this ) }
233255
234256 /** Gets the name of this parameter */
@@ -243,13 +265,13 @@ class Parameter extends Parameter_ {
243265 }
244266
245267 /**
246- * Holds if this parameter is a ' varargs' parameter.
268+ * Holds if this parameter is a " varargs" parameter.
247269 * The `varargs` in `f(a, b, *varargs)`.
248270 */
249271 predicate isVarargs ( ) { exists ( Function func | func .getVararg ( ) = this ) }
250272
251273 /**
252- * Holds if this parameter is a ' kwargs' parameter.
274+ * Holds if this parameter is a " kwargs" parameter.
253275 * The `kwargs` in `f(a, b, **kwargs)`.
254276 */
255277 predicate isKwargs ( ) { exists ( Function func | func .getKwarg ( ) = this ) }
@@ -258,7 +280,8 @@ class Parameter extends Parameter_ {
258280/** An expression that generates a callable object, either a function expression or a lambda */
259281abstract class CallableExpr extends Expr {
260282 /**
261- * Gets the parameters of this callable.
283+ * Gets The default values and annotations (type-hints) for the arguments of this callable.
284+ *
262285 * This predicate is called getArgs(), rather than getParameters() for compatibility with Python's AST module.
263286 */
264287 abstract Arguments getArgs ( ) ;
@@ -295,7 +318,7 @@ class FunctionExpr extends FunctionExpr_, CallableExpr {
295318 override Arguments getArgs ( ) { result = FunctionExpr_ .super .getArgs ( ) }
296319}
297320
298- /** A lambda expression, such as lambda x:x*x */
321+ /** A lambda expression, such as ` lambda x: x+1` */
299322class Lambda extends Lambda_ , CallableExpr {
300323 /** Gets the expression to the right of the colon in this lambda expression */
301324 Expr getExpression ( ) {
@@ -314,13 +337,36 @@ class Lambda extends Lambda_, CallableExpr {
314337 override Arguments getArgs ( ) { result = Lambda_ .super .getArgs ( ) }
315338}
316339
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+ */
318346class Arguments extends Arguments_ {
347+
319348 Expr getASubExpression ( ) {
349+ result = this .getADefault ( ) or
320350 result = this .getAKwDefault ( ) or
351+ //
321352 result = this .getAnAnnotation ( ) or
322- result = this .getKwargannotation ( ) or
323353 result = this .getVarargannotation ( ) or
324- result = this .getADefault ( )
354+ result = this .getAKwAnnotation ( ) or
355+ result = this .getKwargannotation ( )
325356 }
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 ) }
326372}
0 commit comments