JuliaPy / PythonCall.jl

Python and Julia in harmony.
https://juliapy.github.io/PythonCall.jl/stable/
MIT License
715 stars 61 forks source link

datetime.timedelta conversions fail in juliacall #498

Closed takbal closed 1 month ago

takbal commented 1 month ago

Affects: JuliaCall

Describe the bug

https://juliapy.github.io/PythonCall.jl/stable/conversion-to-julia/ explicitly tells that datetime.timedelta conversion to Period types is supported.

I may do something wrong, but in my experience, it does not work when done on the Python side in juliacall:

import juliacall
import datetime
from juliacall import Main as jl
jl.seval("using Dates")

In [5]: juliacall.convert(jl.Second, datetime.timedelta(seconds=1))

results in:

JuliaError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 juliacall.convert(jl.Second, datetime.timedelta(seconds=1))

File ~/workspace/research-stack/.venv/lib/python3.10/site-packages/juliacall/__init__.py:22, in convert(T, x)
     20 if _convert is None:
     21     _convert = PythonCall.JlWrap.seval("pyjlcallback((T,x)->pyjl(pyconvert(pyjlvalue(T)::Type,x)))")
---> 22 return _convert(T, x)

File ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/callback.jl:55, in __call__(self, *args, **kwargs)
     53             return self._jl_callmethod($(pyjl_methodnum(pyjlcallback_str)))
     54     def __call__(self, *args, **kwargs):
---> 55         return self._jl_callmethod($(pyjl_methodnum(pyjlcallback_call)), args, kwargs)
     56 """, @__FILE__(), "exec"), jl.__dict__)
     57 pycopy!(pyjlcallbacktype, jl.CallbackValue)

JuliaError: cannot convert this Python 'timedelta' to a Julia 'Second'
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:357 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Core/Py.jl:132 [inlined]
 [4] pyconvert(::Type{Second}, x::Py)
   @ PythonCall.Convert ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:372
 [5] (::PythonCall.JlWrap.var"#23#24")(T::Py, x::Py)
   @ PythonCall.JlWrap ./none:1
 [6] pyjlcallback_call(self::PythonCall.JlWrap.var"#23#24", args_::Py, kwargs_::Py)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/callback.jl:19
 [7] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/base.jl:72
 [8] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
   @ PythonCall.JlWrap.Cjl ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/C.jl:63

and

In [6]: juliacall.convert(jl.Millisecond, datetime.timedelta(milliseconds=1))
---------------------------------------------------------------------------
JuliaError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 juliacall.convert(jl.Millisecond, datetime.timedelta(milliseconds=1))

File ~/workspace/research-stack/.venv/lib/python3.10/site-packages/juliacall/__init__.py:22, in convert(T, x)
     20 if _convert is None:
     21     _convert = PythonCall.JlWrap.seval("pyjlcallback((T,x)->pyjl(pyconvert(pyjlvalue(T)::Type,x)))")
---> 22 return _convert(T, x)

File ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/callback.jl:55, in __call__(self, *args, **kwargs)
     53             return self._jl_callmethod($(pyjl_methodnum(pyjlcallback_str)))
     54     def __call__(self, *args, **kwargs):
---> 55         return self._jl_callmethod($(pyjl_methodnum(pyjlcallback_call)), args, kwargs)
     56 """, @__FILE__(), "exec"), jl.__dict__)
     57 pycopy!(pyjlcallbacktype, jl.CallbackValue)

JuliaError: cannot convert this Python 'timedelta' to a Julia 'Millisecond'
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:357 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/PythonCall/S5MOg/src/Core/Py.jl:132 [inlined]
 [4] pyconvert(::Type{Millisecond}, x::Py)
   @ PythonCall.Convert ~/.julia/packages/PythonCall/S5MOg/src/Convert/pyconvert.jl:372
 [5] (::PythonCall.JlWrap.var"#23#24")(T::Py, x::Py)
   @ PythonCall.JlWrap ./none:1
 [6] pyjlcallback_call(self::PythonCall.JlWrap.var"#23#24", args_::Py, kwargs_::Py)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/callback.jl:19
 [7] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
   @ PythonCall.JlWrap ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/base.jl:72
 [8] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
   @ PythonCall.JlWrap.Cjl ~/.julia/packages/PythonCall/S5MOg/src/JlWrap/C.jl:63

Your system OS: ubuntu 22.04 LTS Julia version: 1.10.3 Python: 3.10.10 PythonCall: 0.9.20 juliacall: 0.9.20 pip list:

% pip list
Package                   Version         Editable project location
------------------------- --------------- ------------------------------------------
aiofiles                  0.8.0
aiohttp                   3.9.3
aiokafka                  0.8.0
aiosignal                 1.3.1
annotated-types           0.6.0
anyio                     3.7.1
appdirs                   1.4.4
astroid                   2.13.5
asttokens                 2.4.1
async-timeout             4.0.3
attrs                     23.2.0
Bottleneck                1.3.5
certifi                   2024.2.2
charset-normalizer        2.0.12
click                     8.1.7
cloudpickle               3.0.0
colorama                  0.4.6
colorful                  0.5.6
comm                      0.2.1
confluent-kafka           2.3.0
contourpy                 1.2.0
cramjam                   2.8.1
cycler                    0.12.1
debugpy                   1.8.0
decorator                 5.1.1
dill                      0.3.8
distro                    1.9.0
exceptiongroup            1.2.0
executing                 2.0.1
fastjsonschema            2.19.1
fastparquet               2022.11.0
flake8                    6.0.0
fonttools                 4.47.0
frozenlist                1.4.1
fsspec                    2024.2.0
grpcio                    1.59.0
grpcio-tools              1.59.0
h11                       0.14.0
httpcore                  1.0.4
httpx                     0.27.0
idna                      3.6
iniconfig                 2.0.0
ipykernel                 6.28.0
ipython                   8.19.0
isort                     5.13.2
jedi                      0.19.1
jsonschema                4.20.0
jsonschema-specifications 2023.12.1
julia                     0.6.2
juliacall                 0.9.20
juliapkg                  0.1.11
jupyter_client            8.6.0
jupyter_core              5.7.1
kafka-python              2.0.2
kiwisolver                1.4.5
lazy-object-proxy         1.10.0
loky                      3.4.1
matplotlib                3.8.2
matplotlib-inline         0.1.6
mccabe                    0.7.0
multidict                 6.0.5
mypy                      0.990
mypy-extensions           1.0.0
nbformat                  5.9.2
nest-asyncio              1.5.8
numexpr                   2.9.0
numpy                     1.26.3
openai                    1.3.4
orjson                    3.8.0
outcome                   1.3.0.post0
packaging                 23.2
pandas                    2.1.1
parso                     0.8.3
pathspec                  0.12.1
pexpect                   4.9.0
pillow                    10.2.0
pip                       23.3.1
platformdirs              4.1.0
plotly                    5.18.0
pluggy                    1.3.0
prettyprinter             0.18.0
prompt-toolkit            3.0.43
protobuf                  4.24.1
psutil                    5.9.7
ptyprocess                0.7.0
pure-eval                 0.2.2
pyarrow                   15.0.0
pycodestyle               2.10.0
pydantic                  2.6.4
pydantic_core             2.16.3
pydrill                   0.3.4
pyflakes                  3.0.1
Pygments                  2.17.2
pylint                    2.15.10
pymongo                   4.2.0
pymongoarrow              0.6.2
pyparsing                 3.1.1
PySocks                   1.7.1
pytest                    7.4.4
python-dateutil           2.8.2
python-decouple           3.6
python-logging-loki       0.3.1
python-snappy             0.6.1
pytz                      2023.3.post1
pyzmq                     25.1.2
redis                     5.0.1
referencing               0.32.1
regex                     2023.12.25
requests                  2.27.1
rfc3339                   6.2
rpds-py                   0.17.1
selenium                  4.15.2
semantic-version          2.10.0
setuptools                69.0.2
six                       1.16.0
sniffio                   1.3.1
sortedcontainers          2.4.0
stack-data                0.6.3
tables                    3.7.0
tardis-dev                2.0.0a13
tenacity                  8.2.3
toml                      0.10.2
tomli                     2.0.1
tomlkit                   0.12.4
tornado                   6.4
tqdm                      4.64.1
traitlets                 5.14.1
trio                      0.25.0
trio-websocket            0.11.1
types-beautifulsoup4      4.12.0.7
types-colorama            0.4.15.11
types-html5lib            1.1.11.20240228
types-jsonschema          4.16.1
types-orjson              3.6.2
types-python-dateutil     2.8.19
types-PyYAML              6.0.7
types-requests            2.31.0.6
types-setuptools          65.6.0.2
types-urllib3             1.26.25.4
typing_extensions         4.10.0
tzdata                    2023.4
urllib3                   1.26.12
wcwidth                   0.2.12
wheel                     0.42.0
wrapt                     1.16.0
wsproto                   1.2.0
yarl                      1.9.4

juliapkg.status():

In [2]: juliapkg.status()
JuliaPkg Status
/home/bt/workspace/myproject/.venv/julia_env/pyjuliapkg/juliapkg.json (empty project)
Julia 1.10.3 @ /home/bt/bin/julia
cjdoris commented 1 month ago

Yep it was documented but never implemented - fixed now.