Skip to content

Commit a8e50a7

Browse files
[RUF008] Make it clearer that a mutable default in a dataclass is only valid if it is typed as a ClassVar (#10395)
## Summary The previous documentation sounded as if typing a mutable default as a `ClassVar` were optional. However, it is not, as not doing so causes a `ValueError`. The snippet below was tested in Python's interactive shell: ``` >>> from dataclasses import dataclass >>> @DataClass ... class A: ... mutable_default: list[int] = [] ... Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.11/dataclasses.py", line 1230, in dataclass return wrap(cls) ^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 1220, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 958, in _process_class cls_fields.append(_get_field(cls, name, type, kw_only)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/dataclasses.py", line 815, in _get_field raise ValueError(f'mutable default {type(f.default)} for field ' ValueError: mutable default <class 'list'> for field mutable_default is not allowed: use default_factory >>> ``` This behavior is also documented in Python's docs, see [here](https://docs.python.org/3/library/dataclasses.html#mutable-default-values): > [...] the [dataclass()](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass) decorator will raise a [ValueError](https://docs.python.org/3/library/exceptions.html#ValueError) if it detects an unhashable default parameter. The assumption is that if a value is unhashable, it is mutable. This is a partial solution, but it does protect against many common errors. And [here](https://docs.python.org/3/library/dataclasses.html#class-variables) it is documented why it works if it is typed as a `ClassVar`: > One of the few places where [dataclass()](https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass) actually inspects the type of a field is to determine if a field is a class variable as defined in [PEP 526](https://peps.python.org/pep-0526/). It does this by checking if the type of the field is typing.ClassVar. If a field is a ClassVar, it is excluded from consideration as a field and is ignored by the dataclass mechanisms. Such ClassVar pseudo-fields are not returned by the module-level [fields()](https://docs.python.org/3/library/dataclasses.html#dataclasses.fields) function. In this PR I have changed the documentation to make it a little bit clearer that not using `ClassVar` makes the code invalid.
1 parent e944c16 commit a8e50a7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

crates/ruff_linter/src/rules/ruff/rules/mutable_dataclass_default.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::rules::ruff::rules::helpers::{is_class_var_annotation, is_dataclass};
1919
/// Instead of sharing mutable defaults, use the `field(default_factory=...)`
2020
/// pattern.
2121
///
22-
/// If the default value is intended to be mutable, it should be annotated with
23-
/// `typing.ClassVar`.
22+
/// If the default value is intended to be mutable, it must be annotated with
23+
/// `typing.ClassVar`; otherwise, a `ValueError` will be raised.
2424
///
2525
/// ## Examples
2626
/// ```python
@@ -29,6 +29,8 @@ use crate::rules::ruff::rules::helpers::{is_class_var_annotation, is_dataclass};
2929
///
3030
/// @dataclass
3131
/// class A:
32+
/// # A list without a `default_factory` or `ClassVar` annotation
33+
/// # will raise a `ValueError`.
3234
/// mutable_default: list[int] = []
3335
/// ```
3436
///
@@ -44,7 +46,7 @@ use crate::rules::ruff::rules::helpers::{is_class_var_annotation, is_dataclass};
4446
///
4547
/// Or:
4648
/// ```python
47-
/// from dataclasses import dataclass, field
49+
/// from dataclasses import dataclass
4850
/// from typing import ClassVar
4951
///
5052
///

0 commit comments

Comments
 (0)