Skip to content

Commit

Permalink
✅ Add integration tests for Field and const=True (pydantic#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Jun 29, 2023
1 parent aeb5dcc commit 74f9be5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 31 deletions.
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

0 comments on commit 74f9be5

Please sign in to comment.