-
Notifications
You must be signed in to change notification settings - Fork 235
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 equality checks for primitives in literals #1459
Conversation
CodSpeed Performance ReportMerging #1459 will not alter performanceComparing Summary
|
src/validators/literal.rs
Outdated
expected_str: (!expected_str.is_empty()).then_some(expected_str), | ||
expected_py_dict: (!expected_py_dict.is_empty()).then_some(expected_py_dict.into()), | ||
expected_py_values: (!expected_py_values.is_empty()).then_some(expected_py_values), | ||
expected_py_primitives: (!expected_py_primitives.is_empty()).then_some(expected_py_primitives), |
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 wonder, is there an advantage of expected_py_primitives
over expected_py_dict
?
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.
Conclusion was that expected_py_primitives
will be used only in lax
mode. I think it probably makes sense to store as a dict and use hash lookup (as that checks __hash__
and __eq__
) - we can always make more lax 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.
Also conclusion - we're not going to fall back to union validation, that's too lax + time consuming.
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 - can't enforce strict / lax here as we're in the literal lookup, but I've added the hash check.
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.
If enforcing the hash check, surely a dict is just straight up more performant?
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.
Overall I'm ok with this, I think, though we should check we all agree that making Literal
validation a little more lax (via __eq__
) is desirable.
FWIW I think the alternative we understood to be to fall back to union validation if the input didn't validate successfuly.
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.
LGTM, thanks for the many iterations!
Fix pydantic/pydantic#9989
Also just some minor tidying of the surrounding code.
Maybe we don't want this change - this could be a bit dangerous with custom types that implement
__eq__
. I'm not opposed to closing the above issue and closing this PR, but this at least presents a solution if we do want to move forward.