Skip to content

NamedTuple #213

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

Closed
philippeller opened this issue Aug 25, 2022 · 2 comments
Closed

NamedTuple #213

philippeller opened this issue Aug 25, 2022 · 2 comments

Comments

@philippeller
Copy link

I cannot convert a python namedtuple to a julia NamedTuple. Here a minimal example:

from juliacall import Main as jl
from collections import namedtuple

t = namedtuple('test', 'a b')
x = t(a=1, b=2)
jl.convert(jl.NamedTuple, x)

which results in the Error:

TypeError                                 Traceback (most recent call last)
<ipython-input-245-c2a0b5ab7412> in <module>
      3 x = t(a=1, b=2)
      4 
----> 5 jl.convert(jl.NamedTuple, x)

~/anaconda3/lib/python3.8/site-packages/juliacall/__init__.py in __call__(self, *args, **kwargs)
    199             return ValueBase.__dir__(self) + self._jl_callmethod($(pyjl_methodnum(pyjlany_dir)))
    200         def __call__(self, *args, **kwargs):
--> 201             return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
    202         def __len__(self):
    203             return self._jl_callmethod($(pyjl_methodnum(pyjlany_op(length))))

TypeError: Julia: MethodError: Cannot `convert` an object of type 
  Tuple{Int64, Int64} to an object of type 
  NamedTuple
Closest candidates are:
  convert(::Type{NamedTuple}, !Matched::ShapedAsNT) at /home/peller/.julia/packages/ValueShapes/03j48/src/named_tuple_shape.jl:362
  convert(::Type{T}, !Matched::T) where T at essentials.jl:205
  NamedTuple(::Any) at namedtuple.jl:107```
@cjdoris
Copy link
Collaborator

cjdoris commented Sep 3, 2022

I have just added conversion from Python named tuples to Julia named tuples on the main branch, it will be in the next release.

BTW your code is wrong, you need to call juliacall.convert not jl.convert. The difference between these is that jl.convert is just a generic wrapper around Julia's Base.convert function, but such wrappers already automatically convert their inputs. Previously, a Python named tuple would have been converted to a Julia tuple, so jl.convert(jl.NamedTuple, x) would end up calling convert(NamedTuple, (1, 2)), which is not what you want. Instead, juliacall.convert(jl.NamedTuple, x) ends up calling pyconvert(NamedTuple, x), which is what you do want.

Python named tuples will now be converted to Julia named tuples by default (instead of tuples) so in the future you should not even need to do this conversion yourself in most situations.

@cjdoris cjdoris closed this as completed Sep 3, 2022
@philippeller
Copy link
Author

Awesome, thank you!

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

No branches or pull requests

2 participants