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

✨ Rename keywords on ConfigDict #27

Merged
merged 1 commit into from
Jun 22, 2023
Merged
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
4 changes: 2 additions & 2 deletions bump_pydantic/codemods/replace_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"schema_extra": "json_schema_extra",
"validate_all": "validate_default",
}
# TODO: The codemod should not replace `Config` in case of removed keys, right?

BASE_MODEL_WITH_CONFIG = m.ClassDef(
bases=[
Expand Down Expand Up @@ -154,9 +153,10 @@ def visit_Assign(self, node: cst.Assign) -> None:

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]
self.config_args.append(
cst.Arg(
keyword=node.target, # type: ignore[arg-type]
keyword=node.target.with_changes(value=keyword), # type: ignore[arg-type]
value=self.assign_value,
equal=cst.AssignEqual(
whitespace_before=cst.SimpleWhitespace(""),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def expected() -> Folder:
"",
"",
"class A(BaseModel):",
" model_config = ConfigDict(orm_mode=True, validate_all=True)",
" model_config = ConfigDict(from_attributes=True, validate_default=True)",
"",
"",
"class BaseConfig:",
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_replace_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,19 @@ class Potato(BaseModel):
model_config = ConfigDict(allow_mutation=True, smart_union=True)
"""
self.assertCodemod(before, after)

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

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

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