Skip to content

Commit c8ad8ac

Browse files
authored
♻️ Do not convert constr to Annotated (#97)
1 parent 6e1cb4d commit c8ad8ac

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

bump_pydantic/codemods/con_func.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def leave_ann_assign_constr_call(self, original_node: cst.AnnAssign, updated_nod
5757
func_name = cast(str, annotation.func.attr.value) # type: ignore
5858
type_name = MAP_FUNC_TO_TYPE[func_name]
5959

60+
# TODO: When FastAPI supports Pydantic 2.0.4+, remove the conditional below.
61+
if func_name == "constr":
62+
return updated_node
63+
6064
needed_import = MAP_TYPE_TO_NEEDED_IMPORT.get(type_name)
6165
if needed_import is not None:
6266
AddImportsVisitor.add_needed_import(context=self.context, **needed_import) # type: ignore[arg-type]
@@ -82,12 +86,13 @@ def leave_ann_assign_constr_call(self, original_node: cst.AnnAssign, updated_nod
8286
annotation = cst.Annotation(annotation=annotated) # type: ignore[assignment]
8387
return updated_node.with_changes(annotation=annotation)
8488

89+
# TODO: When FastAPI supports Pydantic 2.0.4+, remove the comments below.
8590
@m.leave(CONSTR_CALL)
8691
def leave_constr_call(self, original_node: cst.Call, updated_node: cst.Call) -> cst.Call:
8792
self._remove_import(original_node.func)
88-
AddImportsVisitor.add_needed_import(context=self.context, module="pydantic", obj="StringConstraints")
93+
# AddImportsVisitor.add_needed_import(context=self.context, module="pydantic", obj="StringConstraints")
8994
return updated_node.with_changes(
90-
func=cst.Name("StringConstraints"),
95+
# func=cst.Name("StringConstraints"),
9196
args=[
9297
arg if arg.keyword and arg.keyword.value != "regex" else arg.with_changes(keyword=cst.Name("pattern"))
9398
for arg in updated_node.args

tests/integration/cases/con_func.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
expected=File(
2424
"con_func.py",
2525
content=[
26-
"from pydantic import Field, StringConstraints, BaseModel",
26+
"from pydantic import Field, BaseModel, constr",
2727
"from decimal import Decimal",
2828
"from typing import List, Set",
2929
"from typing_extensions import Annotated",
3030
"",
3131
"",
3232
"class Potato(BaseModel):",
33-
" a: Annotated[str, StringConstraints(pattern='[a-z]+')]",
33+
" a: constr(pattern='[a-z]+')",
3434
" b: Annotated[List[int], Field(min_length=1, max_length=10)]",
3535
" c: Annotated[int, Field(gt=0, lt=10)]",
3636
" d: Annotated[bytes, Field(min_length=1, max_length=10)]",

tests/unit/test_con_func.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from libcst.codemod import CodemodTest
23

34
from bump_pydantic.codemods.con_func import ConFuncCallCommand
@@ -8,6 +9,7 @@ class TestFieldCommand(CodemodTest):
89

910
maxDiff = None
1011

12+
@pytest.mark.xfail(reason="Annotated is not supported yet!")
1113
def test_constr_to_annotated(self) -> None:
1214
before = """
1315
from pydantic import BaseModel, constr
@@ -24,6 +26,7 @@ class Potato(BaseModel):
2426
"""
2527
self.assertCodemod(before, after)
2628

29+
@pytest.mark.xfail(reason="Annotated is not supported yet!")
2730
def test_pydantic_constr_to_annotated(self) -> None:
2831
before = """
2932
import pydantic

0 commit comments

Comments
 (0)