Skip to content

Commit

Permalink
Add section about Constrained classes to the Migration Guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Jul 28, 2023
1 parent eab48b1 commit d98b8ff
Showing 1 changed file with 34 additions and 9 deletions.
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


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

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

0 comments on commit d98b8ff

Please sign in to comment.