Skip to content

Commit

Permalink
Update src/core_codemods/docs/pixee_python_fix-dataclass-defaults.md
Browse files Browse the repository at this point in the history
Co-authored-by: Dan D'Avella <[email protected]>
  • Loading branch information
clavedeluna and drdavella authored Mar 11, 2024
1 parent d61ca4b commit 8bf233a
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This codemod will fix instances of `dataclasses.dataclass` that define default lists, sets, or dicts which raise a runtime `ValueError`. The [dataclass documentation](https://docs.python.org/3/library/dataclasses.html#mutable-default-values) provides a clear explanation of why this code is disallowed and explains how to use `field(default_factory=...)` instead.
When defining a Python dataclass it is not safe to use mutable datatypes (such as `list`, `dict`, or `set`) as defaults for the attributes. This is because the defined attribute will be shared by all instances of the dataclass type. Using such a mutable default will ultimately result in a `ValueError` at runtime. This codemod updates attributes of `dataclasses.dataclass` with mutable defaults to use `dataclasses.field` instead. The [dataclass documentation](https://docs.python.org/3/library/dataclasses.html#mutable-default-values) providesmore details about why using `field(default_factory=...)` is the recommended pattern.

Our changes look something like this:

Expand Down

0 comments on commit 8bf233a

Please sign in to comment.