Skip to content

Commit f163098

Browse files
committed
Python: Cleanup default-indexing upgrade script
1 parent 010d5fb commit f163098

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

python/upgrades/f635b392038a494915307f913657cd3058f9b476/py_exprs.ql

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,18 @@ class Arguments_ extends @py_arguments {
6464
}
6565

6666
class Function_ extends @py_Function {
67-
/** Gets the name of this function. */
68-
string getName() { py_strs(result, this, 0) }
69-
7067
/** Gets the positional parameter list of this function. */
7168
ParameterList getArgs() { py_parameter_lists(result, this) }
7269

7370
/** Gets the nth positional parameter of this function. */
7471
Parameter_ getArg(int index) { result = this.getArgs().getItem(index) }
7572

76-
/** Gets the tuple (*) parameter of this function. */
77-
Expr_ getVararg() { py_exprs(result, _, this, 2) }
78-
7973
/** Gets the keyword-only parameter list of this function. */
8074
ExprList_ getKwonlyargs() { py_expr_lists(result, this, 3) }
8175

8276
/** Gets the nth keyword-only parameter of this function. */
8377
Expr_ getKwonlyarg(int index) { result = this.getKwonlyargs().getItem(index) }
8478

85-
/** Gets the dictionary (**) parameter of this function. */
86-
Expr_ getKwarg() { py_exprs(result, _, this, 4) }
87-
8879
string toString() { result = "Function" }
8980
}
9081

@@ -125,32 +116,42 @@ class FunctionExpr_ extends @py_FunctionExpr, CallableExprAdjusted, Expr_ {
125116
override string toString() { result = "FunctionExpr" }
126117
}
127118

128-
from Expr_ id, int kind, ExprParent_ parent, int oldidx, int newidx
119+
120+
/*
121+
* This upgrade changes the *layout* of the default values for parameters, by
122+
* making `Argument.getKwDefault(i)` return the default value for keyword-only parameter `i`
123+
* (instead of the i'th default for a keyword-only parameter). `Argument.getDefault` is
124+
* changed in the same manner to keep consistency.
125+
*/
126+
from Expr_ expr, int kind, ExprParent_ parent, int oldidx, int newidx
129127
where
130-
py_exprs(id, kind, parent, oldidx) and
128+
py_exprs(expr, kind, parent, oldidx) and
131129
(
132-
not exists(Arguments_ args | args.getDefault(oldidx) = id) and
133-
not exists(Arguments_ args | args.getKwDefault(oldidx) = id) and
130+
// expr is not a parameter default
131+
not exists(Arguments_ args | args.getDefault(oldidx) = expr) and
132+
not exists(Arguments_ args | args.getKwDefault(oldidx) = expr) and
134133
newidx = oldidx
135134
or
135+
// expr is a default for a normal parameter
136136
exists(Arguments_ args, CallableExprAdjusted callable |
137137
callable.getArgs() = args and
138-
args.getDefault(oldidx) = id and
138+
args.getDefault(oldidx) = expr and
139139
newidx = oldidx + count(callable.getInnerScope().getArg(_)) - count(args.getDefault(_))
140140
)
141141
or
142+
// expr is a default for a keyword-only parameter
142143
exists(Arguments_ args, CallableExprAdjusted callable |
143144
callable.getArgs() = args and
144-
args.getKwDefault(oldidx) = id and
145+
args.getKwDefault(oldidx) = expr and
145146
newidx =
146147
max(int i |
147148
exists(Parameter_ param | param = callable.getInnerScope().getKwonlyarg(i) |
148-
param.getLocation().getStartLine() < id.getLocation().getStartLine()
149+
param.getLocation().getStartLine() < expr.getLocation().getStartLine()
149150
or
150-
param.getLocation().getStartLine() = id.getLocation().getStartLine() and
151-
param.getLocation().getStartColumn() < id.getLocation().getStartColumn()
151+
param.getLocation().getStartLine() = expr.getLocation().getStartLine() and
152+
param.getLocation().getStartColumn() < expr.getLocation().getStartColumn()
152153
)
153154
)
154155
)
155156
)
156-
select id, kind, parent, newidx
157+
select expr, kind, parent, newidx
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
description: Support defaults for keyword-only parameters
1+
description: Changed indexing for parameter defaults
22
compatibility: full
33
py_exprs.rel: run py_exprs.qlo

0 commit comments

Comments
 (0)