Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Enable copy-pasting working example code from numpy style docstrings #6699

Closed
MarcSkovMadsen opened this issue Nov 23, 2024 · 6 comments
Closed
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@MarcSkovMadsen
Copy link

MarcSkovMadsen commented Nov 23, 2024

I would like to be able to copy paste working example code from numpy style docstrings. Currently I have to remove >>> and ... my self.

vs-code.mp4

For context I'm currently contributing improved docstrings to HoloViz Param and we are discussing how we can best include useful example code in the docstrings. Join the discussion holoviz/param#977 (comment).

The problem is would like our example code to be useful in VS Code with Pylance, VS Code with jedi-language-server, PyCharm, Sphinx docs, Jupyter Notebook and many more places.

@MarcSkovMadsen
Copy link
Author

It seems jedi-language-server handles this perfectly. Both when displayed and copy-pasted.

vscode-jedi-language-server.mp4

@rchiodo
Copy link
Contributor

rchiodo commented Nov 25, 2024

Jedi is removing the >>> that's in the docstring. I think we'd only do that if Sphynx did it too.

This block here:
https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#doctest-blocks

@rchiodo
Copy link
Contributor

rchiodo commented Nov 25, 2024

An alternative would be to use code blocks. Then the >>> isn't necessary to get the code to format like code:

def foo_with_doctest():
    """
    Some example code:

    >>> print(1)
    >>> print(2)

    """

Gives you this:

Image

While this docstring gives you this:

def foo_with_codeblock():
    """
    Some example code:

    .. code-block:: python

        print(1)
        print(2)
    """

Image

@MarcSkovMadsen
Copy link
Author

MarcSkovMadsen commented Nov 25, 2024

Thx. We are using numpy style docstrings. One of the 4 big styles in Python. Its docstrings use >>> for examples https://numpydoc.readthedocs.io/en/latest/format.html#examples. Thus it does not make sense to use other types of code blocks without switching everything else away from numpy style. And thats a lot of work for a big ecosystem like HoloViz.

@maximlt
Copy link

maximlt commented Nov 25, 2024

Jedi is removing the >>> that's in the docstring. I think we'd only do that if Sphynx did it too.

This block here: https://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html#doctest-blocks

Selecting code in a code block on Python's website (built with Sphinx) doesn't include the prompts.

Image

There's also an option to hide the prompts and outputs.

Image

The sphinx-copybutton extension has a lot of knobs one can tweak, for instance, to exclude prompts for copies, which imho is what most people want (you can actually copy/paste code with these prompts in an IPython shell but that looks more like a workaround :) ). See more info on their site https://sphinx-copybutton.readthedocs.io/en/latest/use.html.


The good news is that there are many ways VSCode would enhance the UX of these docstrings code blocks :) !

@rchiodo
Copy link
Contributor

rchiodo commented Nov 25, 2024

If we want to still return the >>> in the output, VS code would need a way for us to override what happens on copy (like how the Python website does it), but I'm not sure that's possible. At least not that I could find in 'vscode.d.ts'

The other alternative would be to eliminate the >>> in the returned text. I don't think we'd want to do that for everybody though, so we'd need a custom setting to turn them off.

Transferring to a discussion item for up votes.

@microsoft microsoft locked and limited conversation to collaborators Nov 25, 2024
@rchiodo rchiodo converted this issue into discussion #6705 Nov 25, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants