Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Python] Fix record member method naming (by @dbrattli)
* [Python] Fix regression, named arguments not being converted to snake_case (by @dbrattli)
* [Python] Fix regression, erased interfaces should not generate code (#4277) (by @dbrattli)

Expand Down
4 changes: 3 additions & 1 deletion src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* Fix regression, Python erased interfaces should not generate code (#4277) (by @dbrattli)
* [Python] Fix regression, record member method naming (by @dbrattli)
* [Python] Fix regression, named arguments not being converted to snake_case (by @dbrattli)
* [Python] Fix regression, erased interfaces should not generate code (#4277) (by @dbrattli)

### Changed

Expand Down
7 changes: 5 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ module Util =
| None -> InstancePropertyBacking // Conservative fallback for unknown entities
| _ -> RegularField

/// Checks if a field name is an actual record field (not a property/method defined on the record)
let isRecordField (ent: Fable.Entity) (fieldName: string) =
ent.FSharpFields |> List.exists (fun f -> f.Name = fieldName)

// Helper function to apply the appropriate naming convention based on field type and naming kind
let applyFieldNaming
(com: IPythonCompiler)
Expand All @@ -202,9 +206,8 @@ module Util =
match narrowedType with
| Fable.AnonymousRecordType _ when handleAnonymousRecords -> fieldName // Use the field name as is for anonymous records
| Fable.DeclaredType(entityRef, _) ->
// Only apply naming convention for user-defined F# Records (not built-in F# Core types)
match com.TryGetEntity entityRef with
| Some ent when shouldUseRecordFieldNamingForRef entityRef ent ->
| Some ent when shouldUseRecordFieldNamingForRef entityRef ent && isRecordField ent fieldName ->
fieldName |> Naming.toRecordFieldSnakeCase |> Helpers.clean
| _ -> fieldName |> Naming.toPythonNaming // Fallback to Python naming for other types
| _ -> fieldName |> Naming.toPropertyNaming
Expand Down
11 changes: 10 additions & 1 deletion tests/Python/TestRecordType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,13 @@ type CasingRecord =
let ``test Record with both camel-case and pascal-case fields don't conflict`` () =
let x = { firstName = "John"; FirstName = "Jane" }
equal "John" x.firstName
equal "Jane" x.FirstName
equal "Jane" x.FirstName

type RecordWithProperty =
{ items: string list }
member this.fullName = String.concat " - " this.items

[<Fact>]
let ``test Record property access uses correct naming`` () =
let x = { items = ["Hello"; "World"] }
equal "Hello - World" x.fullName
Loading