8
8
import sys
9
9
from collections .abc import Mapping
10
10
from datetime import date , datetime , time , timedelta
11
- from typing import TYPE_CHECKING , Any , Callable , Dict , Generic , Hashable , List , Optional , Set , Type , Union
12
-
13
- from typing_extensions import Literal , TypedDict , TypeVar
11
+ from typing import TYPE_CHECKING , Any , Callable , Dict , Hashable , List , Optional , Set , Type , Union
14
12
15
13
if sys .version_info < (3 , 11 ):
16
14
from typing_extensions import Protocol , Required , TypeAlias
17
15
else :
18
16
from typing import Protocol , Required , TypeAlias
19
17
18
+ if sys .version_info < (3 , 9 ):
19
+ from typing_extensions import Literal , TypedDict
20
+ else :
21
+ from typing import Literal , TypedDict
22
+
20
23
if TYPE_CHECKING :
21
24
from pydantic_core import PydanticUndefined
22
25
else :
@@ -163,10 +166,7 @@ def mode(self) -> Literal['python', 'json']:
163
166
...
164
167
165
168
166
- _FieldInfoType = TypeVar ('_FieldInfoType' , default = Any )
167
-
168
-
169
- class FieldValidationInfo (ValidationInfo , Protocol [_FieldInfoType ]):
169
+ class FieldValidationInfo (ValidationInfo , Protocol ):
170
170
"""
171
171
Argument passed to model field validation functions.
172
172
"""
@@ -177,9 +177,9 @@ def data(self) -> Dict[str, Any]:
177
177
...
178
178
179
179
@property
180
- def field_info (self ) -> _FieldInfoType :
180
+ def field_name (self ) -> str :
181
181
"""
182
- Information on the current field being validated if this validator is
182
+ The name of the current field being validated if this validator is
183
183
attached to a model field.
184
184
"""
185
185
...
@@ -1623,13 +1623,13 @@ class GeneralValidatorFunctionSchema(TypedDict):
1623
1623
1624
1624
1625
1625
# (__input_value: Any, __info: FieldValidationInfo) -> Any
1626
- FieldValidatorFunction = Callable [[Any , FieldValidationInfo [ _FieldInfoType ] ], Any ]
1626
+ FieldValidatorFunction = Callable [[Any , FieldValidationInfo ], Any ]
1627
1627
1628
1628
1629
- class FieldValidatorFunctionSchema (TypedDict , Generic [ _FieldInfoType ] ):
1629
+ class FieldValidatorFunctionSchema (TypedDict ):
1630
1630
type : Literal ['field' ]
1631
- function : FieldValidatorFunction [ _FieldInfoType ]
1632
- field_info : Required [ _FieldInfoType ]
1631
+ function : FieldValidatorFunction
1632
+ field_name : str
1633
1633
1634
1634
1635
1635
ValidationFunction = Union [NoInfoValidatorFunctionSchema , FieldValidatorFunctionSchema , GeneralValidatorFunctionSchema ]
@@ -1691,10 +1691,10 @@ def fn(v: bytes) -> str:
1691
1691
1692
1692
1693
1693
def field_before_validator_function (
1694
- function : FieldValidatorFunction [_FieldInfoType ],
1694
+ function : FieldValidatorFunction ,
1695
+ field_name : str ,
1695
1696
schema : CoreSchema ,
1696
1697
* ,
1697
- field_info : _FieldInfoType = None ,
1698
1698
ref : str | None = None ,
1699
1699
metadata : Any = None ,
1700
1700
serialization : SerSchema | None = None ,
@@ -1706,13 +1706,13 @@ def field_before_validator_function(
1706
1706
```py
1707
1707
from pydantic_core import SchemaValidator, core_schema
1708
1708
1709
- def fn(v: bytes, info: core_schema.FieldValidationInfo[str] ) -> str:
1709
+ def fn(v: bytes, info: core_schema.FieldValidationInfo) -> str:
1710
1710
assert info.data is not None
1711
- assert info.field_info == 'field_name'
1711
+ assert info.field_name is not None
1712
1712
return v.decode() + 'world'
1713
1713
1714
1714
func_schema = core_schema.field_before_validator_function(
1715
- function=fn, schema=core_schema.str_schema(), field_info='field_name'
1715
+ function=fn, field_name='a', schema=core_schema.str_schema()
1716
1716
)
1717
1717
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
1718
1718
@@ -1722,15 +1722,15 @@ def fn(v: bytes, info: core_schema.FieldValidationInfo[str]) -> str:
1722
1722
1723
1723
Args:
1724
1724
function: The validator function to call
1725
+ field_name: The name of the field
1725
1726
schema: The schema to validate the output of the validator function
1726
- field_info: optional metadata describing the field this validator is attached to
1727
1727
ref: optional unique identifier of the schema, used to reference the schema in other places
1728
1728
metadata: Any other information you want to include with the schema, not used by pydantic-core
1729
1729
serialization: Custom serialization schema
1730
1730
"""
1731
1731
return _dict_not_none (
1732
1732
type = 'function-before' ,
1733
- function = {'type' : 'field' , 'function' : function , 'field_info ' : field_info },
1733
+ function = {'type' : 'field' , 'function' : function , 'field_name ' : field_name },
1734
1734
schema = schema ,
1735
1735
ref = ref ,
1736
1736
metadata = metadata ,
@@ -1828,10 +1828,10 @@ def fn(v: str) -> str:
1828
1828
1829
1829
1830
1830
def field_after_validator_function (
1831
- function : FieldValidatorFunction [_FieldInfoType ],
1831
+ function : FieldValidatorFunction ,
1832
+ field_name : str ,
1832
1833
schema : CoreSchema ,
1833
1834
* ,
1834
- field_info : _FieldInfoType = None ,
1835
1835
ref : str | None = None ,
1836
1836
metadata : Any = None ,
1837
1837
serialization : SerSchema | None = None ,
@@ -1849,7 +1849,7 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:
1849
1849
return v + 'world'
1850
1850
1851
1851
func_schema = core_schema.field_after_validator_function(
1852
- function=fn, schema=core_schema.str_schema()
1852
+ function=fn, field_name='a', schema=core_schema.str_schema()
1853
1853
)
1854
1854
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
1855
1855
@@ -1859,15 +1859,15 @@ def fn(v: str, info: core_schema.FieldValidationInfo) -> str:
1859
1859
1860
1860
Args:
1861
1861
function: The validator function to call after the schema is validated
1862
+ field_name: The name of the field
1862
1863
schema: The schema to validate before the validator function
1863
- field_info: optional metadata describing the field this validator is attached to
1864
1864
ref: optional unique identifier of the schema, used to reference the schema in other places
1865
1865
metadata: Any other information you want to include with the schema, not used by pydantic-core
1866
1866
serialization: Custom serialization schema
1867
1867
"""
1868
1868
return _dict_not_none (
1869
1869
type = 'function-after' ,
1870
- function = {'type' : 'field' , 'function' : function , 'field_info ' : field_info },
1870
+ function = {'type' : 'field' , 'function' : function , 'field_name ' : field_name },
1871
1871
schema = schema ,
1872
1872
ref = ref ,
1873
1873
metadata = metadata ,
@@ -1941,13 +1941,13 @@ class GeneralWrapValidatorFunctionSchema(TypedDict):
1941
1941
1942
1942
1943
1943
# (__input_value: Any, __validator: ValidatorFunctionWrapHandler, __info: FieldValidationInfo) -> Any
1944
- FieldWrapValidatorFunction = Callable [[Any , ValidatorFunctionWrapHandler , FieldValidationInfo [ _FieldInfoType ] ], Any ]
1944
+ FieldWrapValidatorFunction = Callable [[Any , ValidatorFunctionWrapHandler , FieldValidationInfo ], Any ]
1945
1945
1946
1946
1947
- class FieldWrapValidatorFunctionSchema (TypedDict , Generic [ _FieldInfoType ] ):
1947
+ class FieldWrapValidatorFunctionSchema (TypedDict ):
1948
1948
type : Literal ['field' ]
1949
- function : FieldWrapValidatorFunction [ _FieldInfoType ]
1950
- field_info : _FieldInfoType
1949
+ function : FieldWrapValidatorFunction
1950
+ field_name : str
1951
1951
1952
1952
1953
1953
WrapValidatorFunction = Union [
@@ -2058,10 +2058,10 @@ def fn(
2058
2058
2059
2059
2060
2060
def field_wrap_validator_function (
2061
- function : FieldWrapValidatorFunction [_FieldInfoType ],
2061
+ function : FieldWrapValidatorFunction ,
2062
+ field_name : str ,
2062
2063
schema : CoreSchema ,
2063
2064
* ,
2064
- field_info : _FieldInfoType = None ,
2065
2065
ref : str | None = None ,
2066
2066
metadata : Any = None ,
2067
2067
serialization : SerSchema | None = None ,
@@ -2078,14 +2078,14 @@ def field_wrap_validator_function(
2078
2078
def fn(
2079
2079
v: bytes,
2080
2080
validator: core_schema.ValidatorFunctionWrapHandler,
2081
- info: core_schema.FieldValidationInfo[str] ,
2081
+ info: core_schema.FieldValidationInfo,
2082
2082
) -> str:
2083
2083
assert info.data is not None
2084
- assert info.field_info == 'field_name'
2084
+ assert info.field_name is not None
2085
2085
return validator(v) + 'world'
2086
2086
2087
2087
func_schema = core_schema.field_wrap_validator_function(
2088
- function=fn, schema=core_schema.str_schema(), field_info='field_name',
2088
+ function=fn, field_name='a', schema=core_schema.str_schema()
2089
2089
)
2090
2090
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
2091
2091
@@ -2095,15 +2095,15 @@ def fn(
2095
2095
2096
2096
Args:
2097
2097
function: The validator function to call
2098
+ field_name: The name of the field
2098
2099
schema: The schema to validate the output of the validator function
2099
- field_info: optional metadata describing the field this validator is attached to
2100
2100
ref: optional unique identifier of the schema, used to reference the schema in other places
2101
2101
metadata: Any other information you want to include with the schema, not used by pydantic-core
2102
2102
serialization: Custom serialization schema
2103
2103
"""
2104
2104
return _dict_not_none (
2105
2105
type = 'function-wrap' ,
2106
- function = {'type' : 'field' , 'function' : function , 'field_info ' : field_info },
2106
+ function = {'type' : 'field' , 'function' : function , 'field_name ' : field_name },
2107
2107
schema = schema ,
2108
2108
ref = ref ,
2109
2109
metadata = metadata ,
@@ -2112,8 +2112,8 @@ def fn(
2112
2112
2113
2113
2114
2114
class PlainValidatorFunctionSchema (TypedDict , total = False ):
2115
- function : Required [ValidationFunction ]
2116
2115
type : Required [Literal ['function-plain' ]]
2116
+ function : Required [ValidationFunction ]
2117
2117
ref : str
2118
2118
metadata : Any
2119
2119
serialization : SerSchema
@@ -2194,9 +2194,9 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str:
2194
2194
2195
2195
2196
2196
def field_plain_validator_function (
2197
- function : FieldValidatorFunction [_FieldInfoType ],
2197
+ function : FieldValidatorFunction ,
2198
+ field_name : str ,
2198
2199
* ,
2199
- field_info : _FieldInfoType = None ,
2200
2200
ref : str | None = None ,
2201
2201
metadata : Any = None ,
2202
2202
serialization : SerSchema | None = None ,
@@ -2208,12 +2208,12 @@ def field_plain_validator_function(
2208
2208
from typing import Any
2209
2209
from pydantic_core import SchemaValidator, core_schema
2210
2210
2211
- def fn(v: Any, info: core_schema.FieldValidationInfo[str] ) -> str:
2211
+ def fn(v: Any, info: core_schema.FieldValidationInfo) -> str:
2212
2212
assert info.data is not None
2213
- assert info.field_info == 'field_name'
2213
+ assert info.field_name is not None
2214
2214
return str(v) + 'world'
2215
2215
2216
- func_schema = core_schema.field_plain_validator_function(function=fn, field_info='field_name ')
2216
+ func_schema = core_schema.field_plain_validator_function(function=fn, field_name='a ')
2217
2217
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)})
2218
2218
2219
2219
v = SchemaValidator(schema)
@@ -2222,14 +2222,14 @@ def fn(v: Any, info: core_schema.FieldValidationInfo[str]) -> str:
2222
2222
2223
2223
Args:
2224
2224
function: The validator function to call
2225
- field_info: optional metadata describing the field this validator is attached to
2225
+ field_name: The name of the field
2226
2226
ref: optional unique identifier of the schema, used to reference the schema in other places
2227
2227
metadata: Any other information you want to include with the schema, not used by pydantic-core
2228
2228
serialization: Custom serialization schema
2229
2229
"""
2230
2230
return _dict_not_none (
2231
2231
type = 'function-plain' ,
2232
- function = {'type' : 'field' , 'function' : function , 'field_info ' : field_info },
2232
+ function = {'type' : 'field' , 'function' : function , 'field_name ' : field_name },
2233
2233
ref = ref ,
2234
2234
metadata = metadata ,
2235
2235
serialization = serialization ,
0 commit comments