Skip to content
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

Fix for allow mutation and corresponding test case #61

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions bump_pydantic/codemods/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ def leave_field_call(self, original_node: cst.Call, updated_node: cst.Call) -> c
new_args: List[cst.Arg] = []
for arg in updated_node.args:
if m.matches(arg, m.Arg(keyword=m.Name())):
keyword = RENAMED_KEYWORDS.get(arg.keyword.value, arg.keyword.value) # type: ignore
new_args.append(arg.with_changes(keyword=arg.keyword.with_changes(value=keyword))) # type: ignore
args_dict = dict()
keyword = RENAMED_KEYWORDS.get(arg.keyword.value, arg.keyword.value)
args_dict["keyword"] = arg.keyword.with_changes(value=keyword) # type: ignore
# Check if keyword is `allow_mutation` and if so, invert the value.
if arg.keyword.value == "allow_mutation":
args_dict["value"] = arg.value.with_changes(value=str(not(arg.value.value == "True")))
new_args.append(arg.with_changes(**args_dict))
else:
new_args.append(arg)

Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ class Potato(BaseModel):
potato: Literal[MyEnum.POTATO] = MyEnum.POTATO
"""
self.assertCodemod(before, after)

def test_flip_frozen_boolean(self) -> None:
before = """
from pydantic import BaseSettings, Field

class Settings(BaseSettings):
potato: int = Field(..., allow_mutation=False)
strawberry: int = Field(..., allow_mutation=True)
"""
after = """
from pydantic import BaseSettings, Field

class Settings(BaseSettings):
potato: int = Field(..., frozen=True)
strawberry: int = Field(..., frozen=False)
"""
self.assertCodemod(before, after)