From f01260b4ff3cdd040580d1310fbf14aefedda3be Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 28 Jul 2023 14:43:50 +0100 Subject: [PATCH] Add section about Constrained classes to the Migration Guide (#6924) --- docs/migration.md | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/docs/migration.md b/docs/migration.md index 0420744b35..f2d4946f7d 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -749,6 +749,40 @@ 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[, Field(...)]`, for example: + +```py test="skip" +from pydantic import BaseModel, ConstrainedInt + + +class MyInt(ConstrainedInt): + ge = 0 + + +class Model(BaseModel): + x: MyInt +``` + +...becomes: + +```py +from typing_extensions import Annotated + +from pydantic import BaseModel, Field + +MyInt = Annotated[int, Field(ge=0)] + + +class Model(BaseModel): + x: MyInt +``` + +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 | @@ -802,11 +836,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` @@ -921,15 +959,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`