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

Can't convert a subtype of a base Python type to a custom Julia type #751

Closed
sethaxen opened this issue Feb 28, 2020 · 0 comments · Fixed by #792
Closed

Can't convert a subtype of a base Python type to a custom Julia type #751

sethaxen opened this issue Feb 28, 2020 · 0 comments · Fixed by #792

Comments

@sethaxen
Copy link
Contributor

When a Python package extends a basic type like a dict to add some functionality, we can't use pytype_mapping to convert it to a custom Julia type. Instead, the default conversion (in this case to Dict) takes precedent.

Example:

using PyCall
import Base: convert

py"""
class MyDict(dict):
   pass
x = MyDict({"a": "b", "c": "d"})
"""

struct MyDictWrapper
   o::PyObject
end

Base.convert(::Type{MyDictWrapper}, obj::PyObject) = MyDictWrapper(obj)

julia> pytype_mapping(py"MyDict", MyDictWrapper)
1-element Array{Tuple{PyObject,Type},1}:
 (PyObject <class '__main__.MyDict'>, MyDictWrapper)

julia> py"x" # expected: MyDictWrapper(PyObject {'a': 'b', 'c': 'd'})
Dict{Any,Any} with 2 entries:
  "c" => "d"
  "a" => "b"
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