Skip to content

Parse type parameters in class definitions #95

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 5 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
32 changes: 32 additions & 0 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,38 @@ class Foo(A, B):
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
}

#[test]
#[cfg(feature = "all-nodes-with-ranges")]
fn test_parse_class_generic_types() {
let source = "\
# TypeVar
class Foo[T](): ...

# TypeVar with bound
class Foo[T: str](): ...

# TypeVar with tuple bound
class Foo[T: (str, bytes)](): ...

# Multiple TypeVar
class Foo[T, U](): ...

# Trailing comma
class Foo[T, U,](): ...

# TypeVarTuple
class Foo[*Ts](): ...

# ParamSpec
class Foo[**P](): ...

# Mixed types
class Foo[X, Y: str, *U, **P]():
pass
";
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
}

#[test]
#[cfg(feature = "all-nodes-with-ranges")]
fn test_parse_dict_comprehension() {
Expand Down
30 changes: 27 additions & 3 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -1126,27 +1126,51 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
};

ClassDef: ast::Stmt = {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
<decorator_list:Decorator*> <location:@L> "class" <name:Identifier> <type_params:TypeParamList?> <a:("(" ArgumentList ")")?> ":" <body:Suite> => {
let (bases, keywords) = match a {
Some((_, arg, _)) => (arg.args, arg.keywords),
None => (vec![], vec![]),
};
let end_location = body.last().unwrap().end();
let type_params = Vec::new();
ast::Stmt::ClassDef(
ast::StmtClassDef {
name,
bases,
keywords,
body,
decorator_list,
type_params,
type_params: type_params.unwrap_or_default(),
range: (location..end_location).into()
},
)
},
};


TypeParamList: Vec<ast::TypeParam> = {
<location:@L> "[" <vars:OneOrMore<TypeParam>> ","? "]" <end_location:@R> => {
vars
}
};

TypeParam: ast::TypeParam = {
<location:@L> <name:Identifier> <bound:(":" <Test<"all">>)?> <end_location:@R> => {
ast::TypeParam::TypeVar(
ast::TypeParamTypeVar { name, bound: bound.map(Box::new), range: (location..end_location).into() }
)
},
<location:@L> "*" <name:Identifier> <end_location:@R> => {
ast::TypeParam::TypeVarTuple(
ast::TypeParamTypeVarTuple { name, range: (location..end_location).into() }
)
},
<location:@L> "**" <name:Identifier> <end_location:@R> => {
ast::TypeParam::ParamSpec(
ast::TypeParamParamSpec { name, range: (location..end_location).into() }
)
}
};

// Decorators:
Decorator: ast::Expr = {
<location:@L> "@" <p:NamedExpressionTest> "\n" => {
Expand Down
Loading