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

Support for examples section #8

Closed
jaimergp opened this issue Mar 4, 2020 · 21 comments
Closed

Support for examples section #8

jaimergp opened this issue Mar 4, 2020 · 21 comments
Labels
docstrings Docstrings parsing docstrings-format:google feature New feature or request

Comments

@jaimergp
Copy link

jaimergp commented Mar 4, 2020

Hi! I would like to start by saying this project has made mkdocs usable for our team and we are SO excited about finally ditching RST! I cannot emphasize this enough: Thank. You.

The only thing we are missing is that, currently, mkdocstrings doesn't support an Examples section, which normally include doctest formatted blocks. For example:

def example_function(arg1, kwarg=None) -> object:
    """
    Example function to demonstrate how APIs are rendered

    Parameters:
        arg1 (dict): Some description for this argument.
            This type (in parenthesis) is ignored.
        kwarg: Some more descriptions

    Returns:
        A description for the returned value

    Examples:

        >>> 2 + 2 == 4
        False

    """
    pass

gets rendered as:

image

The style is not bad, but it could be even better! Notice how pytest is able to parse the block just fine.

I can get what I want by using this other docstring:

def example_function(arg1, kwarg=None) -> object:
    """
    Example function to demonstrate how APIs are rendered

    Parameters:
        arg1 (dict): Some description for this argument.
            This type (in parenthesis) is ignored.
        kwarg: Some more descriptions

    Returns:
        A description for the returned value

    __Examples__

    ```python
    >>> 2 + 2 == 4
    False

    ```
    """
    pass

image

, but I find it a bit annoying (in order of importance):

  1. Manually hardcoding the Examples heading style
  2. Having to specify the code fences
  3. Needing an extra blank line before the closing triple backticks so pytest can get the doctests correctly.

Maybe a good compromise would be to just add Examples to the recognized headers? That way I could write:

def example_function(arg1, kwarg=None) -> object:
    """
    Example function to demonstrate how APIs are rendered

    Parameters:
        arg1 (dict): Some description for this argument.
            This type (in parenthesis) is ignored.
        kwarg: Some more descriptions

    Returns:
        A description for the returned value

    Examples:

        ```python
        >>> 2 + 2 == 4
        False

        ```
    """
    pass

, which currently does not work as intended:

image

I'd be happy to help and contribute a PR if needed, but if I understood correctly, you are currently refactoring the backend and I don't know if this is the best moment to add a feature.

Thanks a lot!

@pawamoy
Copy link
Member

pawamoy commented Mar 4, 2020

Hi @jaimergp, thank you very much for the kind words and the very detailed issue! I'm glad that your team is enjoying this project 🙂

I totally agree that examples must be supported, and I think the preferred way would be the first you wrote, but it's open to discussion of course.

This feature request is one more thing in favor of implementing a proper "google-style docstring markdown extension". We currently parse docstrings in mkdocstrings (and soon in another project responsible for loading the docs only), but it would definitely be way better to have an extension doing that. It would have its own challenges but I think I came with a good design. This design is unfortunately still only available in my mind, but I plan on writing it down here, in a pinned issue, with a "needs help" tag. This would be great if contributors could help with it!

So, as you said in the last paragraph, now is not a good time for such a feature, but I hope I'll be able to come soon with the desired architecture, which will allow us to iteratively improve mkdocstrings.

You'll hear from me when things have moved forward 🙂

@pawamoy
Copy link
Member

pawamoy commented Mar 18, 2020

Hey, I'm close to a new release of mkdocstrings with the new architecture. The code responsible for parsing docstrings is now hosted at https://github.com/pawamoy/py-tkdocs, so I'm transfering this issue over there!

@pawamoy pawamoy transferred this issue from mkdocstrings/mkdocstrings Mar 18, 2020
@jaimergp
Copy link
Author

Oh, great news! Yes, of course, no probs!

Do you need help testing stuff out?

@pawamoy
Copy link
Member

pawamoy commented Mar 20, 2020

Well sure, I'll publish a new version of mkdocstrings, v0.9.0, and if you can test it to see if there are any bugs and report them it would be great 🙂

Then we'll be able to improve the handling of Examples section 😄

@pawamoy
Copy link
Member

pawamoy commented Mar 27, 2020

Hey again @jaimergp

Do you still want to contribute a PR to support the examples sections? I can guide you through the code if needed.

@jaimergp
Copy link
Author

Sure! Where should I start looking in?

@pawamoy
Copy link
Member

pawamoy commented Mar 30, 2020

All the changes should happen in src/pytkdocs/parsers/docstrings.py.

Now this would be the pytkdocs part. Then in mkdocstrings we would need to update the docstring.html template to support these type of sections 🙂

@pawamoy pawamoy added docstrings Docstrings parsing feature New feature or request docstrings-format:google labels Apr 25, 2020
@pawamoy
Copy link
Member

pawamoy commented May 7, 2020

Any progress @jaimergp? If not, and if you don't mind, I'll start working on this 🙂

@jaimergp
Copy link
Author

Sorry @pawamoy, I've been very slow to start new side projects and haven't been able to contribute time to this. Please go ahead and take over! Let me know if I can help you test the usage once it's ready!

@igonro
Copy link
Contributor

igonro commented May 13, 2020

Hey @pawamoy, how is this feature going? I would be interested too. I think I could help if needed.

@pawamoy
Copy link
Member

pawamoy commented May 13, 2020

I didn't start yet, so you can send a PR @igonro! Take a look at my comment above explaning how this can be done 🙂

There were some changes, the module is now src/pytkdocs/parsers/docstrings/google.py, and the method is parse_sections.

@igonro
Copy link
Contributor

igonro commented May 15, 2020

Hey @pawamoy, I'm starting this feature right now! But I found a little problem with the make setup command, it didn't find the pipcommand. In order to fix this, I changed pip and put instead python -m pip. I think this is a safer approach. So later I can send this change as PR if you want.

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

Ah, sorry about that, thanks for reporting that issue. I'll update it myself, thanks (I update my projects from template so it'll be easier to fix the template directly).

@igonro
Copy link
Contributor

igonro commented May 15, 2020

Man, I'm struggling to implement this functionality. I'm trying to write the read_examples_section, but I don't know very well what type of Section I have to return.

Edit: Nevermind, I've just realized that I didn't edit the docstring.html in mkdocstrings.

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

A new one! Just add a type in the Section.Type enum, in the base module, like EXAMPLE = "example".

EDIT: oh okay 🙂

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

I think we'll have to be a bit smarter in mkdocstrings for these examples section. If the codehilite extension is listed in the config, we should override its guess_lang option to True to make sure code blocks are highlighted as Python code. But we'll see that once pytkdocs can read such sections 🙂

Also don't hesitate to open your PR early. You can set it as "draft" of "wip", and this will allow us to discuss about the code 🙂

@igonro
Copy link
Contributor

igonro commented May 15, 2020

Yes. I think I will do that, because I'm a little bit blocked. 😞

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

Oh, I might have forgotten to mention the serialization: https://github.com/pawamoy/pytkdocs/blob/master/src/pytkdocs/serializer.py#L150 😅

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

We really should have a proper JSON encoder instead of these functions, but that's another story.

@igonro
Copy link
Contributor

igonro commented May 15, 2020

Ok. I will be working the following days in this feature when I can dedicate some time. It might take me a few days, but step by step I will eventually get it done 😃

@pawamoy
Copy link
Member

pawamoy commented May 15, 2020

Of course, thank you very much for contributing to mkdocstrings 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docstrings Docstrings parsing docstrings-format:google feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants