Skip to content
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

Merged
merged 5 commits into from
Sep 25, 2024
Merged

Conversation

sydney-runkle
Copy link
Member

@sydney-runkle sydney-runkle commented Sep 20, 2024

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.

Copy link

codspeed-hq bot commented Sep 20, 2024

CodSpeed Performance Report

Merging #1459 will not alter performance

Comparing literal-enum-fix (06262e8) with main (4aa52a8)

Summary

✅ 155 untouched benchmarks

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),
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Contributor

@davidhewitt davidhewitt left a 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.

Copy link
Contributor

@davidhewitt davidhewitt left a 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!

@sydney-runkle sydney-runkle merged commit f389728 into main Sep 25, 2024
29 checks passed
@sydney-runkle sydney-runkle deleted the literal-enum-fix branch September 25, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Coercion of Literal with StrEnum and str is not consistent
2 participants