Skip to content

Conversation

dmontagu
Copy link
Contributor

@dmontagu dmontagu commented Jul 4, 2023

Closes #6438

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Jul 4, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 397c3c4
Status: ✅  Deploy successful!
Preview URL: https://3de20f36.pydantic-docs2.pages.dev
Branch Preview URL: https://dmontagu-6438.pydantic-docs2.pages.dev

View logs

strict_python_schema = core_schema.is_instance_schema(enum_type)
if use_enum_values:
strict_python_schema = core_schema.chain_schema(
[strict_python_schema, core_schema.no_info_plain_validator_function(lambda x: x.value)]
Copy link
Contributor Author

@dmontagu dmontagu Jul 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what happens is that, when parsing unions, it tries to parse in strict mode first so that it gets the right union case, rather than the first one, when possible. Because the strict mode python schema wasn't applying the use_enum_values thing before, it didn't affect the parsing of unions.

I guess it was also a bug that if you were to validate an enum in strict mode with use_enum_values=True it wouldn't actually use the enum values either. I've added a test for that below (and did confirm it fails on main)

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 👍

@dmontagu dmontagu merged commit c9292af into main Jul 5, 2023
@dmontagu dmontagu deleted the dmontagu/6438 branch July 5, 2023 15:29
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.

'use_enum_values' not working in union types
2 participants