Skip to content

Commit 8053e0e

Browse files
committed
Python: Allow list_splats as type annotations
That is, the `*T` in `def foo(*args : *T): ...`. This is apparently a piece of syntax we did not support correctly until now. In terms of the grammar, we simply add `list_splat` as a possible alternative for `type` (which could previously only be an `expression`). We also update `python.tsg` to not specify `expression` those places (as the relevant stanzas will then not work for `list_splat`s). This syntax is not supported by the old parser, hence we only add a new parser test for it.
1 parent fcec8e0 commit 8053e0e

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Module: [1, 0] - [3, 0]
2+
body: [
3+
Assign: [1, 0] - [1, 42]
4+
targets: [
5+
Name: [1, 4] - [1, 26]
6+
variable: Variable('tuple_typed_list_splat', None)
7+
ctx: Store
8+
]
9+
value:
10+
FunctionExpr: [1, 0] - [1, 42]
11+
name: 'tuple_typed_list_splat'
12+
args:
13+
arguments
14+
defaults: []
15+
kw_defaults: []
16+
annotations: []
17+
varargannotation:
18+
Starred: [1, 35] - [1, 40]
19+
value:
20+
Name: [1, 36] - [1, 40]
21+
variable: Variable('ARGS', None)
22+
ctx: Load
23+
ctx: Load
24+
kwargannotation: None
25+
kw_annotations: []
26+
returns: None
27+
inner_scope:
28+
Function: [1, 0] - [1, 42]
29+
name: 'tuple_typed_list_splat'
30+
type_parameters: []
31+
args: []
32+
vararg:
33+
Name: [1, 28] - [1, 32]
34+
variable: Variable('args', None)
35+
ctx: Param
36+
kwonlyargs: []
37+
kwarg: None
38+
body: [
39+
Pass: [2, 4] - [2, 8]
40+
]
41+
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def tuple_typed_list_splat(*args : *ARGS):
2+
pass

python/extractor/tsg-python/python.tsg

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3169,11 +3169,11 @@
31693169
(typed_parameter
31703170
(identifier) @name
31713171
.
3172-
type: (type (expression) @type)
3172+
type: (type (_) @type)
31733173
)
31743174
(typed_default_parameter
31753175
name: (_) @name
3176-
type: (type (expression) @type)
3176+
type: (type (_) @type)
31773177
value: (_) @value
31783178
)
31793179
] @param
@@ -3228,7 +3228,7 @@
32283228
(list_splat_pattern vararg: (_) @name) @starred
32293229
(typed_parameter
32303230
(list_splat_pattern vararg: (_) @name) @starred
3231-
type: (type (expression) @type)
3231+
type: (type (_) @type)
32323232
)
32333233
]
32343234
) @params
@@ -3245,7 +3245,7 @@
32453245

32463246
; Return type
32473247
(function_definition
3248-
return_type: (type (expression) @type)
3248+
return_type: (type (_) @type)
32493249
) @funcdef
32503250
{
32513251
attr (@funcdef.funcexpr) returns = @type.node
@@ -3259,7 +3259,7 @@
32593259
(dictionary_splat_pattern kwarg: (identifier) @name)
32603260
(typed_parameter
32613261
(dictionary_splat_pattern kwarg: (identifier) @name)
3262-
type: (type (expression) @type)
3262+
type: (type (_) @type)
32633263
)
32643264
]
32653265
) @params

python/extractor/tsg-python/tsp/grammar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ module.exports = grammar({
963963
field('type', $.type)
964964
)),
965965

966-
type: $ => $.expression,
966+
type: $ => choice($.list_splat, $.expression),
967967

968968
keyword_argument: $ => seq(
969969
field('name', choice($.identifier, $.keyword_identifier)),

0 commit comments

Comments
 (0)