Skip to content

Add sphinx-codeautolink extension to docs build #7011

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

TomNicholas
Copy link
Member

@TomNicholas TomNicholas commented Sep 8, 2022

I think that sphinx-codeautolink is different from sphinx.ext.linkcode...

@github-actions github-actions bot added CI Continuous Integration tools dependencies Pull requests that update a dependency file labels Sep 8, 2022
@TomNicholas
Copy link
Member Author

I don't understand why this failed - it works when I build the docs locally, but the RTD build doesn't give me a useful error message...

@keewis
Copy link
Collaborator

keewis commented Sep 8, 2022

did you compare the versions? If it works locally, maybe you have a different version of sphinx or sphinx-codeautolink (or any other dependency?)

@dcherian
Copy link
Contributor

dcherian commented Nov 4, 2022

There are a lot of these warnings:

/home/docs/checkouts/readthedocs.org/user_builds/xray/checkouts/7011/doc/whats-new.rst:1: WARNING: invalid syntax (<unknown>, line 6) in document "whats-new"
Parsed source in `ipython` block:
block source: In [66]: ds = xray.Dataset({"x": np.arange(1000)})
              
              In [67]: with xray.set_options(display_width=40):
                 ....:     print(ds)
                 ....: 
              <xarray.Dataset>
              Dimensions:  (x: 1000)
              Coordinates:
                * x        (x) int64 0 1 2 ... 998 999
              Data variables:
                  *empty*

@felix-hilden
Copy link

Hi, it looks like the print output is recognised as Python code and parsed. This shouldn't be happening since your what's-new correctly contains the Out [...]: prefixes which we use to ignore lines for parsing (although I couldn't find the specific case you presented). I'll test the multiline outputs and provide an update to you.

@felix-hilden
Copy link

Yep, a weird interaction between rST processing and the IPython transformer. This class of issues fixed in sphinx-codeautolink==0.12.1!

@dcherian
Copy link
Contributor

dcherian commented Dec 2, 2022

@felix-hilden Looks like we still have the same issue with

sphinx-codeautolink       0.12.1                   pypi_0    pypi

@dcherian
Copy link
Contributor

dcherian commented Jan 15, 2023

We now have a different error where a error message is being treated as code:

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/block.py", line 204, in parse_source
    names = parse_names(modified_source, node)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/parse.py", line 20, in parse_names
    tree = ast.parse(source)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 638
    ValueError: arguments without labels along dimension 'x' cannot be aligned because they have different dimension size(s) {2} than the size of the aligned dimension labels: 3
                          ^
SyntaxError: invalid syntax

@felix-hilden
Copy link

Oh yikes, that should also be fixed now! I hope the parsing logic is finally sound.

@dcherian
Copy link
Contributor

Ah now a different one

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/block.py", line 213, in parse_source
    names = parse_names(modified_source, node)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/parse.py", line 20, in parse_names
    tree = ast.parse(source)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 638
    Out[125]: 
              ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/__init__.py", line 43, in wrapper
    return func(*args, **kwargs)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/__init__.py", line 135, in parse_blocks
    doctree.walkabout(visitor)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/docutils/nodes.py", line 227, in walkabout
    if child.walkabout(visitor):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/docutils/nodes.py", line 227, in walkabout
    if child.walkabout(visitor):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/docutils/nodes.py", line 227, in walkabout
    if child.walkabout(visitor):
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/docutils/nodes.py", line 219, in walkabout
    visitor.dispatch_visit(self)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/docutils/nodes.py", line 2021, in dispatch_visit
    return method(node)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/block.py", line 166, in visit_literal_block
    return self.parse_source(node, node.get("language", None))
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/block.py", line 215, in parse_source
    show_source = self._format_source_for_error(source, prefaces)
  File "/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/7011/lib/python3.9/site-packages/sphinx_codeautolink/extension/block.py", line 246, in _format_source_for_error
    guides[ix] = "block source:"
IndexError: list assignment index out of range

Thanks for be patient with us!

@felix-hilden
Copy link

That actually might be something for you to change. See the issue and in particular this IPython doc on the lexer implementation. It states that blocks not starting with a console prompt should be considered ordinary (IPython) code. So In/Out won't be expected. Now maybe comments are or should be an exception, but I suspect by moving the comment elsewhere you might get it to pass!

If you refuse, I'll have a look at IPython's implementation and how they handle comments 😄

@dcherian
Copy link
Contributor

dcherian commented Jan 16, 2023

It's the second code block here and the source is

.. ipython:: python

    # only one argument has the 'x' coordinate
    arr[0] + 1
    # both arguments have the same 'x' coordinate
    arr[0] - arr[0]

It's rendering properly so it seems like this should just work.

So In/Out won't be expected.

The source block doesn't have In/Out but I guess the extension is receiving the output after IPython has executed the block?

I tried moving the comments around which seems to work, but we do this in a lot of places. Is it easy for you to handle it?

@felix-hilden
Copy link

Yep we'll definitely follow what IPython does, now available in sphinx-codeautolink==0.13.2!

@felix-hilden
Copy link

Wow, it's green! I still see some matching warnings though 🤔 I can have a closer look later if you care about them.

@dcherian
Copy link
Contributor

dcherian commented Jan 26, 2023

It works! so on to the next issue :(

https://xray--7011.org.readthedocs.build/en/7011/user-guide/data-structures.html# has no links in the first cell.

Do you execute ipython cells that are "suppressed" (see https://github.com/pydata/xarray/blob/main/doc/user-guide/data-structures.rst). I think we "suppress" cells with imports at the top of all our doc pages.

Thanks again for all your work!

@dcherian dcherian marked this pull request as draft January 26, 2023 19:58
@felix-hilden
Copy link

felix-hilden commented Jan 28, 2023

I'm not familiar with how :suppress: works, other than the fact that the documentation states that warnings and errors are ignored. But I see that the whole block is invisible, so maybe it is executed only internally by IPython and all output is suppressed. If this is the case, consider adding something like:

.. autolink-preface::

    import numpy as np
    import pandas as pd
    import xarray as xr
    ...

or you can also set a global preface in conf.py!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous Integration tools dependencies Pull requests that update a dependency file topic-documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use sphinx-codeautolink in docs?
4 participants