-
Notifications
You must be signed in to change notification settings - Fork 56
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
Fix Numba decorated function docstrings #173
Conversation
Codecov Report
@@ Coverage Diff @@
## master #173 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 14 14
Lines 1002 1002
=========================================
Hits 1002 1002
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome, thanks for fixing this @thisac!
Since this requires that Numba not be mocked out, one thing we'll have to double check is if Numba is installable on readthedocs. I've created a hidden build of this branch here: https://hafnian.readthedocs.io/en/numba_fix_decorator, and we can track builds here: https://readthedocs.org/projects/hafnian/builds/
docs/conf.py
Outdated
MOCK_MODULES = [ | ||
'numpy', | ||
'numpy.dtype', | ||
# 'numpy', | ||
# 'numpy.dtype', | ||
'scipy', | ||
'scipy.special', | ||
'scipy.optimize', | ||
'scipy.stats', | ||
'cython', | ||
'thewalrus.libwalrus', | ||
'numba', | ||
'numba.jit' | ||
# 'numba', | ||
# 'numba.jit' | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were originally several reasons why these modules are mocked out:
-
There is a bug in Sphinx (it might be fixed now) where Sphinx-autodoc would mistakenly include NumPy function documentation in projects that simply import it. This happened in Strawberry Fields, hence why it is probably mocked out here. I think it's okay to un-mock it now.
-
Mocking allows Sphinx to import the project and inspect docstrings without requiring the dependencies be installed. This is quite important for
'thewalrus.libwalrus'
, as we don't want to have to build the C++ code on the RTDs environment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've noticed that scipy
and numpy
are now in the doc/requirements.txt
, so are being installed on read the docs. So maybe SciPy doesn't have to be mocked any more either.
docs/conf.py
Outdated
def process_numba_signature(app, what, name, obj, options, signature, return_annotation): | ||
if isinstance(obj, numba.targets.registry.CPUDispatcher): | ||
original = obj.py_func | ||
orig_sig = inspect.signature(original) | ||
|
||
if (orig_sig.return_annotation) is inspect._empty: | ||
ret_ann = None | ||
else: | ||
ret_ann = orig_sig.return_annotation.__name__ | ||
|
||
return (str(orig_sig), ret_ann) | ||
|
||
return (signature, return_annotation) | ||
|
||
|
||
def setup(app): | ||
app.connect('autodoc-process-signature', process_numba_signature) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome that this works!
Looks like llvmlite disables installation on readthedocs environments :( |
@thisac: I updated the But RTDs then runs into a different error:
See the full build log here: https://readthedocs.org/projects/hafnian/builds/11086294/ Edit: could something have changed in the latest Numba release? What version of Numba do you have installed locally? |
…nto fix_numba_decorator
Thanks @josh146. I've fixed the |
hey @thisac : This is looking pretty good. There is one minor possibly related issue and is that the modules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @thisac, and nice fix! Note that you will need to add scipy
to the list of documentation requirements so that it will build on read the docs (you can check the build console here: https://readthedocs.org/projects/hafnian/builds/).
Once the fix_numba_decorator
is passing, feel free to merge this in 💯
docs/conf.py
Outdated
# 'numpy', | ||
# 'numpy.dtype', | ||
# 'scipy', | ||
# 'scipy.special', | ||
# 'scipy.optimize', | ||
# 'scipy.stats', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Safe to delete all of these :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Co-authored-by: Josh Izaac <josh146@gmail.com>
Context:
The generated docstrings for functions/methods decorated with Numba's
@jit
decorator didn't render properly since:The decorator obscured the signature to Sphinx.
numba
andnumba.jit
were mocked out inconf.py
causingmagicmock
to provide its docstring instead ofnumba
(which actually provides the correct, original docstring).Description of the Change:
An
autodoc-signature-process
function was added toconf.py
that checks whether the function is a Numba object, and in that case retrieves the original signature and forwards it to Sphinx.The mocked out functions/modules
numba
andnumba.jit
are removed fromconf.py
, letting the correct docstrings be rendered.The mocked out functions/modules
numpy
andnumpy.dtype
are removed fromconf.py
, letting the correct dtype be rendered inside the signatures.Also fixed a few docstrings that weren't rendered correctly due to missing line-breaks.
Benefits:
The docstrings and signatures now render as they should.
Possible Drawbacks:
I'm not sure exactly why
numpy
andnumba
were mocked out inconf.py
(nor the other funtions/method), so I might have broken something on the way. The documentation rendered nicely locally though.Related GitHub Issues:
N/A