mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:43 +00:00
Add a TypeParams
node to the AST (#6261)
## Summary Similar to #6259, this PR adds a `TypeParams` node to the AST, to capture the list of type parameters with their surrounding brackets. If a statement lacks type parameters, the `type_params` field will be `None`.
This commit is contained in:
parent
981e64f82b
commit
b095b7204b
31 changed files with 1104 additions and 866 deletions
|
@ -969,7 +969,7 @@ pub struct StmtFunctionDef<'a> {
|
|||
parameters: ComparableParameters<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
type_params: Option<ComparableTypeParams<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -979,7 +979,7 @@ pub struct StmtAsyncFunctionDef<'a> {
|
|||
parameters: ComparableParameters<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
type_params: Option<ComparableTypeParams<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
|
@ -989,7 +989,7 @@ pub struct StmtClassDef<'a> {
|
|||
arguments: Option<ComparableArguments<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
type_params: Option<ComparableTypeParams<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
@ -1005,10 +1005,23 @@ pub struct StmtDelete<'a> {
|
|||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtTypeAlias<'a> {
|
||||
pub name: Box<ComparableExpr<'a>>,
|
||||
pub type_params: Vec<ComparableTypeParam<'a>>,
|
||||
pub type_params: Option<ComparableTypeParams<'a>>,
|
||||
pub value: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct ComparableTypeParams<'a> {
|
||||
pub type_params: Vec<ComparableTypeParam<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::TypeParams> for ComparableTypeParams<'a> {
|
||||
fn from(type_params: &'a ast::TypeParams) -> Self {
|
||||
Self {
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableTypeParam<'a> {
|
||||
TypeVar(TypeParamTypeVar<'a>),
|
||||
|
@ -1237,7 +1250,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
type_params: type_params.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
|
@ -1253,7 +1266,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
type_params: type_params.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Stmt::ClassDef(ast::StmtClassDef {
|
||||
name,
|
||||
|
@ -1267,7 +1280,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
arguments: arguments.as_ref().map(Into::into),
|
||||
body: body.iter().map(Into::into).collect(),
|
||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
type_params: type_params.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::Stmt::Return(ast::StmtReturn {
|
||||
value,
|
||||
|
@ -1288,7 +1301,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
value,
|
||||
}) => Self::TypeAlias(StmtTypeAlias {
|
||||
name: name.into(),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
type_params: type_params.as_ref().map(Into::into),
|
||||
value: value.into(),
|
||||
}),
|
||||
ast::Stmt::Assign(ast::StmtAssign {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue