Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fable-Python] import statement falsely changed to snake_case. #3808

Closed
Freymaurer opened this issue Apr 18, 2024 · 5 comments · Fixed by #3811 or #3821
Closed

[Fable-Python] import statement falsely changed to snake_case. #3808

Freymaurer opened this issue Apr 18, 2024 · 5 comments · Fixed by #3811 or #3821
Labels

Comments

@Freymaurer
Copy link

The minimal reproduction example requires two files:

Setup

// TestImport.fsx

#r "nuget: Fable.Core, 4.3.0"

open Fable.Core

type FlowchartDirection = 
    | TB
    | TD

[<AttachMembers>]
type flowchartDirection =
    static member tb = FlowchartDirection.TB
    static member td = FlowchartDirection.TD
// test.fsx

#r "nuget: Fable.Core, 4.3.0"
#load "./TestImport.fsx"

open TestImport

printfn "Hello World: %A" flowchartDirection.tb

Transpiled Files

Transpile with dotnet fable .\test.fsx --lang py. I used fable version 4.16.0

# test_import.py
#...
class flowchartDirection:
    @staticmethod
    def tb() -> FlowchartDirection:
        return FlowchartDirection(0)

    @staticmethod
    def td() -> FlowchartDirection:
        return FlowchartDirection(1)
#...
# test.py
from __future__ import annotations
from fable_modules.fable_library.string_ import (to_console, printf)
from test_import import (flowchart_direction, FlowchartDirection)

arg: FlowchartDirection = flowchart_direction.bt()

to_console(printf("Hello World: %A"))(arg)

Problem

In test_import.py we can still find "flowchartDirection", but the import statement in test.py was falsely changed to snake_case.

Personally i would favor having the import be the correct name, and keep the name as written. I hope this helps finding the underlying issue.

@dbrattli
Copy link
Collaborator

dbrattli commented Apr 22, 2024

I've added a fix for this (#3811), but the class will be translated to snake case i.e flowchart_direction. This is because we currently snake_case everything that do not start with a capital letter. So you will need to CapWords the class name to avoid snake casing. Either rename it to Flowchartdirection or use a module instead so you can use the same name as the union e.g:

namespace TestImport

open Fable.Core

type FlowchartDirection =
    | TB
    | TD

module FlowchartDirection =
    let tb = FlowchartDirection.TB
    let td = FlowchartDirection.TD

@Freymaurer
Copy link
Author

as long as the import works correctly its fine with me 🙂

@Freymaurer
Copy link
Author

Freymaurer commented May 12, 2024

One more follow up note:

this reflection function still contains the camel case name:

image

Not sure for what it is uses, just wanted to mention it @dbrattli

@Freymaurer
Copy link
Author

Freymaurer commented May 12, 2024

ah damn one more thing, and this is breaking:

Here you can see f# code in which i use a static member of stateDiagram.

image

in transpiled python (fable v.4.17.0) state_diagram is correct snake_case, but the reference in the members is still stateDiagram:

image

EDIT: Using the following workaround i used before your update, still makes it transpile correctly:

#if FABLE_COMPILER_PYTHON
[<CompiledName("state_diagram")>]
#endif
[<AttachMembers>]
type stateDiagram =

..transpiles to:

image

@dbrattli

@dbrattli
Copy link
Collaborator

Have added fixes for this. Please reopen or post new issues if you find new problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants