I’m attempting to call a Python function from Julia which returns a dictionary with known types. I’d like to efficiently get a Julia dictionary with these correct types.
It seems like there should be a way to provide the expected return types, although I may be misinterpreting this section of the readme:
By default, a
PyDict
is anAny => Any
dictionary (or actuallyPyAny => PyAny
) that performs runtime type inference, but if your Python dictionary has known, fixed types you can instead usePyDict{K,V}
given the key and value typesK
andV
respectively.
julia> using PyCall
julia> d = py"{'foo': 123, 'bar': 456}"
Dict{Any, Any} with 2 entries:
"bar" => 456
"foo" => 123
julia> d = py"{'foo': 123, 'bar': 456}" :: Dict{String, Int64}
ERROR: TypeError: in typeassert, expected Dict{String, Int64}, got a value of type Dict{Any, Any}
Stacktrace:
[1] top-level scope
@ ~/.julia/packages/PyCall/L0fLP/src/pyeval.jl:232
julia> d = py"{'foo': 123, 'bar': 456}" :: PyDict{String, Int64}
ERROR: TypeError: in typeassert, expected PyDict{String, Int64, isdict} where isdict, got a value of type Dict{Any, Any}
Stacktrace:
[1] top-level scope
@ ~/.julia/packages/PyCall/L0fLP/src/pyeval.jl:232
Versions:
PyCall v1.93.0
Julia 1.6.4