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

viewing Julia docstrings from Python #468

Open
ajwheeler opened this issue Mar 6, 2024 · 6 comments
Open

viewing Julia docstrings from Python #468

ajwheeler opened this issue Mar 6, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@ajwheeler
Copy link

Is your feature request related to a problem? Please describe.
I have a julia package that most users use through juliacall. Ideally, they would be able to pull up my docstrings in jupyter notebooks, IDEs, etc.

Describe the solution you'd like
Ideally, the python docstrings of julia functions would be set when they are called through juliacall. I don't know enough about the internals to have an understanding of how difficult this would be, but I have some free effort to help if it's something a non-intiate can do.

Describe alternatives you've considered
I've considered wrapping my package in a custom python package that uses juliacall internally, but that seems like an inelegant solution.

@ajwheeler ajwheeler added the enhancement New feature or request label Mar 6, 2024
@cjdoris
Copy link
Collaborator

cjdoris commented Mar 7, 2024

Unfortunately this isn't possible. help(x) gets the docstring from type(x).__doc__, and since all Julia objects are wrapped with the same Python type, they all must have the same docstring.

However you can do x._jl_help() to display the Julia docstring for x.

@ajwheeler
Copy link
Author

_jl_help() doesn't seem to work for me. Do you know what's going on here?

In [1]: from juliacall import Main as jl

In [2]: jl.sum
Out[2]: sum (generic function with 10 methods)

In [3]: jl.sum._jl_help()
---------------------------------------------------------------------------
JuliaError                                Traceback (most recent call last)
Cell In[3], line 1
----> 1 jl.sum._jl_help()

File ~/.julia/packages/PythonCall/wXfah/src/jlwrap/any.jl:309, in _jl_help(self, mime)
    307     return self._jl_callmethod($(pyjl_methodnum(pyjlany_display)), mime)
    308 def _jl_help(self, mime=None):
--> 309     return self._jl_callmethod($(pyjl_methodnum(pyjlany_help)), mime)
    310 def _repr_mimebundle_(self, include=None, exclude=None):
    311     return self._jl_callmethod($(pyjl_methodnum(pyjlany_mimebundle)), include, exclude)

JuliaError: MethodError: no method matching mode(::Nothing)

Closest candidates are:
  mode(!Matched::REPL.LineEdit.PrefixSearchState)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2054
  mode(!Matched::REPL.LineEdit.SearchState)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2053
  mode(!Matched::REPL.LineEdit.PromptState)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/LineEdit.jl:2052
  ...

Stacktrace:
 [1] (::REPL.var"#55#56"{REPL.REPLDisplay{REPL.LineEditREPL}, MIME{Symbol("text/plain")}, Base.RefValue{Any}})(io::Any)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:263
 [2] with_repl_linfo(f::Any, repl::REPL.LineEditREPL)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:569
 [3] display(d::REPL.REPLDisplay, mime::MIME{Symbol("text/plain")}, x::Any)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:259
 [4] display(d::REPL.REPLDisplay, x::Any)
   @ REPL ~/.julia/juliaup/julia-1.10.1+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/REPL/src/REPL.jl:278
 [5] display(x::Any)
   @ Base.Multimedia ./multimedia.jl:340
 [6] pyjlany_help(self::Function, mime_::Py)
   @ PythonCall ~/.julia/packages/PythonCall/wXfah/src/jlwrap/any.jl:149
 [7] _pyjl_callmethod(f::Any, self_::Ptr{PythonCall.C.PyObject}, args_::Ptr{PythonCall.C.PyObject}, nargs::Int64)
   @ PythonCall ~/.julia/packages/PythonCall/wXfah/src/jlwrap/base.jl:62
 [8] _pyjl_callmethod(o::Ptr{PythonCall.C.PyObject}, args::Ptr{PythonCall.C.PyObject})
   @ PythonCall.C ~/.julia/packages/PythonCall/wXfah/src/cpython/jlwrap.jl:47

jl.sum._jl_help(mime="text/plain") fails the same way. Apologies if this is a silly error.

@cjdoris
Copy link
Collaborator

cjdoris commented Mar 14, 2024

No that looks like a bug. Will take a look.

@cjdoris
Copy link
Collaborator

cjdoris commented Mar 14, 2024

Bug fixed - new release coming out shortly.

@ajwheeler
Copy link
Author

Fantastic!

Follow up question: what do you think about defining juliacall.jlhelp (or similar)? If I could just tell people to put

from juliacall import jlhelp

at the top of their notebooks it would solve 90% of the problem.

@cjdoris
Copy link
Collaborator

cjdoris commented Mar 15, 2024

Yeah that could be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants