Python: Handle keyword-only arguments properly

This commit is contained in:
Rasmus Wriedt Larsen
2020-04-27 14:57:14 +02:00
parent 4185edc087
commit c508e89a00
5 changed files with 27 additions and 3 deletions

View File

@@ -49,7 +49,11 @@ class Function extends Function_, Scope, AstNode {
string getArgName(int index) { result = this.getArg(index).(Name).getId() }
Parameter getArgByName(string name) {
result = this.getAnArg() and
(
result = this.getAnArg()
or
result = this.getAKeywordOnlyArg()
) and
result.(Name).getId() = name
}
@@ -102,6 +106,7 @@ class Function extends Function_, Scope, AstNode {
result = this.getAStmt() or
result = this.getAnArg() or
result = this.getVararg() or
result = this.getAKeywordOnlyArg() or
result = this.getKwarg()
}
@@ -185,6 +190,8 @@ class Parameter extends Parameter_ {
f.getVararg() = this
or
f.getKwarg() = this
or
f.getAKeywordOnlyArg() = this
)
}
@@ -322,11 +329,14 @@ class Lambda extends Lambda_, CallableExpr {
* that is generally only used for type hints today (PEP 484).
*/
class Arguments extends Arguments_ {
Expr getASubExpression() {
result = this.getADefault() or
result = this.getAKwDefault() or
//
result = this.getAnAnnotation() or
result = this.getKwargannotation() or
result = this.getVarargannotation() or
result = this.getADefault()
result = this.getAKwAnnotation() or
result = this.getKwargannotation()
}
}

View File

@@ -1,6 +1,7 @@
| test.py:4:1:11:2 | Function func | test.py:5:5:5:12 | pos_only |
| test.py:4:1:11:2 | Function func | test.py:7:5:7:10 | normal |
| test.py:4:1:11:2 | Function func | test.py:8:6:8:9 | args |
| test.py:4:1:11:2 | Function func | test.py:9:5:9:16 | keyword_only |
| test.py:4:1:11:2 | Function func | test.py:10:7:10:12 | kwargs |
| test.py:4:1:11:2 | Function func | test.py:12:5:12:41 | ExprStmt |
| test.py:4:1:11:2 | Function func | test.py:13:5:13:15 | ExprStmt |
@@ -8,5 +9,8 @@
| test.py:23:1:31:2 | Function func2 | test.py:24:5:24:11 | pos_req |
| test.py:23:1:31:2 | Function func2 | test.py:25:5:25:17 | pos_w_default |
| test.py:23:1:31:2 | Function func2 | test.py:26:5:26:18 | pos_w_default2 |
| test.py:23:1:31:2 | Function func2 | test.py:28:5:28:15 | keyword_req |
| test.py:23:1:31:2 | Function func2 | test.py:29:5:29:21 | keyword_w_default |
| test.py:23:1:31:2 | Function func2 | test.py:30:5:30:20 | keyword_also_req |
| test.py:23:1:31:2 | Function func2 | test.py:32:5:32:18 | ExprStmt |
| test.py:23:1:31:2 | Function func2 | test.py:33:5:40:5 | ExprStmt |

View File

@@ -1,5 +1,9 @@
| test.py:4:1:11:2 | Function func | keyword_only | test.py:9:5:9:16 | Parameter |
| test.py:4:1:11:2 | Function func | normal | test.py:7:5:7:10 | Parameter |
| test.py:4:1:11:2 | Function func | pos_only | test.py:5:5:5:12 | Parameter |
| test.py:23:1:31:2 | Function func2 | keyword_also_req | test.py:30:5:30:20 | Parameter |
| test.py:23:1:31:2 | Function func2 | keyword_req | test.py:28:5:28:15 | Parameter |
| test.py:23:1:31:2 | Function func2 | keyword_w_default | test.py:29:5:29:21 | Parameter |
| test.py:23:1:31:2 | Function func2 | pos_req | test.py:24:5:24:11 | Parameter |
| test.py:23:1:31:2 | Function func2 | pos_w_default | test.py:25:5:25:17 | Parameter |
| test.py:23:1:31:2 | Function func2 | pos_w_default2 | test.py:26:5:26:18 | Parameter |

View File

@@ -3,9 +3,11 @@
| test.py:4:1:11:2 | FunctionExpr | test.py:7:13:7:15 | int |
| test.py:4:1:11:2 | FunctionExpr | test.py:7:19:7:20 | UnaryExpr |
| test.py:4:1:11:2 | FunctionExpr | test.py:8:12:8:23 | Str |
| test.py:4:1:11:2 | FunctionExpr | test.py:9:19:9:21 | int |
| test.py:4:1:11:2 | FunctionExpr | test.py:9:25:9:26 | UnaryExpr |
| test.py:4:1:11:2 | FunctionExpr | test.py:10:15:10:30 | Str |
| test.py:23:1:31:2 | FunctionExpr | test.py:25:20:25:24 | Str |
| test.py:23:1:31:2 | FunctionExpr | test.py:25:28:25:31 | None |
| test.py:23:1:31:2 | FunctionExpr | test.py:26:20:26:23 | None |
| test.py:23:1:31:2 | FunctionExpr | test.py:29:24:29:28 | Str |
| test.py:23:1:31:2 | FunctionExpr | test.py:29:32:29:35 | None |

View File

@@ -1,4 +1,8 @@
| args | varargs |
| keyword_also_req | normal |
| keyword_only | normal |
| keyword_req | normal |
| keyword_w_default | normal |
| kwargs | kwargs |
| normal | normal |
| pos_only | normal |