Skip to content

Commit 7b31e72

Browse files
committed
Parse type parameters in function definitions
1 parent 31767e2 commit 7b31e72

File tree

3 files changed

+4143
-3188
lines changed

3 files changed

+4143
-3188
lines changed

parser/src/parser.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,30 @@ class Foo[X, Y: str, *U, **P](A, B):
706706
";
707707
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
708708
}
709+
#[test]
710+
#[cfg(feature = "all-nodes-with-ranges")]
711+
fn test_parse_function_definition() {
712+
let source = "\
713+
def func(a):
714+
...
715+
716+
def func[T](a: T) -> T:
717+
...
718+
719+
def func[T: str](a: T) -> T:
720+
...
721+
722+
def func[T: (str, bytes)](a: T) -> T:
723+
...
724+
725+
def func[*Ts](*a: *Ts):
726+
...
727+
728+
def func[**P](*args: P.args, **kwargs: P.kwargs):
729+
...
730+
";
731+
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
732+
}
709733

710734
#[test]
711735
#[cfg(feature = "all-nodes-with-ranges")]

parser/src/python.lalrpop

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -965,16 +965,15 @@ WithItem<Goal>: ast::WithItem = {
965965
};
966966

967967
FuncDef: ast::Stmt = {
968-
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
968+
<decorator_list:Decorator*> <location:@L> <is_async:"async"?> "def" <name:Identifier> <type_params:TypeParamList?> <args:Parameters> <r:("->" <Test<"all">>)?> ":" <body:Suite> => {
969969
let args = Box::new(args);
970970
let returns = r.map(|x| Box::new(x));
971971
let end_location = body.last().unwrap().end();
972972
let type_comment = None;
973-
let type_params = Vec::new();
974973
if is_async.is_some() {
975-
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
974+
ast::StmtAsyncFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params: type_params.unwrap_or_default(), range: (location..end_location).into() }.into()
976975
} else {
977-
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params, range: (location..end_location).into() }.into()
976+
ast::StmtFunctionDef { name, args, body, decorator_list, returns, type_comment, type_params: type_params.unwrap_or_default(), range: (location..end_location).into() }.into()
978977
}
979978
},
980979
};

0 commit comments

Comments
 (0)