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
19 changes: 14 additions & 5 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -767,15 +767,24 @@ module Helpers =
match com.Options.Language with
| Python ->
let name =
// Don't apply Python naming convention if member has compiled name attribute
// Don't snake_case the prefix for [<CompiledName>] members. This is
// largely subsumed by the re-casing of the whole composed name below,
// but keeps the intent explicit at this step.
match memb.Attributes |> Helpers.tryFindAttrib Atts.compiledName with
| Some _ -> name
| _ -> Fable.Py.Naming.toPythonNaming name

// Reference sites re-case the full composed name, so apply the naming
// convention to the composed name here as well. Otherwise a lowercase
// member name with an overload suffix is declared with the suffix
// preserved but referenced with it snake_cased
// Import/reference sites re-case the full composed name unconditionally
// (see the imports printer in Fable2Python.Transforms), so re-case it here
// too to keep declarations and references in agreement. This is what makes
// a lowercase member with an overload suffix, e.g. `foo__ctor_Z<hash>`, be
// both declared and imported as `foo__ctor_z<hash>`.
//
// Caveat: being unconditional, this also overrides the [<CompiledName>] skip
// above when the composed name has no uppercase entity/module prefix, i.e. a
// root-level compiled name starting lowercase is snake_cased. Prefixed names
// (class / nested-module members) keep their uppercase prefix, so toPythonNaming
// is a no-op there and the compiled name survives.
Fable.Py.Naming.sanitizeIdent Fable.Py.Naming.pyBuiltins.Contains name part
|> Fable.Py.Naming.toPythonNaming
| Rust -> Naming.buildNameWithoutSanitation name part
Expand Down
4 changes: 4 additions & 0 deletions tests/Python/Misc/Util2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type H = Helper3
// Lowercase type name at the root of the file: the Python class declaration and
// its references from other files must agree on the name
type shape(size: int) =
// Overloaded constructors are emitted as module-level names with an overload
// suffix (e.g. `shape__ctor_Z<hash>`) and imported across files. The declaration
// and the import must agree on the suffix casing too, not just the base name.
new(label: string) = shape(label.Length)
member _.Size = size

type Direction = | Up
Expand Down
8 changes: 8 additions & 0 deletions tests/Python/TestMisc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,14 @@
let s = Util2.shape (4)
s.Size |> equal 4

[<Fact>]
let ``test Overloaded constructor on lowercase type from another file works`` () =
// Exercises the `shape__ctor_Z<hash>` overload-suffix casing across files
let s1 = Util2.shape (4)
let s2 = Util2.shape ("abc")
s1.Size |> equal 4
s2.Size |> equal 3

[<AttachMembers>]
type Direction() =
static member Up = Util2.Direction.Up
Expand Down Expand Up @@ -1192,7 +1200,7 @@
| 2 -> x <- "2"
| 3 | 4 -> x <- "3" // Multiple cases are allowed
// | 5 | 6 as j -> x <- string j // This prevents the optimization
| 4 -> x <- "4" // Unreachable cases are removed

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / analyzers (src/Fable.AST/Fable.AST.fsproj)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.13)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.14)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.14)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

This rule will never be matched

Check warning on line 1203 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.13)

This rule will never be matched
| _ -> x <- "?"
equal "3" x

Expand Down
Loading