From 80221e4536ca48194e3f8a697a43a4d1bb78006e Mon Sep 17 00:00:00 2001 From: Samuel Searles-Bryant Date: Mon, 27 Nov 2023 13:07:42 +0000 Subject: [PATCH] Add advice for fixing RUF008 when mutability is not desired A common mistake in Python code is creating global mutable state by assigning class attributes that are mutable (e.g. a list or dictionary). `RUF012` helpfully catches this mistake and can be used to prevent inadvertent global state form being created. The description of that rule helpfully provides advice for adjusting code to stop beigng reported as an error *if the attribute should be mutable*, but does not say how the error can be avoided when mutability is not the goal. This change adds that advice, so that both possibilities are accounted for. --- .../src/rules/ruff/rules/mutable_class_default.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs b/crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs index da1e2837512a48..776209d6f68125 100644 --- a/crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs +++ b/crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs @@ -20,13 +20,15 @@ use crate::rules::ruff::rules::helpers::{ /// changed in one instance, as those changes will unexpectedly affect all /// other instances. /// -/// When mutable value are intended, they should be annotated with -/// `typing.ClassVar`. +/// When mutable values are intended, they should be annotated with +/// `typing.ClassVar`. When mutability is not required, values should be +/// immutable types, like `tuple` or `frozenset`. /// /// ## Examples /// ```python /// class A: /// mutable_default: list[int] = [] +/// immutable_default: list[int] = [] /// ``` /// /// Use instead: @@ -36,6 +38,7 @@ use crate::rules::ruff::rules::helpers::{ /// /// class A: /// mutable_default: ClassVar[list[int]] = [] +/// immutable_default: tuple[int, ...] = () /// ``` #[violation] pub struct MutableClassDefault;