mirror of
https://github.com/astral-sh/ruff.git
synced 2025-12-23 09:19:39 +00: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:
parent
5364256190
commit
33713a7e2a
10 changed files with 246 additions and 22 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue