Skip to content

Parse type parameters in function definitions #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
7 changes: 3 additions & 4 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
},
};
Expand Down
Loading