File tree 1 file changed +18
-0
lines changed
1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,24 @@ assignment form instead.
76
76
# Invalid: [-1, 2]
77
77
```
78
78
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
+
79
97
## Default values
80
98
81
99
Default values for fields can be provided using the normal assignment syntax or by providing a value
You can’t perform that action at this time.
0 commit comments