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

fix: Parse Deprecated sections in Google-style docstrings #353

Merged
merged 5 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/_griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"modules": DocstringSectionKind.modules,
"warns": DocstringSectionKind.warns,
"warnings": DocstringSectionKind.warns,
"deprecated": DocstringSectionKind.deprecated,
}

_BlockItem = tuple[int, list[str]]
Expand Down Expand Up @@ -707,8 +708,7 @@ def _read_deprecated_section(
try:
version, text = text.split(":", 1)
except ValueError:
docstring_warning(docstring, new_offset, f"Could not parse version, text at line {offset}")
return None, new_offset
version = ""

version = version.lstrip()
description = text.lstrip()
Expand Down
76 changes: 76 additions & 0 deletions tests/test_docstrings/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,28 @@ def test_parse_yields_section(parse_google: ParserType) -> None:
assert "'x'" in warnings[0]


def test_parse_deprecated_section(parse_google: ParserType) -> None:
"""Parse Deprecated section.

See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
for context.

Parameters:
parse_google: Fixture parser.
"""
docstring = """
Deprecated:
1.0: This function is deprecated.
"""
sections, warnings = parse_google(docstring)
assert len(sections) == 1
deprecated = sections[0]
assert deprecated.kind is DocstringSectionKind.deprecated
assert deprecated.value.version == "1.0"
assert deprecated.value.description == "This function is deprecated."
assert not warnings


def test_invalid_sections(parse_google: ParserType) -> None:
"""Warn on invalid sections.

Expand Down Expand Up @@ -1131,6 +1153,60 @@ def test_parse_returns_tuple_in_generator(parse_google: ParserType) -> None:
assert returns[1].annotation.name == "float"


# =============================================================================================
# Deprecated sections


def test_parse_deprecated_section_missing_version(parse_google: ParserType) -> None:
"""Parse version numbers in Deprecated sections that aren't numbers.

See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
for context.

Parameters:
parse_google: Fixture parser.
"""
docstring = """
Summary.

Deprecated:
Since "Dapper Drake": This function is deprecated.
"""
sections, warnings = parse_google(docstring)
assert len(sections) == 2
deprecated = sections[1]
assert deprecated.kind is DocstringSectionKind.deprecated
assert deprecated.value.version == 'Since "Dapper Drake"'
assert deprecated.value.description == "This function is deprecated."
assert not warnings


def test_dont_warn_about_missing_deprecated_version(parse_google: ParserType) -> None:
"""Don't warn about missing version info in Deprecated sections.

Also assert that we don't just swallow the section, but actually parse it.

See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
for context.

Parameters:
parse_google: Fixture parser.
"""
docstring = """
Summary.

Deprecated:
This function is deprecated since v0.9alpha1.
"""
sections, warnings = parse_google(docstring)
assert len(sections) == 2
deprecated = sections[1]
assert deprecated.kind is DocstringSectionKind.deprecated
assert not deprecated.value.version
assert deprecated.value.description == "This function is deprecated since v0.9alpha1."
assert not warnings


# =============================================================================================
# Parser special features
def test_parse_admonitions(parse_google: ParserType) -> None:
Expand Down
Loading