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 integration tests for Field and const=True #42

Merged
merged 1 commit into from
Jun 29, 2023
Merged
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
30 changes: 1 addition & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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)`.
Expand Down
75 changes: 73 additions & 2 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[
Expand Down Expand Up @@ -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=[
Expand Down