JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.45k stars 186 forks source link

pytype_mapping confusion #1049

Closed ejmeitz closed 10 months ago

ejmeitz commented 10 months ago

Hello,

I'm trying to use the pytype_mapping function to get automatic conversion from a PyObject type to one of my wrapper types in Julia. My code is below:

vis= pyimport("ovito.vis")

struct OpenGLRenderer <: Renderer
    o::PyObject

    function OpenGLRenderer(antialiasing_level)
        return new(vis.OpenGLRenderer(antialiasing_level = antialiasing_level))
    end
end

pytype_mapping(vis."OpenGLRenderer", OpenGLRenderer)

renderer = vis.OpenGLRenderer()

The typeof renderer is just PyObject still how do I make this automatically recognize as my Julia type? I'm a little confused as to what the correct pattern is supposed to be here. If I call pytype_query on renderer PyCall sees that renderer could be my OpenGLRenderer Julia type but that conversion is not happening automatically.

On the flip side, I also want the user to be able to construct their own OpenGLRenderer without having to call the python library. My constructor above works for that but it required me to manually add the antialiasing_level. Is the only way around this to use kwargs and args?