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

Python 5.0.0-alpha.10 does not alias imports introducing "Reassignment" errors #4089

Open
Freymaurer opened this issue Mar 25, 2025 · 2 comments · May be fixed by #4090
Open

Python 5.0.0-alpha.10 does not alias imports introducing "Reassignment" errors #4089

Freymaurer opened this issue Mar 25, 2025 · 2 comments · May be fixed by #4090

Comments

@Freymaurer
Copy link
Contributor

Using Thoth.Json.Python in Fable 5.0.0-alpha.9 we get in thoth_json_python/encode.py the following:

import json as json_1
...

def Decode_fromString(decoder: Decoder_1[Any], value: str) -> FSharpResult_2[Any, str]:
    try: 
        json: Any = json_1.loads(value) # This is the relevant line
        match_value: FSharpResult_2[_T, tuple[str, ErrorReason_1[Any]]] = decoder.Decode(Decode_helpers, json)
        if match_value.tag == 1:
            final_error: tuple[str, ErrorReason_1[Any]]
            tupled_arg: tuple[str, ErrorReason_1[Any]] = match_value.fields[0]
            final_error = Helpers_prependPath("$", tupled_arg[0], tupled_arg[1])
            return FSharpResult_2(1, error_to_string(Decode_helpers, final_error[0], final_error[1]))

        else: 
            return FSharpResult_2(0, match_value.fields[0])

Due to json: Any = json_1.loads(value) we must alias import json or we get:

Traceback (most recent call last):
  File "C:\Users\Kevin\source\repos\Fable\src\quicktest-py\quicktest.py", line 83, in <module>
    main(sys.argv[1:])
  File "C:\Users\Kevin\source\repos\Fable\src\quicktest-py\quicktest.py", line 73, in main
    result: FSharpResult_2[dict[str, Any], str] = Decode_fromString(decoder, content)
  File "C:\Users\Kevin\source\repos\Fable\src\quicktest-py\fable_modules\thoth_json_python\decode.py", line 99, in Decode_fromString
    raise match_value_1
  File "C:\Users\Kevin\source\repos\Fable\src\quicktest-py\fable_modules\thoth_json_python\decode.py", line 79, in Decode_fromString
    json: Any = json.loads(value)
UnboundLocalError: local variable 'json' referenced before assignment

If we go one version higher fable-5.0.0-alpha.10 we only get import json without alias.

@Freymaurer Freymaurer changed the title Python Fable-alpha.10 does not alias imports introducing "Reassignment" errors Python 5.0.0-alpha.10 does not alias imports introducing "Reassignment" errors Mar 25, 2025
@Freymaurer
Copy link
Contributor Author

Can be reproduced by adding the following line to python quicktest:

let json = Fable.Python.Json.json.loads @"{""name"":""Alice"",""age"":30}"

@Freymaurer
Copy link
Contributor Author

My proposed fix clashed with one of the tests:

[<Fact>]
let ``test importSideEffects`` () = // See #3965
    importSideEffects "./native_code.py"
    let mutable x = 3
    Py.python $"""
    {x} = native_code.add5({x} + 2)
    """
    x |> equal 10
def test_import_side_effects(__unit: None=None) -> None:
    x: int = 3
    
    x = native_code.add5(x + 2)
    
    Testing_equal(10, x)

But because of my changes the import was aliased to: import tests.Python.native_code as native_code_1.

The referenced issue is in my opinion solved by the test below:

[<Fact>]
let ``test importAll`` () =
    let nativeCode: NativeCode = importAll "./native_code.py"
    3 |> nativeCode.add5 |> equal 8

Which works as expected. I am not sure if the test with raw python based on a fable import should work. I will open a PR, feel free to tell me otherwise.

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

Successfully merging a pull request may close this issue.

1 participant