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 section about Constrained classes to the Migration Guide #6924

Merged
merged 6 commits into from Jul 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 34 additions & 9 deletions docs/migration.md
Expand Up @@ -749,6 +749,36 @@ classes using `Annotated`.
Inheriting from `str` had upsides and downsides, and for V2 we decided it would be better to remove this. To use these
types in APIs which expect `str` you'll now need to convert them (with `str(url)`).

### Constrained types

The `Constrained*` classes were _removed_, and you should replace them by `Annotated[<type>, Field(...)]`, for example:

```py
from pydantic import BaseModel, ConstrainedInt


class MyInt(ConstrainedInt):
ge = 0


class Model(BaseModel):
x: MyInt
```

...becomes:

```py
from pydantic import BaseModel, Field
Kludex marked this conversation as resolved.
Show resolved Hide resolved


class Model(BaseModel):
x: Annotated[int, Field(ge=0)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d make it a type alias so that it’s closer to the original example

Kludex marked this conversation as resolved.
Show resolved Hide resolved
```

Read more about it on the [Composing types via `Annotated`](../usage/types/custom/#composing-types-via-annotated) section.

For `ConstrainedStr` you can use [`StringConstraints`][pydantic.types.StringConstraints] instead.

## Moved in Pydantic V2

| Pydantic V1 | Pydantic V2 |
Expand Down Expand Up @@ -802,11 +832,15 @@ types in APIs which expect `str` you'll now need to convert them (with `str(url)
- `pydantic.ConstrainedStr`
- `pydantic.JsonWrapper`
- `pydantic.NoneBytes`
- This was an alias to `None | bytes`.
- `pydantic.NoneStr`
- This was an alias to `None | str`.
- `pydantic.NoneStrBytes`
- This was an alias to `None | str | bytes`.
- `pydantic.Protocol`
- `pydantic.Required`
- `pydantic.StrBytes`
- This was an alias to `str | bytes`.
- `pydantic.compiled`
- `pydantic.config.get_config`
- `pydantic.config.inherit_config`
Expand Down Expand Up @@ -921,15 +955,6 @@ types in APIs which expect `str` you'll now need to convert them (with `str(url)
- `pydantic.stricturl`
- `pydantic.tools.parse_file_as`
- `pydantic.tools.parse_raw_as`
- `pydantic.types.ConstrainedBytes`
- `pydantic.types.ConstrainedDate`
- `pydantic.types.ConstrainedDecimal`
- `pydantic.types.ConstrainedFloat`
- `pydantic.types.ConstrainedFrozenSet`
- `pydantic.types.ConstrainedInt`
- `pydantic.types.ConstrainedList`
- `pydantic.types.ConstrainedSet`
- `pydantic.types.ConstrainedStr`
- `pydantic.types.JsonWrapper`
- `pydantic.types.NoneBytes`
- `pydantic.types.NoneStr`
Expand Down