Skip to content

Commit

Permalink
Return a slice in StmtClassDef#bases (#6311)
Browse files Browse the repository at this point in the history
Slices are strictly more flexible, since you can always convert to an
iterator, etc., but not the other way around. Suggested in
#6259 (comment).
  • Loading branch information
charliermarsh authored Aug 3, 2023
1 parent 718e394 commit 2fa5087
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub(crate) fn builtin_attribute_shadowing(
// subscripting and not through attribute access.
if class_def
.bases()
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "TypedDict"))
{
return;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_pie/rules/non_unique_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) fn non_unique_enums(checker: &mut Checker, parent: &Stmt, body: &[Stm
return;
};

if !parent.bases().any(|expr| {
if !parent.bases().iter().any(|expr| {
checker
.semantic()
.resolve_call_path(expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub(crate) fn unused_private_protocol(

if !class_def
.bases()
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "Protocol"))
{
continue;
Expand Down Expand Up @@ -309,6 +310,7 @@ pub(crate) fn unused_private_typed_dict(

if !class_def
.bases()
.iter()
.any(|base| checker.semantic().match_typing_expr(base, "TypedDict"))
{
continue;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/src/rules/flake8_type_checking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn runtime_evaluated_base_class(base_classes: &[String], semantic: &SemanticMode
return false;
};

class_def.bases().any(|base| {
class_def.bases().iter().any(|base| {
semantic.resolve_call_path(base).is_some_and(|call_path| {
base_classes
.iter()
Expand Down
22 changes: 10 additions & 12 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,19 @@ pub struct StmtClassDef {

impl StmtClassDef {
/// Return an iterator over the bases of the class.
pub fn bases(&self) -> impl Iterator<Item = &Expr> {
self.arguments
.as_ref()
.map(|arguments| &arguments.args)
.into_iter()
.flatten()
pub fn bases(&self) -> &[Expr] {
match &self.arguments {
Some(arguments) => &arguments.args,
None => &[],
}
}

/// Return an iterator over the metaclass keywords of the class.
pub fn keywords(&self) -> impl Iterator<Item = &Keyword> {
self.arguments
.as_ref()
.map(|arguments| &arguments.keywords)
.into_iter()
.flatten()
pub fn keywords(&self) -> &[Keyword] {
match &self.arguments {
Some(arguments) => &arguments.keywords,
None => &[],
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_semantic/src/analyze/function_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn classify(
FunctionType::StaticMethod
} else if matches!(name, "__new__" | "__init_subclass__" | "__class_getitem__")
// Special-case class method, like `__new__`.
|| class_def.bases().any(|expr| {
|| class_def.bases().iter().any(|expr| {
// The class itself extends a known metaclass, so all methods are class methods.
semantic
.resolve_call_path(map_callable(expr))
Expand Down

0 comments on commit 2fa5087

Please sign in to comment.