Skip to content

Commit

Permalink
[ruff] Fix invalid annotation in docs example
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Feb 7, 2025
1 parent 84ceddc commit 10f926a
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions crates/ruff_linter/src/rules/ruff/rules/mutable_class_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,69 @@ use crate::rules::ruff::rules::helpers::{
/// changed in one instance, as those changes will unexpectedly affect all
/// other instances.
///
/// 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`.
/// Generally speaking, you probably want to avoid having mutable default
/// values in the class body at all; instead, these variables should usually
/// be initialized in `__init__`. However, other possible fixes for the issue
/// can include:
/// - Explicitly annotating the variable with [`typing.ClassVar`][ClassVar] to
/// indicate that it is intended to be shared across all instances.
/// - Using an immutable data type (e.g. a tuple instead of a list)
/// for the default value.
///
/// For mutable variables, prefer to initialize them in `__init__`.
/// ## Example
///
/// ## Examples
/// ```python
/// class A:
/// variable_1: list[int] = []
/// variable_2: set[int] = set()
/// variable_3: dict[str, int] = {}
/// ```
///
/// Using `ClassVar` and imutable types:
/// Use instead:
///
/// ```python
/// class A:
/// mutable_default: list[int] = []
/// immutable_default: list[int] = []
/// def __init__(self) -> None:
/// self.variable_1: list[int] = []
/// self.variable_2: set[int] = set()
/// self.variable_3: dict[str, int] = {}
/// ```
///
/// Use instead:
/// Or:
///
/// ```python
/// from typing import ClassVar
///
///
/// class A:
/// mutable_default: ClassVar[list[int]] = []
/// immutable_default: tuple[int, ...] = ()
/// variable_1: ClassVar[list[int]] = []
/// variable_2: ClassVar[set[int]] = set()
/// variable_3: ClassVar[dict[str, int]] = {}
/// ```
///
/// Using instance variables instead of class variables:
/// Or:
///
/// ```python
/// class A:
/// instance_dict: dict[str, str] = {"key": "value"}
/// variable_1: list[int] | None = None
/// variable_2: set[int] | None = None
/// variable_3: dict[str, int] | None = None
/// ```
///
/// Use instead:
/// Or:
///
/// ```python
/// class A:
/// instance_dict: ClassVar[dict[str, str]]
/// from collections.abc import Sequence, Mapping, Set as AbstractSet
/// from types import MappingProxyType
///
/// def __init__(self) -> None:
/// self.instance_dict: dict[str, str] = {"key": "value"}
///
/// class A:
/// variable_1: Sequence[int] = ()
/// variable_2: AbstractSet[int] = frozenset()
/// variable_3: Mapping[str, int] = MappingProxyType({})
/// ```
///
/// In cases where memory efficiency is a priority, `MappingProxyType`
/// can be used to create immutable dictionaries that are shared between
/// instances.
/// [ClassVar]: https://docs.python.org/3/library/typing.html#typing.ClassVar
#[derive(ViolationMetadata)]
pub(crate) struct MutableClassDefault;

Expand Down

0 comments on commit 10f926a

Please sign in to comment.