mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Implement Comparable
for type aliases and parameters (#5865)
Part of https://github.com/astral-sh/ruff/issues/5062
This commit is contained in:
parent
cdfed3d50e
commit
d5c43a45b3
1 changed files with 78 additions and 7 deletions
|
@ -935,6 +935,7 @@ pub struct StmtFunctionDef<'a> {
|
|||
args: ComparableArguments<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
}
|
||||
|
@ -945,6 +946,7 @@ pub struct StmtAsyncFunctionDef<'a> {
|
|||
args: ComparableArguments<'a>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
returns: Option<ComparableExpr<'a>>,
|
||||
type_comment: Option<&'a str>,
|
||||
}
|
||||
|
@ -956,6 +958,7 @@ pub struct StmtClassDef<'a> {
|
|||
keywords: Vec<ComparableKeyword<'a>>,
|
||||
body: Vec<ComparableStmt<'a>>,
|
||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||
type_params: Vec<ComparableTypeParam<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
|
@ -968,6 +971,61 @@ pub struct StmtDelete<'a> {
|
|||
targets: Vec<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtTypeAlias<'a> {
|
||||
pub name: Box<ComparableExpr<'a>>,
|
||||
pub type_params: Vec<ComparableTypeParam<'a>>,
|
||||
pub value: Box<ComparableExpr<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ComparableTypeParam<'a> {
|
||||
TypeVar(TypeParamTypeVar<'a>),
|
||||
ParamSpec(TypeParamParamSpec<'a>),
|
||||
TypeVarTuple(TypeParamTypeVarTuple<'a>),
|
||||
}
|
||||
|
||||
impl<'a> From<&'a ast::TypeParam> for ComparableTypeParam<'a> {
|
||||
fn from(type_param: &'a ast::TypeParam) -> Self {
|
||||
match type_param {
|
||||
ast::TypeParam::TypeVar(ast::TypeParamTypeVar {
|
||||
name,
|
||||
bound,
|
||||
range: _,
|
||||
}) => Self::TypeVar(TypeParamTypeVar {
|
||||
name: name.as_str(),
|
||||
bound: bound.as_ref().map(Into::into),
|
||||
}),
|
||||
ast::TypeParam::TypeVarTuple(ast::TypeParamTypeVarTuple { name, range: _ }) => {
|
||||
Self::TypeVarTuple(TypeParamTypeVarTuple {
|
||||
name: name.as_str(),
|
||||
})
|
||||
}
|
||||
ast::TypeParam::ParamSpec(ast::TypeParamParamSpec { name, range: _ }) => {
|
||||
Self::ParamSpec(TypeParamParamSpec {
|
||||
name: name.as_str(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct TypeParamTypeVar<'a> {
|
||||
pub name: &'a str,
|
||||
pub bound: Option<Box<ComparableExpr<'a>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct TypeParamParamSpec<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct TypeParamTypeVarTuple<'a> {
|
||||
pub name: &'a str,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StmtAssign<'a> {
|
||||
targets: Vec<ComparableExpr<'a>>,
|
||||
|
@ -1117,6 +1175,7 @@ pub enum ComparableStmt<'a> {
|
|||
Raise(StmtRaise<'a>),
|
||||
Try(StmtTry<'a>),
|
||||
TryStar(StmtTryStar<'a>),
|
||||
TypeAlias(StmtTypeAlias<'a>),
|
||||
Assert(StmtAssert<'a>),
|
||||
Import(StmtImport<'a>),
|
||||
ImportFrom(StmtImportFrom<'a>),
|
||||
|
@ -1138,8 +1197,8 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
decorator_list,
|
||||
returns,
|
||||
type_comment,
|
||||
range: _,
|
||||
type_params: _,
|
||||
type_params,
|
||||
range: _range,
|
||||
}) => Self::FunctionDef(StmtFunctionDef {
|
||||
name: name.as_str(),
|
||||
args: args.into(),
|
||||
|
@ -1147,6 +1206,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::AsyncFunctionDef(ast::StmtAsyncFunctionDef {
|
||||
name,
|
||||
|
@ -1155,8 +1215,8 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
decorator_list,
|
||||
returns,
|
||||
type_comment,
|
||||
range: _,
|
||||
type_params: _,
|
||||
type_params,
|
||||
range: _range,
|
||||
}) => Self::AsyncFunctionDef(StmtAsyncFunctionDef {
|
||||
name: name.as_str(),
|
||||
args: args.into(),
|
||||
|
@ -1164,6 +1224,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||
returns: returns.as_ref().map(Into::into),
|
||||
type_comment: type_comment.as_ref().map(String::as_str),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::ClassDef(ast::StmtClassDef {
|
||||
name,
|
||||
|
@ -1171,14 +1232,15 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
keywords,
|
||||
body,
|
||||
decorator_list,
|
||||
range: _,
|
||||
type_params: _,
|
||||
type_params,
|
||||
range: _range,
|
||||
}) => Self::ClassDef(StmtClassDef {
|
||||
name: name.as_str(),
|
||||
bases: bases.iter().map(Into::into).collect(),
|
||||
keywords: keywords.iter().map(Into::into).collect(),
|
||||
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(),
|
||||
}),
|
||||
ast::Stmt::Return(ast::StmtReturn {
|
||||
value,
|
||||
|
@ -1192,6 +1254,16 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
}) => Self::Delete(StmtDelete {
|
||||
targets: targets.iter().map(Into::into).collect(),
|
||||
}),
|
||||
ast::Stmt::TypeAlias(ast::StmtTypeAlias {
|
||||
range: _range,
|
||||
name,
|
||||
type_params,
|
||||
value,
|
||||
}) => Self::TypeAlias(StmtTypeAlias {
|
||||
name: name.into(),
|
||||
type_params: type_params.iter().map(Into::into).collect(),
|
||||
value: value.into(),
|
||||
}),
|
||||
ast::Stmt::Assign(ast::StmtAssign {
|
||||
targets,
|
||||
value,
|
||||
|
@ -1377,7 +1449,6 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
|||
ast::Stmt::Pass(_) => Self::Pass,
|
||||
ast::Stmt::Break(_) => Self::Break,
|
||||
ast::Stmt::Continue(_) => Self::Continue,
|
||||
ast::Stmt::TypeAlias(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue