JuliaPy / JuliaPy/PythonCall.jl

Add an option for more aggressive conversion

Đang mở
#355 2 bình luận 0 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Julia
Star
1.1k
Fork
86
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
3

Mô tả

**Is your feature request related to a problem? Please describe.**
I have users that are passing in python objects of unknown type and Julia code that expects native Julia objects. When trying to migrate from PyCall to PythonCall, I find the conversions defined here do not convert user provided objects into the most native equivalent Julia type.

**Describe the solution you'd like**
I would like an option to convert an arbitrary Python object to a Julia object with eager semantics. For example,
```julia
julia> eager_pyconvert(Any, pyeval("[1.0, 2.0, 3.0]", Main))
3-element Vector{Float64}:
1.0
2.0
3.0

julia> pyconvert(Any, pyeval("[1.0, 2.0, 3.0]", Main)) # Current (if type is unknown)
3-element PyList{Any}:
1.0
2.0
3.0

julia> pyconvert(Array, pyeval("[1.0, 2.0, 3.0]", Main)) # Current (if type is known)
3-element Vector{Float64}:
1.0
2.0
3.0

julia> py"[1.0, 2.0, 3.0]" # PyCall
3-element Vector{Float64}:
1.0
2.0
3.0
```
By "eager semantics" I mean it will convert to as specific a target type as possible. Or, equivalently, opt out of types defined by PythonCall.jl.

**Describe alternatives you've considered**
Coming up with a "magic type" that can be passed to the existing `pyconvert` function
```julia
types(m) = (x for x in (getglobal(m, n) for n in names(m)) if x isa Type && x !== Any)
BaseTypes = BaseTypes = Union{types(Core)..., types(Base)...}
pyconvert(BaseTypes, pyeval("[1.0, 2.0, 3.0]", Main))
```
Fails because `PyList <: BaseTypes`.

Restricting to concrete types segfaults julia
```julia
types(m) = (x for x in (getglobal(m, n) for n in names(m)) if x isa Type && !isabstracttype(x))
BaseTypes = Union{types(Core)..., types(Base)...}
pyconvert(BaseTypes, pyeval("[1.0, 2.0, 3.0]", Main))
# [68187] signal (11.2): Segmentation fault: 11
# in expression starting at REPL[41]:1
# ...
```

Home rolling conversion rules for specific types feels like reinventing the wheel and hard to maintain.

**Additional context**
Basically, I'd like to opt into the conversion behavior of PyCall.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

No files or tests are named. Start by reading the existing pyconvert conversion definitions and the public conversion API, then compare their behavior with the eager examples in the issue. Done means an option or API for eager conversion handles unknown Python objects as native Julia values without requiring caller-defined conversion rules.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
julia, python
Lĩnh vực
backend-api-design
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.