Skip to content

Commit 5306219

Browse files
committed
Don't add default None when default is ...
1 parent b08f445 commit 5306219

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

bump_pydantic/codemods/add_default_none.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,22 @@ def leave_AnnAssign(self, original_node: cst.AnnAssign, updated_node: cst.AnnAss
8181
if self.inside_base_model and self.should_add_none:
8282
if updated_node.value is None:
8383
updated_node = updated_node.with_changes(value=cst.Name("None"))
84-
# TODO: Should accept `pydantic.Field` as well.
8584
elif m.matches(updated_node.value, m.Call(func=m.Name("Field"))):
8685
assert isinstance(updated_node.value, cst.Call)
87-
if updated_node.value.args:
88-
arg = updated_node.value.args[0]
89-
if (arg.keyword is None or arg.keyword.value == "default") and m.matches(arg.value, m.Ellipsis()):
86+
args = updated_node.value.args
87+
if args:
88+
# NOTE: It has a "default" value as positional argument. Nothing to do.
89+
if args[0].keyword is None:
90+
...
91+
# NOTE: It has a "default" or "default_factory" keyword argument. Nothing to do.
92+
elif any(arg.keyword and arg.keyword.value in ("default", "default_factory") for arg in args):
93+
...
94+
else:
9095
updated_node = updated_node.with_changes(
91-
value=updated_node.value.with_changes(
92-
args=[arg.with_changes(value=cst.Name("None")), *updated_node.value.args[1:]]
93-
)
96+
value=updated_node.value.with_changes(args=[cst.Arg(value=cst.Name("None")), *args])
9497
)
95-
# This is the case where `Field` is called without any arguments e.g. `Field()`.
98+
99+
# NOTE: This is the case where `Field` is called without any arguments e.g. `Field()`.
96100
else:
97101
updated_node = updated_node.with_changes(
98102
value=updated_node.value.with_changes(args=[cst.Arg(value=cst.Name("None"))]) # type: ignore

tests/integration/cases/add_none.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
" g: Optional[int] = Field()",
2323
" h: Optional[int] = Field(...)",
2424
" i: Optional[int] = Field(default_factory=lambda: None)",
25+
" j: Optional[int] = ...",
26+
" k: Optional[int] = None",
27+
" l: Optional[int] = Field(lt=10, default=None)",
28+
" m: Optional[int] = Field(lt=10)",
29+
" n: Optional[int] = Field(default=...)",
30+
" o: Optional[int] = Field(default=None)",
2531
],
2632
),
2733
expected=File(
@@ -38,10 +44,16 @@
3844
" c: Union[int, None] = None",
3945
" d: Any = None",
4046
" e: Dict[str, str]",
41-
" f: Optional[int] = Field(None, lt=10)",
47+
" f: Optional[int] = Field(..., lt=10)",
4248
" g: Optional[int] = Field(None)",
43-
" h: Optional[int] = Field(None)",
49+
" h: Optional[int] = Field(...)",
4450
" i: Optional[int] = Field(default_factory=lambda: None)",
51+
" j: Optional[int] = ...",
52+
" k: Optional[int] = None",
53+
" l: Optional[int] = Field(lt=10, default=None)",
54+
" m: Optional[int] = Field(None, lt=10)",
55+
" n: Optional[int] = Field(default=...)",
56+
" o: Optional[int] = Field(default=None)",
4557
],
4658
),
4759
)

0 commit comments

Comments
 (0)