1
- The ` validate_assignment ` decorator allows the arguments passed to a function to be parsed and validated using
1
+ The ` validate_arguments ` decorator allows the arguments passed to a function to be parsed and validated using
2
2
the function's annotations before the function is called. While under the hood this uses the same approach of model
3
3
creation and initialisation; it provides an extremely easy way to apply validation to your code with minimal
4
4
boilerplate.
5
5
6
6
!!! info "In Beta"
7
- The ` validate_assignment ` decorator is in ** beta** , it has been added to * pydantic* in ** v1.5** on a
7
+ The ` validate_arguments ` decorator is in ** beta** , it has been added to * pydantic* in ** v1.5** on a
8
8
** provisional basis** . It may change significantly in future releases and its interface will not be concrete
9
9
until ** v2** . Feedback from the community while it's still provisional would be extremely useful; either comment
10
10
on [ #1205 ] ( https://github.com/samuelcolvin/pydantic/issues/1205 ) or create a new issue.
@@ -19,7 +19,7 @@ _(This script is complete, it should run "as is")_
19
19
## Argument Types
20
20
21
21
Argument types are inferred from type annotations on the function, arguments without a type decorator are considered
22
- as ` Any ` . Since ` validate_assignment ` internally uses a standard ` BaseModel ` , all types listed in
22
+ as ` Any ` . Since ` validate_arguments ` internally uses a standard ` BaseModel ` , all types listed in
23
23
[ types] ( types.md ) can be validated, including * pydantic* models and [ custom types] ( types.md#custom-data-types ) .
24
24
As with the rest of * pydantic* , types can be coerced by the decorator before they're passed to the actual function:
25
25
@@ -34,7 +34,7 @@ A few notes:
34
34
* ` max ` has no type annotation, so will be considered as ` Any ` by the decorator
35
35
36
36
Type coercion like this can be extremely helpful but also confusing or not desired,
37
- see [ below] ( #coercion-and-stictness ) for a discussion of ` validate_assignment ` 's limitations in this regard.
37
+ see [ below] ( #coercion-and-stictness ) for a discussion of ` validate_arguments ` 's limitations in this regard.
38
38
39
39
## Function Signatures
40
40
@@ -56,7 +56,7 @@ _(This script is complete, it should run "as is")_
56
56
57
57
## Usage with mypy
58
58
59
- The ` validate_assignment ` decorator should work "out of the box" with [ mypy] ( http://mypy-lang.org/ ) since it's
59
+ The ` validate_arguments ` decorator should work "out of the box" with [ mypy] ( http://mypy-lang.org/ ) since it's
60
60
defined to return a function with the same signature as the function it decorates. The only limitation is that
61
61
since we trick mypy into thinking the function returned by the decorator is the same as the function being
62
62
decorated; access to the [ raw function] ( #raw-function ) or other attributes will require ` type: ignore ` .
@@ -73,7 +73,7 @@ _(This script is complete, it should run "as is")_
73
73
74
74
## Async Functions
75
75
76
- ` validate_assignment ` can also be used on async functions:
76
+ ` validate_arguments ` can also be used on async functions:
77
77
78
78
``` py
79
79
{!.tmp_examples/ validation_decorator_async.py!}
@@ -83,7 +83,7 @@ _(This script is complete, it should run "as is")_
83
83
84
84
## Limitations
85
85
86
- ` validate_assignment ` has been released on a provisional basis without all the bells and whistles, which may
86
+ ` validate_arguments ` has been released on a provisional basis without all the bells and whistles, which may
87
87
be added later, see [ #1205 ] ( https://github.com/samuelcolvin/pydantic/issues/1205 ) for some more discussion of this.
88
88
89
89
In particular:
@@ -102,21 +102,21 @@ exception by default, or both.
102
102
### Coercion and Stictness
103
103
104
104
* pydantic* currently leans on the side of trying to coerce types rather than raise an error if a type is wrong,
105
- see [ model data conversion] ( models.md#data-conversion ) and ` validate_assignment ` is no different.
105
+ see [ model data conversion] ( models.md#data-conversion ) and ` validate_arguments ` is no different.
106
106
107
107
See [ #1098 ] ( https://github.com/samuelcolvin/pydantic/issues/1098 ) and other issues with the "strictness" label
108
- for a discussion of this. If * pydantic* get's a "strict" mode in future, ` validate_assignment ` will have an option
108
+ for a discussion of this. If * pydantic* gets a "strict" mode in future, ` validate_arguments ` will have an option
109
109
to use this, it may even become the default for the decorator.
110
110
111
111
### Performance
112
112
113
113
We've made a big effort to make * pydantic* as performant as possible (see [ the benchmarks] ( ../benchmarks.md ) )
114
114
and argument inspect and model creation is only performed once when the function is defined, however
115
- there will still be a performance impact to using the ` validate_assignment ` decorator compared to
115
+ there will still be a performance impact to using the ` validate_arguments ` decorator compared to
116
116
calling the raw function.
117
117
118
118
In many situations this will have little or no noticeable effect, however be aware that
119
- ` validate_assignment ` is not an equivalent or alternative to function definitions in strongly typed languages,
119
+ ` validate_arguments ` is not an equivalent or alternative to function definitions in strongly typed languages;
120
120
it never will be.
121
121
122
122
### Return Value
0 commit comments