fix(python): avoid union case field name collision with Union.name#4647
Merged
Conversation
A named union case field called `name` (e.g. `Case of name: string * optional: string`) generated a Python dataclass field `name: str`. The `Union` base class defines a `name` property, which `@dataclass` treats as the field's default value, so any following field without a default raised "non-default argument follows default argument" at runtime. Use `toRecordFieldSnakeCase` for union case fields (same convention records already use), so `name` becomes `name_` and no longer collides. Union construction is positional and field access is by index, so the dataclass field name is purely internal. Fixes #4645 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Python Type Checking Results (Pyright)
Excluded files with errors (4 files)These files have known type errors and are excluded from CI. Remove from
|
The helper is now used for union case fields too, not just records, so the "Record" qualifier is misleading. Rename to toFieldSnakeCase and tidy the related comments. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
dbrattli
added a commit
that referenced
this pull request
Jun 10, 2026
- Replace the 'Reflection Constructor' section with 'Runtime References Target the Base Class', documenting the centralized redirectUnionToPythonBaseClass helper and both of its call sites, including the Python-gated one in the shared FSharp2Fable.Util.fs. - Add a 'Case Field Naming' section for the toFieldSnakeCase convention introduced by #4647 (fixes the Union.name dataclass collision, #4645) and update the generated-output examples accordingly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
Fixes #4645.
A named union case field called
namefails at runtime in Python:raises:
Root cause
The generated case class looked like:
The
Unionbase class (fable_library/union.py) defines anameproperty. When@dataclassprocesses thenameannotation, it finds that inherited property object viagetattr(cls, 'name')and treats it as the field's default value. The next field without a default (optional) then violates Python's "non-default argument follows default argument" rule.This is the same field-name collision class that records already solve with
toFieldSnakeCase(formerlytoRecordFieldSnakeCase); union case field generation was the one place still using plaintoSnakeCase.Fix
Use
toFieldSnakeCasefor union case field annotations, sonamebecomesname_and no longer collides:This is safe because union construction is positional (
Union_Case("v1", "v2")) and field access is by index (self.fields[i]), so the dataclass field name is purely internal.Rename
Since the
toRecordFieldSnakeCasehelper is now used for union case fields too — not just records — the "Record" qualifier was misleading. Renamed it totoFieldSnakeCaseacross the Python transforms and tidied the related comments. (shouldUseRecordFieldNamingstays as-is: it is genuinely record-specific.)Verification
TypeError, confirmed the fix runs correctly.tests/Python/TestUnionType.fs.🤖 Generated with Claude Code