Skip to content

Commit 3ad6333

Browse files
fix: Parse Deprecated sections in Google-style docstrings
The code to parse these sections already existed, but the "Deprecated" section header was missing from the table of known section headers. Being an unknown section, Griffe then treated this as an admonition. Griffe initially required a version indicator in Google-style Deprecated sections. This change now allows it to be missing, in which case the version is the empty string. Issue-352: #352 PR-353: #353
1 parent 0d71c2f commit 3ad6333

2 files changed

Lines changed: 78 additions & 2 deletions

File tree

src/_griffe/docstrings/google.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"modules": DocstringSectionKind.modules,
7070
"warns": DocstringSectionKind.warns,
7171
"warnings": DocstringSectionKind.warns,
72+
"deprecated": DocstringSectionKind.deprecated,
7273
}
7374

7475
_BlockItem = tuple[int, list[str]]
@@ -705,8 +706,7 @@ def _read_deprecated_section(
705706
try:
706707
version, text = text.split(":", 1)
707708
except ValueError:
708-
docstring_warning(docstring, new_offset, f"Could not parse version, text at line {offset}")
709-
return None, new_offset
709+
version = ""
710710

711711
version = version.lstrip()
712712
description = text.lstrip()

tests/test_docstrings/test_google.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,28 @@ def test_parse_yields_section(parse_google: ParserType) -> None:
525525
assert "'x'" in warnings[0]
526526

527527

528+
def test_parse_deprecated_section(parse_google: ParserType) -> None:
529+
"""Parse Deprecated section.
530+
531+
See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
532+
for context.
533+
534+
Parameters:
535+
parse_google: Fixture parser.
536+
"""
537+
docstring = """
538+
Deprecated:
539+
1.0: This function is deprecated.
540+
"""
541+
sections, warnings = parse_google(docstring)
542+
assert len(sections) == 1
543+
deprecated = sections[0]
544+
assert deprecated.kind is DocstringSectionKind.deprecated
545+
assert deprecated.value.version == "1.0"
546+
assert deprecated.value.description == "This function is deprecated."
547+
assert not warnings
548+
549+
528550
def test_invalid_sections(parse_google: ParserType) -> None:
529551
"""Warn on invalid sections.
530552
@@ -1131,6 +1153,60 @@ def test_parse_returns_tuple_in_generator(parse_google: ParserType) -> None:
11311153
assert returns[1].annotation.name == "float"
11321154

11331155

1156+
# =============================================================================================
1157+
# Deprecated sections
1158+
1159+
1160+
def test_parse_deprecated_section_missing_version(parse_google: ParserType) -> None:
1161+
"""Parse version numbers in Deprecated sections that aren't numbers.
1162+
1163+
See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
1164+
for context.
1165+
1166+
Parameters:
1167+
parse_google: Fixture parser.
1168+
"""
1169+
docstring = """
1170+
Summary.
1171+
1172+
Deprecated:
1173+
Since "Dapper Drake": This function is deprecated.
1174+
"""
1175+
sections, warnings = parse_google(docstring)
1176+
assert len(sections) == 2
1177+
deprecated = sections[1]
1178+
assert deprecated.kind is DocstringSectionKind.deprecated
1179+
assert deprecated.value.version == 'Since "Dapper Drake"'
1180+
assert deprecated.value.description == "This function is deprecated."
1181+
assert not warnings
1182+
1183+
1184+
def test_dont_warn_about_missing_deprecated_version(parse_google: ParserType) -> None:
1185+
"""Don't warn about missing version info in Deprecated sections.
1186+
1187+
Also assert that we don't just swallow the section, but actually parse it.
1188+
1189+
See [`mkdocstrings/griffe#352`](https://github.com/mkdocstrings/griffe/issues/352)
1190+
for context.
1191+
1192+
Parameters:
1193+
parse_google: Fixture parser.
1194+
"""
1195+
docstring = """
1196+
Summary.
1197+
1198+
Deprecated:
1199+
This function is deprecated since v0.9alpha1.
1200+
"""
1201+
sections, warnings = parse_google(docstring)
1202+
assert len(sections) == 2
1203+
deprecated = sections[1]
1204+
assert deprecated.kind is DocstringSectionKind.deprecated
1205+
assert not deprecated.value.version
1206+
assert deprecated.value.description == "This function is deprecated since v0.9alpha1."
1207+
assert not warnings
1208+
1209+
11341210
# =============================================================================================
11351211
# Parser special features
11361212
def test_parse_admonitions(parse_google: ParserType) -> None:

0 commit comments

Comments
 (0)