mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39:22 +00:00
Parse type parameters in class definitions
This commit is contained in:
commit
c33fbeef54
4 changed files with 24271 additions and 22583 deletions
|
@ -636,6 +636,38 @@ class Foo(A, B):
|
||||||
insta::assert_debug_snapshot!(ast::Suite::parse(source, "<test>").unwrap());
|
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]
|
#[test]
|
||||||
#[cfg(feature = "all-nodes-with-ranges")]
|
#[cfg(feature = "all-nodes-with-ranges")]
|
||||||
fn test_parse_dict_comprehension() {
|
fn test_parse_dict_comprehension() {
|
||||||
|
|
|
@ -1126,13 +1126,12 @@ KwargParameter<ArgType>: Option<Box<ast::Arg>> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
ClassDef: ast::Stmt = {
|
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 {
|
let (bases, keywords) = match a {
|
||||||
Some((_, arg, _)) => (arg.args, arg.keywords),
|
Some((_, arg, _)) => (arg.args, arg.keywords),
|
||||||
None => (vec![], vec![]),
|
None => (vec![], vec![]),
|
||||||
};
|
};
|
||||||
let end_location = body.last().unwrap().end();
|
let end_location = body.last().unwrap().end();
|
||||||
let type_params = Vec::new();
|
|
||||||
ast::Stmt::ClassDef(
|
ast::Stmt::ClassDef(
|
||||||
ast::StmtClassDef {
|
ast::StmtClassDef {
|
||||||
name,
|
name,
|
||||||
|
@ -1140,13 +1139,38 @@ ClassDef: ast::Stmt = {
|
||||||
keywords,
|
keywords,
|
||||||
body,
|
body,
|
||||||
decorator_list,
|
decorator_list,
|
||||||
type_params,
|
type_params: type_params.unwrap_or_default(),
|
||||||
range: (location..end_location).into()
|
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:
|
// Decorators:
|
||||||
Decorator: ast::Expr = {
|
Decorator: ast::Expr = {
|
||||||
<location:@L> "@" <p:NamedExpressionTest> "\n" => {
|
<location:@L> "@" <p:NamedExpressionTest> "\n" => {
|
||||||
|
|
46417
parser/src/python.rs
generated
46417
parser/src/python.rs
generated
File diff suppressed because it is too large
Load diff
375
parser/src/snapshots/rustpython_parser__parser__tests__parse_class_generic_types.snap
generated
Normal file
375
parser/src/snapshots/rustpython_parser__parser__tests__parse_class_generic_types.snap
generated
Normal file
|
@ -0,0 +1,375 @@
|
||||||
|
---
|
||||||
|
source: parser/src/parser.rs
|
||||||
|
expression: "ast::Suite::parse(source, \"<test>\").unwrap()"
|
||||||
|
---
|
||||||
|
[
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 10..29,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 26..29,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 26..29,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 20..21,
|
||||||
|
name: Identifier(
|
||||||
|
"T",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 52..76,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 73..76,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 73..76,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 62..68,
|
||||||
|
name: Identifier(
|
||||||
|
"T",
|
||||||
|
),
|
||||||
|
bound: Some(
|
||||||
|
Name(
|
||||||
|
ExprName {
|
||||||
|
range: 65..68,
|
||||||
|
id: Identifier(
|
||||||
|
"str",
|
||||||
|
),
|
||||||
|
ctx: Load,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 105..138,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 135..138,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 135..138,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 115..130,
|
||||||
|
name: Identifier(
|
||||||
|
"T",
|
||||||
|
),
|
||||||
|
bound: Some(
|
||||||
|
Tuple(
|
||||||
|
ExprTuple {
|
||||||
|
range: 118..130,
|
||||||
|
elts: [
|
||||||
|
Name(
|
||||||
|
ExprName {
|
||||||
|
range: 119..122,
|
||||||
|
id: Identifier(
|
||||||
|
"str",
|
||||||
|
),
|
||||||
|
ctx: Load,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
Name(
|
||||||
|
ExprName {
|
||||||
|
range: 124..129,
|
||||||
|
id: Identifier(
|
||||||
|
"bytes",
|
||||||
|
),
|
||||||
|
ctx: Load,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
ctx: Load,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 159..181,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 178..181,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 178..181,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 169..170,
|
||||||
|
name: Identifier(
|
||||||
|
"T",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 172..173,
|
||||||
|
name: Identifier(
|
||||||
|
"U",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 200..223,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 220..223,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 220..223,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 210..211,
|
||||||
|
name: Identifier(
|
||||||
|
"T",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 213..214,
|
||||||
|
name: Identifier(
|
||||||
|
"U",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 240..261,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 258..261,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 258..261,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVarTuple(
|
||||||
|
TypeParamTypeVarTuple {
|
||||||
|
range: 250..253,
|
||||||
|
name: Identifier(
|
||||||
|
"Ts",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 275..296,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Expr(
|
||||||
|
StmtExpr {
|
||||||
|
range: 293..296,
|
||||||
|
value: Constant(
|
||||||
|
ExprConstant {
|
||||||
|
range: 293..296,
|
||||||
|
value: Ellipsis,
|
||||||
|
kind: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
ParamSpec(
|
||||||
|
TypeParamParamSpec {
|
||||||
|
range: 285..288,
|
||||||
|
name: Identifier(
|
||||||
|
"P",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ClassDef(
|
||||||
|
StmtClassDef {
|
||||||
|
range: 312..351,
|
||||||
|
name: Identifier(
|
||||||
|
"Foo",
|
||||||
|
),
|
||||||
|
bases: [],
|
||||||
|
keywords: [],
|
||||||
|
body: [
|
||||||
|
Pass(
|
||||||
|
StmtPass {
|
||||||
|
range: 347..351,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
decorator_list: [],
|
||||||
|
type_params: [
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 322..323,
|
||||||
|
name: Identifier(
|
||||||
|
"X",
|
||||||
|
),
|
||||||
|
bound: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TypeVar(
|
||||||
|
TypeParamTypeVar {
|
||||||
|
range: 325..331,
|
||||||
|
name: Identifier(
|
||||||
|
"Y",
|
||||||
|
),
|
||||||
|
bound: Some(
|
||||||
|
Name(
|
||||||
|
ExprName {
|
||||||
|
range: 328..331,
|
||||||
|
id: Identifier(
|
||||||
|
"str",
|
||||||
|
),
|
||||||
|
ctx: Load,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
TypeVarTuple(
|
||||||
|
TypeParamTypeVarTuple {
|
||||||
|
range: 333..335,
|
||||||
|
name: Identifier(
|
||||||
|
"U",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ParamSpec(
|
||||||
|
TypeParamParamSpec {
|
||||||
|
range: 337..340,
|
||||||
|
name: Identifier(
|
||||||
|
"P",
|
||||||
|
),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
Loading…
Add table
Add a link
Reference in a new issue