-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Replace
constr
by StringConstraints
- Loading branch information
Showing
4 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import libcst as cst | ||
from libcst import matchers as m | ||
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand | ||
from libcst.codemod.visitors import AddImportsVisitor, RemoveImportsVisitor | ||
|
||
CONSTR_CALL = m.Call(func=m.Name("constr") | m.Attribute(value=m.Name("pydantic"), attr=m.Name("constr"))) | ||
ANN_ASSIGN_CONSTR_CALL = m.AnnAssign(annotation=m.Annotation(annotation=CONSTR_CALL)) | ||
|
||
|
||
class ConFuncCallCommand(VisitorBasedCodemodCommand): | ||
def __init__(self, context: CodemodContext) -> None: | ||
super().__init__(context) | ||
|
||
@m.leave(ANN_ASSIGN_CONSTR_CALL) | ||
def leave_ann_assign_constr_call(self, original_node: cst.AnnAssign, updated_node: cst.AnnAssign) -> cst.AnnAssign: | ||
AddImportsVisitor.add_needed_import(context=self.context, module="typing_extensions", obj="Annotated") | ||
annotated = cst.Subscript( | ||
value=cst.Name("Annotated"), | ||
slice=[ | ||
cst.SubscriptElement(slice=cst.Index(value=cst.Name("str"))), | ||
cst.SubscriptElement(slice=cst.Index(value=updated_node.annotation.annotation)), | ||
], | ||
) | ||
annotation = cst.Annotation(annotation=annotated) | ||
return updated_node.with_changes(annotation=annotation) | ||
|
||
@m.leave(CONSTR_CALL) | ||
def leave_constr_call(self, original_node: cst.Call, updated_node: cst.Call) -> cst.Call: | ||
RemoveImportsVisitor.remove_unused_import(context=self.context, module="pydantic", obj="constr") | ||
AddImportsVisitor.add_needed_import(context=self.context, module="pydantic", obj="StringConstraints") | ||
return updated_node.with_changes(func=cst.Name("StringConstraints")) | ||
|
||
|
||
if __name__ == "__main__": | ||
import textwrap | ||
|
||
from rich.console import Console | ||
|
||
console = Console() | ||
|
||
source = textwrap.dedent( | ||
""" | ||
from pydantic import BaseModel, constr | ||
class A(BaseModel): | ||
a: constr(max_length=10) | ||
b: Annotated[str, StringConstraints(max_length=10)] | ||
""" | ||
) | ||
console.print(source) | ||
console.print("=" * 80) | ||
|
||
mod = cst.parse_module(source) | ||
context = CodemodContext(filename="main.py") | ||
wrapper = cst.MetadataWrapper(mod) | ||
command = ConFuncCallCommand(context=context) | ||
console.print(mod) | ||
|
||
mod = wrapper.visit(command) | ||
print(mod.code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from libcst.codemod import CodemodTest | ||
|
||
from bump_pydantic.codemods.con_func import ConFuncCallCommand | ||
|
||
|
||
class TestFieldCommand(CodemodTest): | ||
TRANSFORM = ConFuncCallCommand | ||
|
||
maxDiff = None | ||
|
||
def test_constr_to_annotated(self) -> None: | ||
before = """ | ||
from pydantic import BaseModel, constr | ||
class Potato(BaseModel): | ||
potato: constr(min_length=1, max_length=10) | ||
""" | ||
after = """ | ||
from pydantic import StringConstraints, BaseModel | ||
from typing_extensions import Annotated | ||
class Potato(BaseModel): | ||
potato: Annotated[str, StringConstraints(min_length=1, max_length=10)] | ||
""" | ||
self.assertCodemod(before, after) | ||
|
||
def test_pydantic_constr_to_annotated(self) -> None: | ||
before = """ | ||
import pydantic | ||
from pydantic import BaseModel | ||
class Potato(BaseModel): | ||
potato: pydantic.constr(min_length=1, max_length=10) | ||
""" | ||
after = """ | ||
import pydantic | ||
from pydantic import StringConstraints, BaseModel | ||
from typing_extensions import Annotated | ||
class Potato(BaseModel): | ||
potato: Annotated[str, StringConstraints(min_length=1, max_length=10)] | ||
""" | ||
self.assertCodemod(before, after) |