Skip to content

Commit 828fc48

Browse files
committed
Add documentation note about common pitfall with the annotated pattern
Backport of: #11811
1 parent 42bf1fd commit 828fc48

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

docs/concepts/fields.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ assignment form instead.
7676
# Invalid: [-1, 2]
7777
```
7878

79+
Be careful not mixing *field* and *type* metadata:
80+
81+
```python {test="skip" lint="skip"}
82+
class Model(BaseModel):
83+
field_bad: Annotated[int, Field(deprecated=True)] | None = None # (1)!
84+
field_ok: Annotated[int | None, Field(deprecated=True)] = None # (2)!
85+
```
86+
87+
1. The [`Field()`][pydantic.fields.Field] function is applied to `int` type, hence the
88+
`deprecated` flag won't have any effect. While this may be confusing given that the name of
89+
the [`Field()`][pydantic.fields.Field] function would imply it should apply to the field,
90+
the API was designed when this function was the only way to provide metadata. You can
91+
alternatively make use of the [`annotated_types`](https://github.com/annotated-types/annotated-types)
92+
library which is now supported by Pydantic.
93+
94+
2. The [`Field()`][pydantic.fields.Field] function is applied to the "top-level" union type,
95+
hence the `deprecated` flag will be applied to the field.
96+
7997
## Default values
8098

8199
Default values for fields can be provided using the normal assignment syntax or by providing a value

0 commit comments

Comments
 (0)