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
11 changes: 10 additions & 1 deletion src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,11 @@ module Helpers =

let sanitizedName =
match com.Options.Language with
| Python -> Fable.Py.Naming.sanitizeIdent Fable.Py.Naming.pyBuiltins.Contains name part
| Python ->
// Python classes are declared with PascalCase names (PEP 8), so entity
// references must use the same casing as the declaration
let name = Fable.Py.Naming.toPascalCase name
Fable.Py.Naming.sanitizeIdent Fable.Py.Naming.pyBuiltins.Contains name part
| Rust -> (entityName |> cleanNameAsRustIdentifier)
| Dart -> Naming.sanitizeDartIdent (fun _ -> false) name part
| _ -> Naming.sanitizeJsIdent (fun _ -> false) name part
Expand Down Expand Up @@ -768,7 +772,12 @@ module Helpers =
| 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
Fable.Py.Naming.sanitizeIdent Fable.Py.Naming.pyBuiltins.Contains name part
|> Fable.Py.Naming.toPythonNaming
| Rust -> Naming.buildNameWithoutSanitation name part
| Dart -> Naming.sanitizeDartIdent (fun _ -> false) name part
| _ -> Naming.sanitizeJsIdent (fun _ -> false) name part
Expand Down
21 changes: 14 additions & 7 deletions src/Fable.Transforms/Python/Fable2Python.Annotation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -727,13 +727,20 @@ let makeEntityTypeAnnotation com ctx (entRef: Fable.EntityRef) genArgs repeatedG
// Inside base class or not a union - use as-is
id

// Import the type if it's from another file
if ent.IsFSharpUnion then
match ent.Ref.SourcePath with
| Some path when path <> com.CurrentFile ->
let importPath = Path.getRelativeFileOrDirPath false com.CurrentFile false path
com.GetImportExpr(ctx, importPath, annotationName) |> ignore
| _ -> ()
// Import the type if it's from another file. The import may be aliased when the
// name clashes with a local declaration, so use the returned local identifier
let annotationName =
if ent.IsFSharpUnion then
match ent.Ref.SourcePath with
| Some path when path <> com.CurrentFile ->
let importPath = Path.getRelativeFileOrDirPath false com.CurrentFile false path

match com.GetImportExpr(ctx, importPath, annotationName) with
| Expression.Name { Id = Identifier localId } -> localId
| _ -> annotationName
| _ -> annotationName
else
annotationName

makeGenericTypeAnnotation com ctx annotationName genArgs repeatedGenerics, stmts
// TODO: Resolve references to types in nested modules
Expand Down
5 changes: 3 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.Transforms.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3355,7 +3355,7 @@ let declareDataClassType
|> List.filter (fun genArg ->
match genArg with
| Fable.DeclaredType({ FullName = fullName }, _) ->
Helpers.removeNamespace (fullName) <> entName
Naming.toPascalCase (Helpers.removeNamespace fullName) <> entName
| _ -> true
)

Expand Down Expand Up @@ -3628,7 +3628,8 @@ let declareClassType
int.GenericArgs
|> List.filter (fun genArg ->
match genArg with
| Fable.DeclaredType({ FullName = fullName }, _) -> Helpers.removeNamespace (fullName) <> entName
| Fable.DeclaredType({ FullName = fullName }, _) ->
Naming.toPascalCase (Helpers.removeNamespace fullName) <> entName
| _ -> true
)

Expand Down
7 changes: 7 additions & 0 deletions tests/Python/Misc/Util2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ type Helper3(i: int) =

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) =
member _.Size = size

type Direction = | Up


module Extensions =
type String with
Expand Down
13 changes: 13 additions & 0 deletions tests/Python/TestMisc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,19 @@
let ``test Naming values with same name as module works`` () =
equal 30 Same.Same.shouldEqual30

[<Fact>]
let ``test Lowercase type name from another file works`` () =
let s = Util2.shape (4)
s.Size |> equal 4

[<AttachMembers>]
type Direction() =
static member Up = Util2.Direction.Up

[<Fact>]
let ``test Type with same name as type from another file works`` () =
Direction.Up |> equal Util2.Direction.Up

[<Fact>]
let ``test Can access nested recursive function with mangled name`` () =
Util.Bar.nestedRecursive 3 |> equal 10
Expand Down Expand Up @@ -1179,7 +1192,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 1195 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 1195 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 1195 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 1195 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 1195 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

Check warning on line 1195 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
| _ -> x <- "?"
equal "3" x

Expand Down
Loading