-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
fix for #8512 #8567
Merged
sydney-runkle
merged 5 commits into
pydantic:main
from
Anvil:plain-validator-and-serializer
Jan 19, 2024
Merged
fix for #8512 #8567
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
202a251
propagate serialization information in PlainValidator schema creation…
Anvil 33dc02c
Test that PlainValidator actually keep serialization information in t…
Anvil b10e771
ensure correct type is used for serialization in PlainValidator using…
Anvil dac27ff
put both serializer/validator combinations in test_plain_validator_pl…
Anvil 1c222e9
add bug reference as test_plain_validator_plain_serializer docstring
Anvil 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 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can avoid introducing a wrapper here and just do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello.
Thanks for the advice. I originally tried that, but ended up with some type checking errors:
Should I cast the value ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha, pardon my french.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with_info_plain_validator_function
accepts aSerSchema
: https://github.com/pydantic/pydantic-core/blob/4da7192ffc104cd6c424d102bc1c9c5bfdad543e/python/pydantic_core/core_schema.py#L2183But some schemas, e.g.
dict
have other serializers: https://github.com/pydantic/pydantic-core/blob/4da7192ffc104cd6c424d102bc1c9c5bfdad543e/python/pydantic_core/core_schema.py#L1724-L1733@davidhewitt I don't recall what the deal was with the inc/exc stuff but I'd suggest one of two things:
SerSchema
union if this usage is validassert schema['type'] in ...
orassert schema['type'] not in ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potentially instead of the
assert
we can fall back to the slower wrap serializer proposed here just to be safe?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont get all the details (yet?), but if I can be of any use, let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, adding the inc/exc stuff to the
SerSchema
union isn't easily done - for ex:We would end up having recursive defs in the
SerSchema
union, as this is how those types are defined. For now, I think it makes sense to do a type check, and perhaps we could add an issue to thepydantic-core
board if we want to figure out a way to add those types to theSerSchema
Union in the long term.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, a proposed fix:
This requires some
# type: ignore
statements, which isn't ideal. However, it's not possible to check if something is an instance ofSerSchema
, as that's a union of types. Furthermore, checking against the individual types is difficult bc they're oftenTypedDict
types, which aren't friendly with isinstance checks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, let's go with:
Or something along those lines. @Anvil, could you please implement that change + we can get this across the line today?
Just as a side note - this approach still feels a bit unclean to me, so I'm going to mark this as something to come back to and see if we can clean up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, after discussing a bit more with @adriangb, I think that my above suggestion is ugly enough that we shouldn't use that alternative.
@Anvil, let's go with what you currently have, and I'll create an issue to address the performance of the
line that you've added. I'd rather have the bug fix now and improve the performance down the line.
Thanks for all of your help on this!