mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
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.
This commit is contained in:
41
python/extractor/tests/parser/functions_new.expected
Normal file
41
python/extractor/tests/parser/functions_new.expected
Normal file
@@ -0,0 +1,41 @@
|
||||
Module: [1, 0] - [3, 0]
|
||||
body: [
|
||||
Assign: [1, 0] - [1, 42]
|
||||
targets: [
|
||||
Name: [1, 4] - [1, 26]
|
||||
variable: Variable('tuple_typed_list_splat', None)
|
||||
ctx: Store
|
||||
]
|
||||
value:
|
||||
FunctionExpr: [1, 0] - [1, 42]
|
||||
name: 'tuple_typed_list_splat'
|
||||
args:
|
||||
arguments
|
||||
defaults: []
|
||||
kw_defaults: []
|
||||
annotations: []
|
||||
varargannotation:
|
||||
Starred: [1, 35] - [1, 40]
|
||||
value:
|
||||
Name: [1, 36] - [1, 40]
|
||||
variable: Variable('ARGS', None)
|
||||
ctx: Load
|
||||
ctx: Load
|
||||
kwargannotation: None
|
||||
kw_annotations: []
|
||||
returns: None
|
||||
inner_scope:
|
||||
Function: [1, 0] - [1, 42]
|
||||
name: 'tuple_typed_list_splat'
|
||||
type_parameters: []
|
||||
args: []
|
||||
vararg:
|
||||
Name: [1, 28] - [1, 32]
|
||||
variable: Variable('args', None)
|
||||
ctx: Param
|
||||
kwonlyargs: []
|
||||
kwarg: None
|
||||
body: [
|
||||
Pass: [2, 4] - [2, 8]
|
||||
]
|
||||
]
|
||||
2
python/extractor/tests/parser/functions_new.py
Normal file
2
python/extractor/tests/parser/functions_new.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def tuple_typed_list_splat(*args : *ARGS):
|
||||
pass
|
||||
@@ -3169,11 +3169,11 @@
|
||||
(typed_parameter
|
||||
(identifier) @name
|
||||
.
|
||||
type: (type (expression) @type)
|
||||
type: (type (_) @type)
|
||||
)
|
||||
(typed_default_parameter
|
||||
name: (_) @name
|
||||
type: (type (expression) @type)
|
||||
type: (type (_) @type)
|
||||
value: (_) @value
|
||||
)
|
||||
] @param
|
||||
@@ -3228,7 +3228,7 @@
|
||||
(list_splat_pattern vararg: (_) @name) @starred
|
||||
(typed_parameter
|
||||
(list_splat_pattern vararg: (_) @name) @starred
|
||||
type: (type (expression) @type)
|
||||
type: (type (_) @type)
|
||||
)
|
||||
]
|
||||
) @params
|
||||
@@ -3245,7 +3245,7 @@
|
||||
|
||||
; Return type
|
||||
(function_definition
|
||||
return_type: (type (expression) @type)
|
||||
return_type: (type (_) @type)
|
||||
) @funcdef
|
||||
{
|
||||
attr (@funcdef.funcexpr) returns = @type.node
|
||||
@@ -3259,7 +3259,7 @@
|
||||
(dictionary_splat_pattern kwarg: (identifier) @name)
|
||||
(typed_parameter
|
||||
(dictionary_splat_pattern kwarg: (identifier) @name)
|
||||
type: (type (expression) @type)
|
||||
type: (type (_) @type)
|
||||
)
|
||||
]
|
||||
) @params
|
||||
|
||||
@@ -963,7 +963,7 @@ module.exports = grammar({
|
||||
field('type', $.type)
|
||||
)),
|
||||
|
||||
type: $ => $.expression,
|
||||
type: $ => choice($.list_splat, $.expression),
|
||||
|
||||
keyword_argument: $ => seq(
|
||||
field('name', choice($.identifier, $.keyword_identifier)),
|
||||
|
||||
Reference in New Issue
Block a user