-
Notifications
You must be signed in to change notification settings - Fork 293
Ensure ValidationInfo.field_name
is correct on validator reuse
#1692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c0f01df
Pass field_name to validators through ValidationState so functions ar…
DouweM 0fd7ef1
Update test as validate_assignment ValidationInfo now gets a field_name
DouweM df34451
Update tests as ValidationInfo now always gets a field_name
DouweM e609bb3
Only clone the field_name when necessary
DouweM 73c2362
Set ValidationState.field_name for ROOT_FIELD
DouweM 9bac3bd
Deprecate field_name argument on core_schema.with_info_{before,after,…
DouweM b5cc2e9
Set stacklevel on validator function field_name arg DeprecationWarning
DouweM 718324d
Set field_name when validating call arguments
DouweM 3d69981
Add deprecated comment to WithInfoWrapValidatorFunctionSchema.field_name
DouweM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1954,7 +1954,7 @@ class NoInfoValidatorFunctionSchema(TypedDict): | |
class WithInfoValidatorFunctionSchema(TypedDict, total=False): | ||
type: Required[Literal['with-info']] | ||
function: Required[WithInfoValidatorFunction] | ||
field_name: str | ||
field_name: str # deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this |
||
|
||
|
||
ValidationFunction = Union[NoInfoValidatorFunctionSchema, WithInfoValidatorFunctionSchema] | ||
|
@@ -2042,7 +2042,7 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str: | |
return v.decode() + 'world' | ||
|
||
func_schema = core_schema.with_info_before_validator_function( | ||
function=fn, schema=core_schema.str_schema(), field_name='a' | ||
function=fn, schema=core_schema.str_schema() | ||
) | ||
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)}) | ||
|
||
|
@@ -2052,13 +2052,20 @@ def fn(v: bytes, info: core_schema.ValidationInfo) -> str: | |
|
||
Args: | ||
function: The validator function to call | ||
field_name: The name of the field | ||
field_name: The name of the field this validator is applied to, if any (deprecated) | ||
schema: The schema to validate the output of the validator function | ||
ref: optional unique identifier of the schema, used to reference the schema in other places | ||
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type | ||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||
serialization: Custom serialization schema | ||
""" | ||
if field_name is not None: | ||
warnings.warn( | ||
'The `field_name` argument on `with_info_before_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.', | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
|
||
return _dict_not_none( | ||
type='function-before', | ||
function=_dict_not_none(type='with-info', function=function, field_name=field_name), | ||
|
@@ -2140,7 +2147,7 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str: | |
return v + 'world' | ||
|
||
func_schema = core_schema.with_info_after_validator_function( | ||
function=fn, schema=core_schema.str_schema(), field_name='a' | ||
function=fn, schema=core_schema.str_schema() | ||
) | ||
schema = core_schema.typed_dict_schema({'a': core_schema.typed_dict_field(func_schema)}) | ||
|
||
|
@@ -2151,11 +2158,18 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str: | |
Args: | ||
function: The validator function to call after the schema is validated | ||
schema: The schema to validate before the validator function | ||
field_name: The name of the field this validators is applied to, if any | ||
field_name: The name of the field this validator is applied to, if any (deprecated) | ||
ref: optional unique identifier of the schema, used to reference the schema in other places | ||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||
serialization: Custom serialization schema | ||
""" | ||
if field_name is not None: | ||
warnings.warn( | ||
'The `field_name` argument on `with_info_after_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.', | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
|
||
return _dict_not_none( | ||
type='function-after', | ||
function=_dict_not_none(type='with-info', function=function, field_name=field_name), | ||
|
@@ -2187,7 +2201,7 @@ class NoInfoWrapValidatorFunctionSchema(TypedDict): | |
class WithInfoWrapValidatorFunctionSchema(TypedDict, total=False): | ||
type: Required[Literal['with-info']] | ||
function: Required[WithInfoWrapValidatorFunction] | ||
field_name: str | ||
field_name: str # deprecated | ||
|
||
|
||
WrapValidatorFunction = Union[NoInfoWrapValidatorFunctionSchema, WithInfoWrapValidatorFunctionSchema] | ||
|
@@ -2287,12 +2301,19 @@ def fn( | |
Args: | ||
function: The validator function to call | ||
schema: The schema to validate the output of the validator function | ||
field_name: The name of the field this validators is applied to, if any | ||
field_name: The name of the field this validator is applied to, if any (deprecated) | ||
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type | ||
ref: optional unique identifier of the schema, used to reference the schema in other places | ||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||
serialization: Custom serialization schema | ||
""" | ||
if field_name is not None: | ||
warnings.warn( | ||
'The `field_name` argument on `with_info_wrap_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.', | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
|
||
return _dict_not_none( | ||
type='function-wrap', | ||
function=_dict_not_none(type='with-info', function=function, field_name=field_name), | ||
|
@@ -2379,12 +2400,19 @@ def fn(v: str, info: core_schema.ValidationInfo) -> str: | |
|
||
Args: | ||
function: The validator function to call | ||
field_name: The name of the field this validators is applied to, if any | ||
field_name: The name of the field this validator is applied to, if any (deprecated) | ||
ref: optional unique identifier of the schema, used to reference the schema in other places | ||
json_schema_input_schema: The core schema to be used to generate the corresponding JSON Schema input type | ||
metadata: Any other information you want to include with the schema, not used by pydantic-core | ||
serialization: Custom serialization schema | ||
""" | ||
if field_name is not None: | ||
warnings.warn( | ||
'The `field_name` argument on `with_info_plain_validator_function` is deprecated, it will be passed to the function through `ValidationState` instead.', | ||
DeprecationWarning, | ||
stacklevel=2, | ||
) | ||
|
||
return _dict_not_none( | ||
type='function-plain', | ||
function=_dict_not_none(type='with-info', function=function, field_name=field_name), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.