@@ -24,14 +24,20 @@ use crate::rules::ruff::rules::helpers::{
24
24
/// `typing.ClassVar`. When mutability is not required, values should be
25
25
/// immutable types, like `tuple` or `frozenset`.
26
26
///
27
+ /// For mutable variables, prefer to initialize them in `__init__`.
28
+ ///
27
29
/// ## Examples
30
+ ///
31
+ /// Using `ClassVar` and imutable types:
32
+ ///
28
33
/// ```python
29
34
/// class A:
30
35
/// mutable_default: list[int] = []
31
36
/// immutable_default: list[int] = []
32
37
/// ```
33
38
///
34
39
/// Use instead:
40
+ ///
35
41
/// ```python
36
42
/// from typing import ClassVar
37
43
///
@@ -40,6 +46,27 @@ use crate::rules::ruff::rules::helpers::{
40
46
/// mutable_default: ClassVar[list[int]] = []
41
47
/// immutable_default: tuple[int, ...] = ()
42
48
/// ```
49
+ ///
50
+ /// Using instance variables instead of class variables:
51
+ ///
52
+ /// ```python
53
+ /// class A:
54
+ /// instance_dict: dict[str, str] = {"key": "value"}
55
+ /// ```
56
+ ///
57
+ /// Use instead:
58
+ ///
59
+ /// ```python
60
+ /// class A:
61
+ /// instance_dict: ClassVar[dict[str, str]]
62
+ ///
63
+ /// def __init__(self) -> None:
64
+ /// self.instance_dict: dict[str, str] = {"key": "value"}
65
+ /// ```
66
+ ///
67
+ /// In cases where memory efficiency is a priority, `MappingProxyType`
68
+ /// can be used to create immutable dictionaries that are shared between
69
+ /// instances.
43
70
#[ derive( ViolationMetadata ) ]
44
71
pub ( crate ) struct MutableClassDefault ;
45
72
0 commit comments