Skip to content

Commit fff291a

Browse files
committed
Parse type parameters in function definitions
1 parent c33fbee commit fff291a

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
@@ -667,6 +667,30 @@ class Foo[X, Y: str, *U, **P]():
667667
";
668668
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
669669
}
670+
#[test]
671+
#[cfg(feature = "all-nodes-with-ranges")]
672+
fn test_parse_function_definition() {
673+
let source = "\
674+
def func(a):
675+
...
676+
677+
def func[T](a: T) -> T:
678+
...
679+
680+
def func[T: str](a: T) -> T:
681+
...
682+
683+
def func[T: (str, bytes)](a: T) -> T:
684+
...
685+
686+
def func[*Ts](*a: *Ts):
687+
...
688+
689+
def func[**P](*args: P.args, **kwargs: P.kwargs):
690+
...
691+
";
692+
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
693+
}
670694

671695
#[test]
672696
#[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)