mirror of
https://github.com/astral-sh/ruff
synced 2026-01-09 07:34:06 -05:00
Add rule to detect unnecessary class properties (#21535)
Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com> Co-authored-by: Amethyst Reese <amethyst@n7.gg> Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
@@ -3,12 +3,13 @@ use std::path::Path;
|
||||
use bitflags::bitflags;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use ruff_python_ast::helpers::from_relative_import;
|
||||
use ruff_python_ast::helpers::{from_relative_import, map_subscript};
|
||||
use ruff_python_ast::name::{QualifiedName, UnqualifiedName};
|
||||
use ruff_python_ast::{self as ast, Expr, ExprContext, PySourceType, Stmt};
|
||||
use ruff_text_size::{Ranged, TextRange, TextSize};
|
||||
|
||||
use crate::Imported;
|
||||
use crate::analyze::visibility;
|
||||
use crate::binding::{
|
||||
Binding, BindingFlags, BindingId, BindingKind, Bindings, Exceptions, FromImport, Import,
|
||||
SubmoduleImport,
|
||||
@@ -2153,6 +2154,21 @@ impl<'a> SemanticModel<'a> {
|
||||
function.range() == function_def.range()
|
||||
})
|
||||
}
|
||||
|
||||
/// Return `true` if the model is in a `typing.Protocol` subclass or an abstract
|
||||
/// method.
|
||||
pub fn in_protocol_or_abstract_method(&self) -> bool {
|
||||
self.current_scopes().any(|scope| match scope.kind {
|
||||
ScopeKind::Class(class_def) => class_def
|
||||
.bases()
|
||||
.iter()
|
||||
.any(|base| self.match_typing_expr(map_subscript(base), "Protocol")),
|
||||
ScopeKind::Function(function_def) => {
|
||||
visibility::is_abstract(&function_def.decorator_list, self)
|
||||
}
|
||||
_ => false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ShadowedBinding {
|
||||
|
||||
Reference in New Issue
Block a user