Skip to content

Commit 7b4ea91

Browse files
committed
Catch and log warnings with context from NumPyDoc
1 parent 95e70c1 commit 7b4ea91

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/docstub-stubs/_docstrings.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import traceback
5+
import warnings
56
from collections.abc import Generator, Iterable
67
from dataclasses import dataclass, field
78
from functools import cached_property

src/docstub/_docstrings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44
import traceback
5+
import warnings
56
from dataclasses import dataclass, field
67
from functools import cached_property
78

@@ -366,14 +367,20 @@ def __init__(self, docstring, *, matcher=None, reporter=None, stats=None):
366367
stats : ~.Stats, optional
367368
"""
368369
self.docstring = docstring
369-
self.np_docstring = npds.NumpyDocString(docstring)
370370
self.matcher = matcher or TypeMatcher()
371371
self.stats = Stats() if stats is None else stats
372372

373373
if reporter is None:
374374
reporter = ContextReporter(logger=logger, line=0)
375375
self.reporter = reporter.copy_with(logger=logger)
376376

377+
with warnings.catch_warnings(record=True) as records:
378+
self.np_docstring = npds.NumpyDocString(docstring)
379+
for message in records:
380+
short = "Warning in NumPyDoc while parsing docstring"
381+
details = message.message.args[0]
382+
self.reporter.warn(short, details=details)
383+
377384
@cached_property
378385
def attributes(self):
379386
"""Return the attributes found in the docstring.

tests/test_docstrings.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,23 @@ def test_combined_numpydoc_params(self):
282282

283283
assert "d" not in annotations.parameters
284284
assert "e" not in annotations.parameters
285+
286+
@pytest.mark.filterwarnings("default:Unknown section:UserWarning:numpydoc")
287+
def test_unknown_section_logged(self, caplog):
288+
docstring = dedent(
289+
"""
290+
Parameters
291+
----------
292+
a : bool
293+
294+
To Do
295+
-----
296+
An unknown section
297+
"""
298+
)
299+
annotations = DocstringAnnotations(docstring)
300+
assert len(annotations.parameters) == 1
301+
assert annotations.parameters["a"].value == "bool"
302+
303+
assert caplog.messages == ["Warning in NumPyDoc while parsing docstring"]
304+
assert caplog.records[0].details == "Unknown section To Do"

0 commit comments

Comments
 (0)