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

Add special case for NumpyDoc warnings section so they render as warning admonitions #236

Merged
merged 4 commits into from
Feb 12, 2024

Conversation

parafoxia
Copy link
Contributor

@parafoxia parafoxia commented Feb 10, 2024

This PR fixes a bug in the NumpyDoc parser where the Warnings section would render as a note admonition. They now render as warning admonitions.

I felt bad opening an issue about this after accusing the NumpyDoc parser of being buggy yesterday, so thought I'd at least contribute this one myself 😄

@parafoxia parafoxia marked this pull request as draft February 10, 2024 13:55
@parafoxia parafoxia force-pushed the fix-warnings-section-admonition branch from 970e722 to 919e6e8 Compare February 10, 2024 14:04
@parafoxia parafoxia marked this pull request as ready for review February 10, 2024 14:04
@parafoxia
Copy link
Contributor Author

I'm not sure what the deal with the (macos-latest, 3.8) tests is, but I don't think that's my fault? 😅

@pawamoy
Copy link
Member

pawamoy commented Feb 10, 2024

Indeed, not sure what these /var / /private/var shenanigans are.

Regarding the change: I don't like modifying kinds in the generic admonition class. Ideally, Griffe is never aware of how admonition kinds are used, which ones are available, etc. It all depends on the downstream docs system. For all we know, maybe someone has defined a custom warnings (plural) admonition that differs from a warning (singular) one. Or maybe other docstring styles allow such plural kinds.

With this in mind, I'd prefer that we handle this special case in the Numpy parser itself, rather than in the generic admonition class, and only because Warnings section are actually specified in the style-guide (https://numpydoc.readthedocs.io/en/latest/format.html#warnings).

Looks like _append_section would be the right place for that:

def _append_section(sections: list, current: list[str], admonition_title: str) -> None:
    if admonition_title:
        kind = admonition_title.lower().replace(" ", "-")
        # Numpydoc style-guide defines a Warnings section,
        # so we special-case it here to avoid breaking user expectations.
        if kind == "warnings":
            kind = "warning"
        sections.append(
            DocstringSectionAdmonition(
                kind=kind,
                text="\n".join(current).rstrip("\n"),
                title=admonition_title,
            ),
        )
    elif current and any(current):
        sections.append(DocstringSectionText("\n".join(current).rstrip("\n")))

WDYT?

By the way, maybe we should also modify notes into note. It works in MkDocs because code responsible for admonitions defaults to the note style when the kind is unknown, but maybe that's not the case in other systems.

@parafoxia
Copy link
Contributor Author

Wrote this comment, made the changes, then realised I forgot to press send lmao. I like your suggestion though, didn't realise there were NumpyDoc specific sections so deffo better to handle the special cases in those. I've also added a special case for the Notes section as well.

Copy link
Member

@pawamoy pawamoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, LGTM! Just two suggestions that I'll commit right away, and we can merge 🙂

tests/test_docstrings/test_numpy.py Outdated Show resolved Hide resolved
tests/test_docstrings/test_numpy.py Outdated Show resolved Hide resolved
@pawamoy pawamoy merged commit 3b47cdb into mkdocstrings:main Feb 12, 2024
6 of 17 checks passed
@parafoxia parafoxia deleted the fix-warnings-section-admonition branch February 12, 2024 08:47
@parafoxia parafoxia restored the fix-warnings-section-admonition branch February 25, 2024 11:13
@parafoxia
Copy link
Contributor Author

Leaving the branch up as I need to build the docs from it until the next Griffe version is released.

@pawamoy
Copy link
Member

pawamoy commented Feb 25, 2024

I wanted to pack a few more things in the next release, but they've been delayed, so I'll release one today 🙂

@pawamoy
Copy link
Member

pawamoy commented Feb 26, 2024

v0.41.0 released.

@parafoxia
Copy link
Contributor Author

Awesome, thanks! 🎉

@parafoxia parafoxia deleted the fix-warnings-section-admonition branch February 26, 2024 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants