mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-07 13:15:21 +00:00
Parse type parameters in function definitions (#96)
* Parse type parameters in function definitions * Add test for combined items
This commit is contained in:
parent
c33fbeef54
commit
6980037ad9
4 changed files with 5634 additions and 4116 deletions
|
@ -667,6 +667,33 @@ class Foo[X, Y: str, *U, **P]():
|
|||
";
|
||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
fn test_parse_function_definition() {
|
||||
let source = "\
|
||||
def func(a):
|
||||
...
|
||||
|
||||
def func[T](a: T) -> T:
|
||||
...
|
||||
|
||||
def func[T: str](a: T) -> T:
|
||||
...
|
||||
|
||||
def func[T: (str, bytes)](a: T) -> T:
|
||||
...
|
||||
|
||||
def func[*Ts](*a: *Ts):
|
||||
...
|
||||
|
||||
def func[**P](*args: P.args, **kwargs: P.kwargs):
|
||||
...
|
||||
|
||||
def func[T, U: str, *Ts, **P]():
|
||||
pass
|
||||
";
|
||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "all-nodes-with-ranges")]
|
||||
|
|
|
@ -965,16 +965,15 @@ WithItem<Goal>: ast::WithItem = {
|
|||
};
|
||||
|
||||
FuncDef: ast::Stmt = {
|
||||
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
|
||||
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <type_params:TypeParamList?> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
|
||||
let args = Box::new(args);
|
||||
let returns = r.map(|x| Box::new(x));
|
||||
let end_location = body.last().unwrap().end();
|
||||
let type_comment = None;
|
||||
let type_params = Vec::new();
|
||||
if is_async.is_some() {
|
||||
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
|
||||
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params: type_params.unwrap_or_default(), range: (location..end_location).into() }.into()
|
||||
} else {
|
||||
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
|
||||
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params: type_params.unwrap_or_default(), range: (location..end_location).into() }.into()
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
9156
parser/src/python.rs
generated
9156
parser/src/python.rs
generated
File diff suppressed because it is too large
Load diff
560
parser/src/snapshots/rustpython_parser__parser__tests__parse_function_definition.snap
generated
Normal file
560
parser/src/snapshots/rustpython_parser__parser__tests__parse_function_definition.snap
generated
Normal file
|
@ -0,0 +1,560 @@
|
|||
---
|
||||
source: parser/src/parser.rs
|
||||
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||
---
|
||||
[
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 0..20,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 9..10,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 9..10,
|
||||
def: Arg {
|
||||
range: 9..10,
|
||||
arg: Identifier(
|
||||
"a",
|
||||
),
|
||||
annotation: None,
|
||||
type_comment: None,
|
||||
},
|
||||
default: None,
|
||||
},
|
||||
],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 17..20,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 17..20,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: None,
|
||||
type_comment: None,
|
||||
type_params: [],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 22..53,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 34..38,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 34..38,
|
||||
def: Arg {
|
||||
range: 34..38,
|
||||
arg: Identifier(
|
||||
"a",
|
||||
),
|
||||
annotation: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 37..38,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
default: None,
|
||||
},
|
||||
],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 50..53,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 50..53,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 43..44,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 31..32,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: None,
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 55..91,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 72..76,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 72..76,
|
||||
def: Arg {
|
||||
range: 72..76,
|
||||
arg: Identifier(
|
||||
"a",
|
||||
),
|
||||
annotation: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 75..76,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
default: None,
|
||||
},
|
||||
],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 88..91,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 88..91,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 81..82,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 64..70,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 67..70,
|
||||
id: Identifier(
|
||||
"str",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 93..138,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 119..123,
|
||||
posonlyargs: [],
|
||||
args: [
|
||||
ArgWithDefault {
|
||||
range: 119..123,
|
||||
def: Arg {
|
||||
range: 119..123,
|
||||
arg: Identifier(
|
||||
"a",
|
||||
),
|
||||
annotation: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 122..123,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
default: None,
|
||||
},
|
||||
],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 135..138,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 135..138,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 128..129,
|
||||
id: Identifier(
|
||||
"T",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 102..117,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: Some(
|
||||
Tuple(
|
||||
ExprTuple {
|
||||
range: 105..117,
|
||||
elts: [
|
||||
Name(
|
||||
ExprName {
|
||||
range: 106..109,
|
||||
id: Identifier(
|
||||
"str",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
Name(
|
||||
ExprName {
|
||||
range: 111..116,
|
||||
id: Identifier(
|
||||
"bytes",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
],
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 140..171,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 154..161,
|
||||
posonlyargs: [],
|
||||
args: [],
|
||||
vararg: Some(
|
||||
Arg {
|
||||
range: 155..161,
|
||||
arg: Identifier(
|
||||
"a",
|
||||
),
|
||||
annotation: Some(
|
||||
Starred(
|
||||
ExprStarred {
|
||||
range: 158..161,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 159..161,
|
||||
id: Identifier(
|
||||
"Ts",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
),
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 168..171,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 168..171,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: None,
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
TypeVarTuple(
|
||||
TypeParamTypeVarTuple {
|
||||
range: 149..152,
|
||||
name: Identifier(
|
||||
"Ts",
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 173..230,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 187..220,
|
||||
posonlyargs: [],
|
||||
args: [],
|
||||
vararg: Some(
|
||||
Arg {
|
||||
range: 188..200,
|
||||
arg: Identifier(
|
||||
"args",
|
||||
),
|
||||
annotation: Some(
|
||||
Attribute(
|
||||
ExprAttribute {
|
||||
range: 194..200,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 194..195,
|
||||
id: Identifier(
|
||||
"P",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
attr: Identifier(
|
||||
"args",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
),
|
||||
kwonlyargs: [],
|
||||
kwarg: Some(
|
||||
Arg {
|
||||
range: 204..220,
|
||||
arg: Identifier(
|
||||
"kwargs",
|
||||
),
|
||||
annotation: Some(
|
||||
Attribute(
|
||||
ExprAttribute {
|
||||
range: 212..220,
|
||||
value: Name(
|
||||
ExprName {
|
||||
range: 212..213,
|
||||
id: Identifier(
|
||||
"P",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
attr: Identifier(
|
||||
"kwargs",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
type_comment: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
body: [
|
||||
Expr(
|
||||
StmtExpr {
|
||||
range: 227..230,
|
||||
value: Constant(
|
||||
ExprConstant {
|
||||
range: 227..230,
|
||||
value: Ellipsis,
|
||||
kind: None,
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: None,
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
ParamSpec(
|
||||
TypeParamParamSpec {
|
||||
range: 182..185,
|
||||
name: Identifier(
|
||||
"P",
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
FunctionDef(
|
||||
StmtFunctionDef {
|
||||
range: 232..273,
|
||||
name: Identifier(
|
||||
"func",
|
||||
),
|
||||
args: Arguments {
|
||||
range: 261..263,
|
||||
posonlyargs: [],
|
||||
args: [],
|
||||
vararg: None,
|
||||
kwonlyargs: [],
|
||||
kwarg: None,
|
||||
},
|
||||
body: [
|
||||
Pass(
|
||||
StmtPass {
|
||||
range: 269..273,
|
||||
},
|
||||
),
|
||||
],
|
||||
decorator_list: [],
|
||||
returns: None,
|
||||
type_comment: None,
|
||||
type_params: [
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 241..242,
|
||||
name: Identifier(
|
||||
"T",
|
||||
),
|
||||
bound: None,
|
||||
},
|
||||
),
|
||||
TypeVar(
|
||||
TypeParamTypeVar {
|
||||
range: 244..250,
|
||||
name: Identifier(
|
||||
"U",
|
||||
),
|
||||
bound: Some(
|
||||
Name(
|
||||
ExprName {
|
||||
range: 247..250,
|
||||
id: Identifier(
|
||||
"str",
|
||||
),
|
||||
ctx: Load,
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
),
|
||||
TypeVarTuple(
|
||||
TypeParamTypeVarTuple {
|
||||
range: 252..255,
|
||||
name: Identifier(
|
||||
"Ts",
|
||||
),
|
||||
},
|
||||
),
|
||||
ParamSpec(
|
||||
TypeParamParamSpec {
|
||||
range: 257..260,
|
||||
name: Identifier(
|
||||
"P",
|
||||
),
|
||||
},
|
||||
),
|
||||
],
|
||||
},
|
||||
),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue