JuliaPy / JuliaPy/PyCall.jl

Can't automatically convert KeyedTuple (from sqlalchemy) to Julia's NamedTuple

Open
#700 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
1.5k
Forks
186
PR merge metrics
No merged PRs in 30d

Description

I use PyCall to call sqlalchemy from Julia, which uses a class called `KeyedTuple` to represent rows resulting from a query and I want to convert those automatically to julia's `NamedTuple`. I expected something like this to work:

```julia
using PyCall

sa = pyimport("sqlalchemy")

pytype_mapping(sa."util"."KeyedTuple", NamedTuple)

Base.convert(::Type{NamedTuple}, o::PyObject) = NamedTuple{Symbol.(o._fields)}([getproperty(o, f) for f in Symbol.(o._fields)])

x = sa.util.KeyedTuple([1, 2, 3], labels=["one", "two", "three"]) # returns (1, 2, 3)

```

This is no surprise, since:

```julia
o = @pycall sa.util.KeyedTuple([1, 2, 3], labels=["one", "two", "three"])::PyObject

pytype_query(o) # Returns `Tuple{Integer,Integer,Integer}`
```

I guess a simple way to make my thing work would be to alter the order in which `pytype_query` does things> I mean first look for a match in the `pytype_queries` array and then proceed with the rest...?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.