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

DOC: -np.inf as default argument not rendering correctly in DIRECT docstring #16385

Closed
dschmitz89 opened this issue Jun 10, 2022 · 19 comments
Closed
Labels
upstream bug Items related to bugs in upstream projects Website Items related to the website; please also check https://github.com/scipy/scipy.org

Comments

@dschmitz89
Copy link
Contributor

There is a minor annoyance in the docstring of the recently added direct algorithm:
image

sphinx cannot correctly parse -np.inf . I tried to google the issue but could not find an immediate fix. Maybe a more experienced sphinx user has an idea?

@dschmitz89 dschmitz89 changed the title DOC: -np.inf as default argument not rendering correctly in docstring DOC: -np.inf as default argument not rendering correctly in DIRECT docstring Jun 10, 2022
@WarrenWeckesser
Copy link
Member

Other occurrences of the problem with -np.inf:

It looks like there are two issues here:

Maybe the numpydoc devs have seen this before: @jarrodmillman, @larsoner, @rossbar, is this a known issue?

@WarrenWeckesser
Copy link
Member

Another example: the spurious space shows up in the rendering of the na_sentinel default value in the Pandas docs for pandas.factorize.

@WarrenWeckesser WarrenWeckesser added Website Items related to the website; please also check https://github.com/scipy/scipy.org upstream bug Items related to bugs in upstream projects labels Jun 12, 2022
@WarrenWeckesser
Copy link
Member

I added the "upstream bug" tag. This is probably a bug in Sphinx itself or in the theme used by SciPy, NumPy and Pandas.

@tupui
Copy link
Member

tupui commented Jun 13, 2022

If there is nothing we can/should do, then we should close it IMO.

@WarrenWeckesser
Copy link
Member

It might be better to leave it open until the cause of the problem has been identified, and we know which version of which upstream tool fixes the problem.

@dschmitz89
Copy link
Contributor Author

Other occurrences of the problem with -np.inf:

It looks like there are two issues here:

Would adding autodoc_preserve_defaults = True to conf.py be enough to fix this?

@WarrenWeckesser
Copy link
Member

WarrenWeckesser commented Jun 14, 2022

@dschmitz89, it turns out that the problem is a bug in Sphinx: sphinx-doc/sphinx#10550

The proposed fix is in sphinx-doc/sphinx#10551

I'll close this issue when that Sphinx pull request is merged, because then we'll have a definitive resolution of the issue: the problem is fixed in Sphinx 5.1.0.

Using autodoc_preserve_defaults might, in fact, fix the issue reported here by avoiding the sphinx bug altogether, but I haven't tried it. I think the use of that option would require some discussion and some trial runs to see how it affects the rendered default values throughout the SciPy docs.

@dschmitz89
Copy link
Contributor Author

dschmitz89 commented Jun 15, 2022

@WarrenWeckesser : from what I understood, the Sphinx bug will fix the additional space but we would still see -inf instead of -np.inf. Changing sphinx config can be painful, I agree, maybe @tupui or another seasoned scipy doc expert has an opinion.

@tupui
Copy link
Member

tupui commented Jun 15, 2022

I would be fine with seeing -inf instead of -np.inf. There is no such thing in Python's grammar to confuse it with.

@AA-Turner
Copy link

AA-Turner commented Jun 15, 2022

autodoc_preserve_defaults wouldn't work as it still uses the AST, example reproducer script below.

import shutil
from pathlib import Path

from sphinx.cmd.make_mode import run_make_mode

def write(filename, text): Path(filename).write_text(text, encoding="utf-8")

write("conf.py", '''\
import os, sys
sys.path.insert(0, os.path.abspath(".."))
extensions = ["sphinx.ext.autodoc"]
autodoc_preserve_defaults = True
''')

write("extra_white.py", '''\
def func(axis=-0x1): ...
''')

write("index.rst", '''\
.. autofunction:: extra_white.func
''')

shutil.rmtree("_build", ignore_errors=True)
run_make_mode(["html", ".", "_build", "-T", "-W"])

A

@WarrenWeckesser
Copy link
Member

Thanks @AA-Turner, that makes sense. So if we used that option in SciPy now we would get f_min=- np.inf in the rendered direct docstring.

@tupui
Copy link
Member

tupui commented Jun 15, 2022

I confirmed the spacing issue will be resolved with Sphinx 5.1.0 (see sphinx-doc/sphinx#10551 (comment)).

@dschmitz89
Copy link
Contributor Author

-inf instead of -np.inf might not be too bad but what if a default argument is some complex class like scipy.stats.rv_continuous ?

@tupui
Copy link
Member

tupui commented Jun 15, 2022

-inf instead of -np.inf might not be too bad but what if a default argument is some complex class like scipy.stats.rv_continuous ?

Well this should not really happen as it would be a bad design. Default arguments should always be simple things like string, scalars, booleans, None. Anything else would break (e.g. using an empty dict which leads to bugs) and is an anti pattern.

@WarrenWeckesser
Copy link
Member

Over in the Sphinx repo, sphinx-doc/sphinx#10551 has been merged and the fix for the extra space will be in Sphinx 5.1.0.

I'm closing this issue, because I think the extra space was the main problem. Of course, our web site won't reflect this change until we build it with Sphinx 5.1, so if anyone is even more conservative about closing this than I am and thinks it should remain open until our web site is actually fixed, feel free to reopen the issue.

I think the question of enabling autodoc_preserve_defaults should be discussed in a new issue.

@tupui
Copy link
Member

tupui commented Jun 16, 2022

I am even less conservative so 😉
But the next dev on master will have it, and also 1.9 since we don't have upper pins for Sphinx. So all good.

What we might want to discuss on the ML is the other issue/enhancement.

@WarrenWeckesser
Copy link
Member

But the next dev on master will have it, and also 1.9 since we don't have upper pins for Sphinx. So all good.

The fix will be in Sphinx 5.1.0, which is not released yet. It looks like Sphinx has a release cadence of about two months, and 5.0.0 was released May 29, so I wouldn't expect 5.1.0 to be released until the end of July.

@WarrenWeckesser
Copy link
Member

Status update: It looks like the 1.9.0 docs were built with Sphinx <5.1.0, so they still have the spurious space, e.g. https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.direct.html.

A newer version of Sphinx is used to build the development version of the docs, so in those the problem is fixed: https://scipy.github.io/devdocs/reference/generated/scipy.optimize.direct.html

@tupui
Copy link
Member

tupui commented Aug 8, 2022

Maybe we should just bump the minimal version for Sphinx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream bug Items related to bugs in upstream projects Website Items related to the website; please also check https://github.com/scipy/scipy.org
Projects
None yet
Development

No branches or pull requests

4 participants