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

Add default none for Field correctly #126

Closed
wants to merge 2 commits 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
4 changes: 2 additions & 2 deletions bump_pydantic/codemods/add_default_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def leave_AnnAssign(self, original_node: cst.AnnAssign, updated_node: cst.AnnAss
assert isinstance(updated_node.value, cst.Call)
if updated_node.value.args:
arg = updated_node.value.args[0]
if (arg.keyword is None or arg.keyword.value == "default") and m.matches(arg.value, m.Ellipsis()):
if arg.keyword and arg.keyword.value != "default":
updated_node = updated_node.with_changes(
value=updated_node.value.with_changes(
args=[arg.with_changes(value=cst.Name("None")), *updated_node.value.args[1:]]
args=[cst.Arg(value=cst.Name("None")), *updated_node.value.args]
)
)
# This is the case where `Field` is called without any arguments e.g. `Field()`.
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/cases/add_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
" c: Union[int, None] = None",
" d: Any = None",
" e: Dict[str, str]",
" f: Optional[int] = Field(None, lt=10)",
" f: Optional[int] = Field(..., lt=10)",
" g: Optional[int] = Field(None)",
" h: Optional[int] = Field(None)",
" i: Optional[int] = Field(default_factory=lambda: None)",
" h: Optional[int] = Field(...)",
" i: Optional[int] = Field(None, default_factory=lambda: None)",
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong. You shouldn't have both default and default_factory being set.

],
),
)
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_add_default_none.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,35 @@ class Potato(BaseModel):

class Potato(BaseModel):
a: Union[Union[str, None], int] = None
"""
)
assert module.code == expected

def test_with_field(self) -> None:
module = self.add_default_none(
"some/test/module.py",
"""
from pydantic import BaseModel, Field
from typing import Optional

class Foo(BaseModel):
a: Optional[int] = Field()
c: Optional[int] = Field(...)
d: Optional[int] = Field(description="spam")
e: Optional[int] = Field(default=..., description="spam")
f: Optional[int] = Field(default=None, description="spam")
""",
)
expected = textwrap.dedent(
"""from pydantic import BaseModel, Field
from typing import Optional

class Foo(BaseModel):
a: Optional[int] = Field(None)
c: Optional[int] = Field(...)
d: Optional[int] = Field(None, description="spam")
e: Optional[int] = Field(default=..., description="spam")
f: Optional[int] = Field(default=None, description="spam")
"""
)
assert module.code == expected