-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32f0a32
commit d61ca4b
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from codemodder.codemods.test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
from core_codemods.fix_dataclass_defaults import FixDataclassDefaults | ||
|
||
|
||
class TestFixDataclassDefaults(BaseIntegrationTest): | ||
codemod = FixDataclassDefaults | ||
code_path = "tests/samples/fix_dataclass_defaults.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
(0, """from dataclasses import field, dataclass\n"""), | ||
(5, """ phones: list = field(default_factory=list)\n"""), | ||
(6, """ friends: dict = field(default_factory=dict)\n"""), | ||
(7, """ family: set = field(default_factory=set)\n"""), | ||
], | ||
) | ||
|
||
# fmt: off | ||
expected_diff =( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -1,8 +1,8 @@\n""" | ||
"""-from dataclasses import dataclass\n""" | ||
"""+from dataclasses import field, dataclass\n""" | ||
""" \n""" | ||
""" @dataclass\n""" | ||
""" class Test:\n""" | ||
""" name: str = ""\n""" | ||
"""- phones: list = []\n""" | ||
"""- friends: dict = {}\n""" | ||
"""- family: set = set()\n""" | ||
"""+ phones: list = field(default_factory=list)\n""" | ||
"""+ friends: dict = field(default_factory=dict)\n""" | ||
"""+ family: set = field(default_factory=set)\n""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "6" | ||
change_description = FixDataclassDefaults.change_description | ||
num_changes = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/core_codemods/docs/pixee_python_fix-dataclass-defaults.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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. | ||
|
||
Our changes look something like this: | ||
|
||
```diff | ||
-from dataclasses import dataclass | ||
+from dataclasses import field, dataclass | ||
|
||
@dataclass | ||
class Person: | ||
name: str = "" | ||
- phones: list = [] | ||
- friends: dict = {} | ||
- family: set = set() | ||
+ phones: list = field(default_factory=list) | ||
+ friends: dict = field(default_factory=dict) | ||
+ family: set = field(default_factory=set) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from dataclasses import dataclass | ||
|
||
@dataclass | ||
class Test: | ||
name: str = "" | ||
phones: list = [] | ||
friends: dict = {} | ||
family: set = set() |