Fix nested discriminated unions not discovered by env/CLI providers#816
Merged
Fix nested discriminated unions not discovered by env/CLI providers#816
Conversation
When a field uses `Annotated[Union[A, B], Discriminator(...)]`, the env and CLI providers failed to discover the inner BaseModel subclasses because `_union_is_complex` delegated to `_annotation_is_complex` which doesn't handle bare Union types. Similarly, `_CliArg.sub_models` only checked immediate union args for model classes. - `_union_is_complex`: after `_annotation_is_complex` returns False, recurse into inner unions (respecting Json metadata suppression). - `_CliArg.sub_models`: extract model collection into a recursive `_collect_sub_models` helper that walks nested unions. Fixes #815 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
33f9526 to
e02af20
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #815
_union_is_complex(utils.py): When a union arg isAnnotated[Union[A, B], Discriminator(...)],_annotation_is_complexstrips the Annotated wrapper and sees a bareUnion[A, B], which it can't evaluate — returningFalse. The fix recurses into such inner unions while respectingJsonmetadata that intentionally suppresses complexity._CliArg.sub_models(cli.py): Extracted model collection into a recursive_collect_sub_modelshelper that walks nested unions to find allBaseModel/dataclass subclasses.Test plan
test_nested_discriminated_unionintest_settings.py— validates env vars with nested delimiter (a_or_b__x,a_or_b__b)test_cli_nested_discriminated_unionintest_source_cli.py— validates CLI args (--a_or_b.x,--a_or_b.b)test_json_field_with_discriminated_unionstill passes (Json metadata respected)test_env_union_with_complex_subfields_parses_plain_if_json_failsstill passes (parse-failure fallback preserved)test_settings.py(190 passed) andtest_source_cli.py(169 passed) suites green🤖 Generated with Claude Code