From 1c6a008aff1e833784d9174b2a16fcc8269a57eb Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 29 Jun 2023 16:35:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +------------- tests/integration/test_cli.py | 75 ++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index e0288b1..2221b40 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,6 @@ Bump Pydantic is a tool to help you migrate your code from Pydantic V1 to V2. - [BP005: Replace `GenericModel` by `BaseModel`](#bp005-replace-genericmodel-by-basemodel) - [BP006: Replace `__root__` by `RootModel`](#bp006-replace-__root__-by-rootmodel) - [BP007: Replace decorators](#bp007-replace-decorators) - - [BP008: Replace `const=True` by `Literal`](#bp008-replace-consttrue-by-literal) - [BP009: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter`](#bp009-replace-pydanticparse_obj_as-by-pydantictypeadapter) - [License](#license) @@ -129,6 +128,7 @@ class User(BaseModel): ### BP003: Replace `Field` old parameters to new ones - ✅ Replace `Field` old parameters to new ones. +- ✅ Replace `field: Enum = Field(Enum.VALUE, const=True)` by `field: Literal[Enum.VALUE] = Enum.VALUE`. The following code will be transformed: @@ -284,34 +284,6 @@ class User(BaseModel): return values ``` -### BP008: Replace `const=True` by `Literal` - -- ✅ Replace `field: Enum = Field(Enum.VALUE, const=True)` by `field: Literal[Enum.VALUE] = Enum.VALUE`. - -The following code will be transformed: - -```py -from enum import Enum - -from pydantic import BaseModel, Field - - -class User(BaseModel): - name: Enum = Field(Enum.VALUE, const=True) -``` - -Into: - -```py -from enum import Enum - -from pydantic import BaseModel, Field - - -class User(BaseModel): - name: Literal[Enum.VALUE] = Enum.VALUE -``` - ### BP009: Replace `pydantic.parse_obj_as` by `pydantic.TypeAdapter` - ✅ Replace `pydantic.parse_obj_as(T, obj)` to `pydantic.TypeAdapter(T).validate_python(obj)`. diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.py index 1cf0a25..40d9c62 100644 --- a/tests/integration/test_cli.py +++ b/tests/integration/test_cli.py @@ -171,7 +171,41 @@ def before() -> Folder: "class A(BaseModel):", " __root__ = int", ], - ) + ), + File( + "replace_validator.py", + content=[ + "from pydantic import BaseModel, validator, root_validator", + "", + "", + "class A(BaseModel):", + " a: int", + " b: str", + "", + " @validator('a')", + " def validate_a(cls, v):", + " return v + 1", + "", + " @root_validator()", + " def validate_b(cls, values):", + " return values", + ], + ), + File( + "const_to_literal.py", + content=[ + "from enum import Enum", + "from pydantic import BaseModel, Field", + "", + "", + "class A(str, Enum):", + " a = 'a'", + " b = 'b'", + "", + "class A(BaseModel):", + " a: A = Field(A.a, const=True)", + ], + ), # File( # "config_dict_and_settings.py", # content=[ @@ -284,7 +318,44 @@ def expected() -> Folder: "class A(RootModel[int]):", " pass", ], - ) + ), + File( + "replace_validator.py", + content=[ + "from pydantic import field_validator, model_validator, BaseModel", + "", + "", + "class A(BaseModel):", + " a: int", + " b: str", + "", + " @field_validator('a')", + " @classmethod", + " def validate_a(cls, v):", + " return v + 1", + "", + " @model_validator()", + " @classmethod", + " def validate_b(cls, values):", + " return values", + ], + ), + File( + "const_to_literal.py", + content=[ + "from enum import Enum", + "from pydantic import BaseModel", + "from typing import Literal", + "", + "", + "class A(str, Enum):", + " a = 'a'", + " b = 'b'", + "", + "class A(BaseModel):", + " a: Literal[A.a] = A.a", + ], + ), # File( # "config_dict_and_settings.py", # content=[