fix(python): keep original F# field names in record reflection#4852
Merged
Conversation
Python was the only target that mangled record field names in the reflection TypeInfo, not just the runtime slots. FSharpType.GetRecordFields returned snake_case names (first_name, httpstatus, id) instead of the F# names (FirstName, HTTPStatus, ID) that .NET, JS/TS and Beam all report. The transform is lossy, so reflection-driven serializers can't reproduce the .NET/JS wire format. Emit the pristine F# field name as PropertyInfo.Name, carrying the snake_case slot as an optional 3rd tuple element (only when it differs) so value access still resolves the real attribute. fable-library-py reads that slot for get_value/get_record_field, falling back to element 0 for already-published 2-tuple output. This mirrors the Beam target's name/erl_name split. Union reflection already kept the pristine name. Collapses the FABLE_COMPILER test fork that enshrined the mangled names and adds coverage for multi-word/acronym fields. Co-Authored-By: Claude Opus 4.8 <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
|
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
Python is the only Fable target that mangles record field names in the reflection
TypeInfo, not just the runtime slots. Fortype Rec = { FirstName: string; HTTPStatus: int; ID: int }:FSharpType.GetRecordFields(t).[i].NameFirstName,HTTPStatus,IDfirst_name,httpstatus,idThe transform is lossy (
httpstatuscould beHttpStatus/HTTPStatus/Httpstatus), so any reflection-driven serializer on Python can't reproduce the wire format that .NET and the JS/TS backend produce. Beam already demonstrates the intended design: snake_case runtime map keys, but a pristine F# name in the reflection metadata (name/erl_namesplit).Fix
Fable2Python.Reflection.fs): emitfi.Name(pristine) as the reflection field name, adding the snake_case slot as an optional 3rd tuple element, only when it differs from the F# name.fable-library-py/reflection.py): widenFieldInfototuple[str, TypeInfo] | tuple[str, TypeInfo, str];get_value/get_record_fieldresolve the attribute via a new_field_attr_namehelper (element2if present, else0). Backward-compatible with already-published 2-tuple output.Tests
#if FABLE_COMPILERfork inTestReflection.fsthat enshrined the mangled names ("string"/"int",map [(age..)]) down to the single .NET-matching version.test Record reflection keeps original F# field namescovering{ FirstName; HTTPStatus; ID }: reflected names equal the F# names, andGetValue/MakeRecordstill resolve the mangled slots.All 2418 Python tests pass; Pyright clean (0 errors, no
# type: ignore).Notes
src/fable-library-pyis PyPI-distributed; this is backward-compatible within the minor version — the library still reads old 2-tuple reflection output, andPropertyInfo.Namesemantics only become more correct.🤖 Generated with Claude Code