mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Treat class C: ...
and class C(): ...
equivalently (#8659)
## Summary These should be seen as identical from the `ComparableAst` perspective.
This commit is contained in:
parent
a8e0d4ab4f
commit
345e1401cf
2 changed files with 17 additions and 30 deletions
|
@ -363,7 +363,7 @@ impl<'a> From<&'a ast::Number> for ComparableNumber<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
#[derive(Debug, Default, PartialEq, Eq, Hash)]
|
||||||
pub struct ComparableArguments<'a> {
|
pub struct ComparableArguments<'a> {
|
||||||
args: Vec<ComparableExpr<'a>>,
|
args: Vec<ComparableExpr<'a>>,
|
||||||
keywords: Vec<ComparableKeyword<'a>>,
|
keywords: Vec<ComparableKeyword<'a>>,
|
||||||
|
@ -1051,7 +1051,7 @@ pub struct StmtClassDef<'a> {
|
||||||
decorator_list: Vec<ComparableDecorator<'a>>,
|
decorator_list: Vec<ComparableDecorator<'a>>,
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
type_params: Option<ComparableTypeParams<'a>>,
|
type_params: Option<ComparableTypeParams<'a>>,
|
||||||
arguments: Option<ComparableArguments<'a>>,
|
arguments: ComparableArguments<'a>,
|
||||||
body: Vec<ComparableStmt<'a>>,
|
body: Vec<ComparableStmt<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1309,7 +1309,7 @@ impl<'a> From<&'a ast::Stmt> for ComparableStmt<'a> {
|
||||||
range: _,
|
range: _,
|
||||||
}) => Self::ClassDef(StmtClassDef {
|
}) => Self::ClassDef(StmtClassDef {
|
||||||
name: name.as_str(),
|
name: name.as_str(),
|
||||||
arguments: arguments.as_ref().map(Into::into),
|
arguments: arguments.as_ref().map(Into::into).unwrap_or_default(),
|
||||||
body: body.iter().map(Into::into).collect(),
|
body: body.iter().map(Into::into).collect(),
|
||||||
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
decorator_list: decorator_list.iter().map(Into::into).collect(),
|
||||||
type_params: type_params.as_ref().map(Into::into),
|
type_params: type_params.as_ref().map(Into::into),
|
||||||
|
|
|
@ -32,18 +32,7 @@ impl Normalizer {
|
||||||
|
|
||||||
impl Transformer for Normalizer {
|
impl Transformer for Normalizer {
|
||||||
fn visit_stmt(&self, stmt: &mut Stmt) {
|
fn visit_stmt(&self, stmt: &mut Stmt) {
|
||||||
match stmt {
|
if let Stmt::Delete(delete) = stmt {
|
||||||
Stmt::ClassDef(class_def) => {
|
|
||||||
// Treat `class C: ...` and `class C(): ...` equivalently.
|
|
||||||
if class_def
|
|
||||||
.arguments
|
|
||||||
.as_ref()
|
|
||||||
.is_some_and(|arguments| arguments.is_empty())
|
|
||||||
{
|
|
||||||
class_def.arguments = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Stmt::Delete(delete) => {
|
|
||||||
// Treat `del a, b` and `del (a, b)` equivalently.
|
// Treat `del a, b` and `del (a, b)` equivalently.
|
||||||
delete.targets = delete
|
delete.targets = delete
|
||||||
.targets
|
.targets
|
||||||
|
@ -58,8 +47,6 @@ impl Transformer for Normalizer {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
transformer::walk_stmt(self, stmt);
|
transformer::walk_stmt(self, stmt);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue