Fix NotImplementedError on heterogeneous union model fields#747
Open
wbarnha wants to merge 1 commit into
Open
Conversation
A Record field annotated with a heterogeneous union -- one whose members are neither all Model subclasses nor all scalar "literal" types, e.g. `str | list | dict | None` -- reached `UnionNode.build`, which unconditionally raised `NotImplementedError` at class-definition time. This surfaced after mode-streaming began recognizing PEP 604 (`X | Y`) unions (see faust-streaming/mode#80): such annotations now register as unions and reach this node, whereas previously an unrecognized `X | Y` fell through to a bare pass-through. The exception is raised here by faust's type-expression compiler, not by mode. There is no unambiguous way to coerce a value into a heterogeneous union, so build a pass-through expression instead -- mirroring how an all-literal union such as `Union[str, int, float]` is already handled (it collapses to a bare pass-through via `_maybe_unroll_union`). This restores the behavior that existed before the union was recognized and lets these models be defined again; `Optional`/`None` members keep their existing optional wrapping. Add regression tests covering both the `typing.Union` and PEP 604 spellings, at the `TypeExpression` level and end-to-end through `Record`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BnTZQEXqpedyrFAgwRnbsA
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #747 +/- ##
==========================================
+ Coverage 95.98% 96.06% +0.07%
==========================================
Files 103 103
Lines 11069 11069
Branches 1191 1191
==========================================
+ Hits 10625 10633 +8
+ Misses 350 345 -5
+ Partials 94 91 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Description
Defining a
faust.Recordwith a heterogeneous union field — one whose members are neither allModelsubclasses nor all scalar "literal" types (str/bytes/float/int) — raisedNotImplementedError: Union of types ... not supportedat class-definition time. Minimal reproduction (from faust-streaming/mode#80):Root cause
faust's type-expression compiler (
faust/models/typing.py) dispatches unions toUnionNode.UnionNode._maybe_unroll_unioncollapses two union shapes to a single node — all-Modelunions, and all-LITERAL_TYPESunions (which is whyUnion[str, int, float]already compiles to a bare pass-through). Every other union fell through toUnionNode.build, which unconditionally raised.That path became reachable for
X | Yannotations once mode-streaming began recognizing PEP 604 unions (mode 0.5.3):str | list | dict | Nonenow registers as a union and reaches this node, whereas an unrecognizedX | Ypreviously fell through to a literal pass-through. The exception is raised by faust's compiler, not by mode — this is the faust-side fix for the boundary characterized in faust-streaming/mode#81.Fix
A heterogeneous union has no unambiguous coercion target, so
UnionNode.buildnow emits a pass-through expression (the already-deserialized value is returned unchanged), mirroring the existing all-literal handling.Optional/Nonemembers keep their optional wrapping ((a if a is not None else None)). This restores the pre-0.5.3 behavior and is strictly more permissive than the previous hard failure — no existing test asserted the raise.Tests
TypeExpression-level cases added to the parametrizedCASES:Union[str, list, dict]→a, andUnion[str, list, dict, None]→(a if a is not None else None).Recordregression tests for both thetyping.Unionand PEP 604 (|) spellings, asserting values (includingNone) round-trip unchanged.All 108
tests/{functional,unit}/modelstests pass locally;isort/black/flake8clean.Fixes the crash reported in faust-streaming/mode#80.
🤖 Generated with Claude Code
https://claude.ai/code/session_01BnTZQEXqpedyrFAgwRnbsA
Generated by Claude Code