Skip to content

Convert allow_mutation to frozen in model config #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions bump_pydantic/codemods/replace_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
CHECK_LINK_COMMENT = "# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information."

REMOVED_KEYS = [
"allow_mutation",
"error_msg_templates",
"fields",
"getter_dict",
Expand All @@ -38,6 +37,7 @@
"orm_mode": "from_attributes",
"schema_extra": "json_schema_extra",
"validate_all": "validate_default",
"allow_mutation": "frozen",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are going to check the same "allow_mutation" value again, there's no need to include it in this dictionary.

}

EXTRA_ATTRIBUTE = m.Attribute(
Expand Down Expand Up @@ -164,16 +164,23 @@ def visit_Assign(self, node: cst.Assign) -> None:
self.assign_value = node.value

def visit_AssignTarget(self, node: cst.AssignTarget) -> None:
if self.inside_config_class:
keyword = RENAMED_KEYS.get(node.target.value, node.target.value) # type: ignore[attr-defined]
if self.inside_config_class and isinstance(node.target, cst.Name):
keyword = RENAMED_KEYS.get(node.target.value, node.target.value)
value: cst.BaseExpression
if m.matches(self.assign_value, EXTRA_ATTRIBUTE):
value = cst.SimpleString(value=f'"{self.assign_value.attr.value}"') # type: ignore[attr-defined]
RemoveImportsVisitor.remove_unused_import(self.context, "pydantic", "Extra")
else:
value = self.assign_value # type: ignore[assignment]
value = self.assign_value
if node.target.value == "allow_mutation":
# The `allow_mutation` keyword is the negative of `frozen`.
if m.matches(value, m.Name(value="False")):
value = cst.Name("True")
elif m.matches(value, m.Name(value="True")):
value = cst.Name("False")
Comment on lines +175 to +180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have this as an elif in the above conditional.

self.config_args.append(
cst.Arg(
keyword=node.target.with_changes(value=keyword), # type: ignore[arg-type]
keyword=node.target.with_changes(value=keyword),
value=value,
equal=cst.AssignEqual(
whitespace_before=cst.SimpleWhitespace(""),
Expand Down
28 changes: 22 additions & 6 deletions tests/unit/test_replace_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,37 @@ class Potato(BaseModel):
"""
self.assertCodemod(before, after)

def test_allow_mutation(self) -> None:
before = """
from pydantic import BaseModel

class Potato(BaseModel):
class Config:
allow_mutation = False
"""
after = """
from pydantic import ConfigDict, BaseModel

class Potato(BaseModel):
model_config = ConfigDict(frozen=True)
"""
self.assertCodemod(before, after)

def test_removed_keys(self) -> None:
before = """
from pydantic import BaseModel

class Potato(BaseModel):
class Config:
allow_mutation = True
underscore_attrs_are_private = True
"""
after = """
from pydantic import ConfigDict, BaseModel

class Potato(BaseModel):
# TODO[pydantic]: The following keys were removed: `allow_mutation`.
# TODO[pydantic]: The following keys were removed: `underscore_attrs_are_private`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(allow_mutation=True)
model_config = ConfigDict(underscore_attrs_are_private=True)
"""
self.assertCodemod(before, after)

Expand All @@ -216,16 +232,16 @@ def test_multiple_removed_keys(self) -> None:

class Potato(BaseModel):
class Config:
allow_mutation = True
underscore_attrs_are_private = True
smart_union = True
"""
after = """
from pydantic import ConfigDict, BaseModel

class Potato(BaseModel):
# TODO[pydantic]: The following keys were removed: `allow_mutation`, `smart_union`.
# TODO[pydantic]: The following keys were removed: `underscore_attrs_are_private`, `smart_union`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(allow_mutation=True, smart_union=True)
model_config = ConfigDict(underscore_attrs_are_private=True, smart_union=True)
"""
self.assertCodemod(before, after)

Expand Down