JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
720 stars 61 forks source link

How to bypass automatic conversion to Float64? #396

Open LilithHafner opened 8 months ago

LilithHafner commented 8 months ago

I want to get my hands on a Tuple{Float32, Float32} in Python and there doesn't seem to be a good way of doing it

>>> jl("typeof")(jl("Tuple{Float32, Float32}")((0.0, 1.0)))
Julia: Tuple{Float64, Float64}
cjdoris commented 8 months ago

Short answer: juliacall.convert.

Long answer: I got conversion right in PythonCall but wrong in JuliaCall.

In your example, you start with a python tuple of floats, which is converted to a Tuple{Float64,Float64} in Julia, which you then convert to a Tuple{Float32, Float32} in Julia, pass back to Python which converts it to a Python tuple of floats, which you then pass back to Julia where it becomes a Tuple{Float64,Float64}, hence the reported type.

In v1 I'm going to prevent this automatic conversion when passing values from Julia to Python, which will make code like this much simpler and predictable. You'll always get a Jl back, just like in PythonCall you always get a Py back.