Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/ruff/RUF009.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ class D:

@dataclass
class E:
c: C = C()
c: C = C()

# https://github.com/astral-sh/ruff/issues/20266
@dataclass(frozen=True)
class C:
@dataclass(frozen=True)
class D:
foo: int = 1

d: D = D() # OK
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(crate) fn deferred_scopes(checker: &Checker) {
);
}
if checker.is_rule_enabled(Rule::FunctionCallInDataclassDefaultArgument) {
ruff::rules::function_call_in_dataclass_default(checker, class_def);
ruff::rules::function_call_in_dataclass_default(checker, class_def, scope_id);
}
if checker.is_rule_enabled(Rule::MutableClassDefault) {
ruff::rules::mutable_class_default(checker, class_def);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use ruff_python_ast::{self as ast, Expr, Stmt};

use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::name::{QualifiedName, UnqualifiedName};
use ruff_python_semantic::SemanticModel;
use ruff_python_semantic::analyze::typing::{
is_immutable_annotation, is_immutable_func, is_immutable_newtype_call,
};
use ruff_python_semantic::{ScopeId, SemanticModel};
use ruff_text_size::Ranged;

use crate::Violation;
Expand Down Expand Up @@ -77,7 +77,11 @@ impl Violation for FunctionCallInDataclassDefaultArgument {
}

/// RUF009
pub(crate) fn function_call_in_dataclass_default(checker: &Checker, class_def: &ast::StmtClassDef) {
pub(crate) fn function_call_in_dataclass_default(
checker: &Checker,
class_def: &ast::StmtClassDef,
scope_id: ScopeId,
) {
let semantic = checker.semantic();

let Some((dataclass_kind, _)) = dataclass_kind(class_def, semantic) else {
Expand Down Expand Up @@ -144,7 +148,7 @@ pub(crate) fn function_call_in_dataclass_default(checker: &Checker, class_def: &
|| func.as_name_expr().is_some_and(|name| {
is_immutable_newtype_call(name, checker.semantic(), &extend_immutable_calls)
})
|| is_frozen_dataclass_instantiation(func, semantic)
|| is_frozen_dataclass_instantiation(func, semantic, scope_id)
{
continue;
}
Expand All @@ -165,16 +169,22 @@ fn any_annotated(class_body: &[Stmt]) -> bool {

/// Checks that the passed function is an instantiation of the class,
/// retrieves the ``StmtClassDef`` and verifies that it is a frozen dataclass
fn is_frozen_dataclass_instantiation(func: &Expr, semantic: &SemanticModel) -> bool {
semantic.lookup_attribute(func).is_some_and(|id| {
let binding = &semantic.binding(id);
let Some(Stmt::ClassDef(class_def)) = binding.statement(semantic) else {
return false;
};

let Some((_, dataclass_decorator)) = dataclass_kind(class_def, semantic) else {
return false;
};
is_frozen_dataclass(dataclass_decorator, semantic)
})
fn is_frozen_dataclass_instantiation(
func: &Expr,
semantic: &SemanticModel,
scope_id: ScopeId,
) -> bool {
semantic
.lookup_attribute_in_scope(func, scope_id)
.is_some_and(|id| {
let binding = &semantic.binding(id);
let Some(Stmt::ClassDef(class_def)) = binding.statement(semantic) else {
return false;
};

let Some((_, dataclass_decorator)) = dataclass_kind(class_def, semantic) else {
return false;
};
is_frozen_dataclass(dataclass_decorator, semantic)
})
}
8 changes: 7 additions & 1 deletion crates/ruff_python_semantic/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -859,11 +859,17 @@ impl<'a> SemanticModel<'a> {
/// associated with `Class`, then the `BindingKind::FunctionDefinition` associated with
/// `Class.method`.
pub fn lookup_attribute(&self, value: &Expr) -> Option<BindingId> {
self.lookup_attribute_in_scope(value, self.scope_id)
}

/// Lookup a qualified attribute in the certain scope.
pub fn lookup_attribute_in_scope(&self, value: &Expr, scope_id: ScopeId) -> Option<BindingId> {
let unqualified_name = UnqualifiedName::from_expr(value)?;

// Find the symbol in the current scope.
let (symbol, attribute) = unqualified_name.segments().split_first()?;
let mut binding_id = self.lookup_symbol(symbol)?;
let mut binding_id =
self.lookup_symbol_in_scope(symbol, scope_id, self.in_forward_reference())?;

// Recursively resolve class attributes, e.g., `foo.bar.baz` in.
let mut tail = attribute;
Expand Down
Loading