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
Make @validate_call
return a function instead of a custom descriptor - fixes binding issue with inheritance and adds self/cls
argument to validation errors
#8268
Merged
davidhewitt
merged 5 commits into
pydantic:main
from
alexmojaki:validate-call-plain-function-wrapper
Dec 11, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a49093f
Make validate_call return a function instead of a custom descriptor
alexmojaki 05e0397
move import, clarify names
alexmojaki 13185da
Simplify ValidateCallWrapper, remove unneeded instance variables
alexmojaki 2edf728
Remove unused CallMarker
alexmojaki 494bc53
remove stale docstring part
alexmojaki File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
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.
So the observable change with this approach looks like the error messages will now always show the
self
orcls
argument. There's debate to be had whether this is a good thing; showing this argument is less implicit but now it doesn't match exactly what the user typed as input arguments, which has potential for confusion.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.
IMO Python already forces users to get used to the idea of the extra self argument with error messages like this:
I think there may be times when showing the value of
self
(assuming it has a usefulrepr
) might make the error message more useful by providing some extra context about the state of things aside from the actual validation error.But I'm worried there might be cases where users broadly catch validation errors, serialize them to JSON, and return that in an API. I don't know if that's common but it looks like an intended use case. Then I expect that JSON serialization errors may start appearing, or the validation errors seen by clients (who are far removed from the Python code) will look confusing.
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.
Right, good points. I think overall we tend to be ok with error messages changing (we generally justify this by saying that we work to improve error messages). So given that Python itself is a bit mixed here, I'm tempted to argue let's merge this patch as it's a fantastic simplification, and then if we want to work again to improve this later we revisit.