Skip to content

Commit

Permalink
Fix for field
Browse files Browse the repository at this point in the history
  • Loading branch information
denisart committed Feb 18, 2024
1 parent 368b296 commit a317c97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
31 changes: 16 additions & 15 deletions bump_pydantic/codemods/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,22 @@ 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())):
if arg.keyword.value == "json_schema_extra":
json_schema_extra_elements.extend(arg.value.elements) # type: ignore
continue

if (
(arg.keyword.value not in RENAMED_KEYWORDS)
and (arg.keyword.value not in Field.__annotations__)
and (arg.keyword != "extra")
):
new_dict_element = cst.DictElement(
key=cst.SimpleString(value=f'"{arg.keyword.value}"'), value=arg.value,

)
json_schema_extra_elements.append(new_dict_element)
continue
if arg.keyword is not None:
if arg.keyword.value == "json_schema_extra":
json_schema_extra_elements.extend(arg.value.elements) # type: ignore
continue

if (
(arg.keyword.value not in RENAMED_KEYWORDS)
and (arg.keyword.value not in Field.__annotations__)
and (arg.keyword != "extra")
):
new_dict_element = cst.DictElement(
key=cst.SimpleString(value=f'"{arg.keyword.value}"'),
value=arg.value,
)
json_schema_extra_elements.append(new_dict_element)
continue

keyword = RENAMED_KEYWORDS.get(arg.keyword.value, arg.keyword.value) # type: ignore
value = arg.value
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ class Settings(BaseSettings):
def test_json_schema_extra_exist(self) -> None:
before = """
from pydantic import BaseModel, Field
class Human(BaseModel):
name: str = Field(..., some_extra_field="some_extra_field_value", json_schema_extra={"a": "b"})
"""
after = """
from pydantic import BaseModel, Field
class Human(BaseModel):
name: str = Field(..., json_schema_extra={"some_extra_field": "some_extra_field_value", "a": "b"})
"""
Expand Down

0 comments on commit a317c97

Please sign in to comment.