Skip to content

Commit 5bf0746

Browse files
committed
fix: Allow passing warn_unknown_params option to Google and Numpy parsers
1 parent 65fee70 commit 5bf0746

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

src/griffe/docstrings/google.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
_RE_DOCTEST_FLAGS: Pattern = re.compile(r"(\s*#\s*doctest:.+)$")
8383

8484

85-
def _read_block_items(docstring: Docstring, *, offset: int) -> ItemsBlock:
85+
def _read_block_items(docstring: Docstring, *, offset: int, **options: Any) -> ItemsBlock: # noqa: ARG001
8686
lines = docstring.lines
8787
if offset >= len(lines):
8888
return [], offset
@@ -145,7 +145,7 @@ def _read_block_items(docstring: Docstring, *, offset: int) -> ItemsBlock:
145145
return items, new_offset - 1
146146

147147

148-
def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]:
148+
def _read_block(docstring: Docstring, *, offset: int, **options: Any) -> tuple[str, int]: # noqa: ARG001
149149
lines = docstring.lines
150150
if offset >= len(lines):
151151
return "", offset - 1
@@ -181,11 +181,12 @@ def _read_parameters(
181181
*,
182182
offset: int,
183183
warn_unknown_params: bool = True,
184+
**options: Any,
184185
) -> tuple[list[DocstringParameter], int]:
185186
parameters = []
186187
annotation: str | Expr | None
187188

188-
block, new_offset = _read_block_items(docstring, offset=offset)
189+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
189190

190191
for line_number, param_lines in block:
191192
# check the presence of a name and description, separated by a colon
@@ -241,9 +242,9 @@ def _read_parameters_section(
241242
docstring: Docstring,
242243
*,
243244
offset: int,
244-
**options: Any, # noqa: ARG001
245+
**options: Any,
245246
) -> tuple[DocstringSectionParameters | None, int]:
246-
parameters, new_offset = _read_parameters(docstring, offset=offset)
247+
parameters, new_offset = _read_parameters(docstring, offset=offset, **options)
247248

248249
if parameters:
249250
return DocstringSectionParameters(parameters), new_offset
@@ -256,9 +257,10 @@ def _read_other_parameters_section(
256257
docstring: Docstring,
257258
*,
258259
offset: int,
259-
**options: Any, # noqa: ARG001
260+
warn_unknown_params: bool = True, # noqa: ARG001
261+
**options: Any,
260262
) -> tuple[DocstringSectionOtherParameters | None, int]:
261-
parameters, new_offset = _read_parameters(docstring, offset=offset, warn_unknown_params=False)
263+
parameters, new_offset = _read_parameters(docstring, offset=offset, warn_unknown_params=False, **options)
262264

263265
if parameters:
264266
return DocstringSectionOtherParameters(parameters), new_offset
@@ -271,10 +273,10 @@ def _read_attributes_section(
271273
docstring: Docstring,
272274
*,
273275
offset: int,
274-
**options: Any, # noqa: ARG001
276+
**options: Any,
275277
) -> tuple[DocstringSectionAttributes | None, int]:
276278
attributes = []
277-
block, new_offset = _read_block_items(docstring, offset=offset)
279+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
278280

279281
annotation: str | Expr | None = None
280282
for line_number, attr_lines in block:
@@ -311,10 +313,10 @@ def _read_functions_section(
311313
docstring: Docstring,
312314
*,
313315
offset: int,
314-
**options: Any, # noqa: ARG001
316+
**options: Any,
315317
) -> tuple[DocstringSectionFunctions | None, int]:
316318
functions = []
317-
block, new_offset = _read_block_items(docstring, offset=offset)
319+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
318320

319321
signature: str | Expr | None = None
320322
for line_number, func_lines in block:
@@ -346,10 +348,10 @@ def _read_classes_section(
346348
docstring: Docstring,
347349
*,
348350
offset: int,
349-
**options: Any, # noqa: ARG001
351+
**options: Any,
350352
) -> tuple[DocstringSectionClasses | None, int]:
351353
classes = []
352-
block, new_offset = _read_block_items(docstring, offset=offset)
354+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
353355

354356
signature: str | Expr | None = None
355357
for line_number, class_lines in block:
@@ -381,10 +383,10 @@ def _read_modules_section(
381383
docstring: Docstring,
382384
*,
383385
offset: int,
384-
**options: Any, # noqa: ARG001
386+
**options: Any,
385387
) -> tuple[DocstringSectionModules | None, int]:
386388
modules = []
387-
block, new_offset = _read_block_items(docstring, offset=offset)
389+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
388390

389391
for line_number, module_lines in block:
390392
try:
@@ -406,10 +408,10 @@ def _read_raises_section(
406408
docstring: Docstring,
407409
*,
408410
offset: int,
409-
**options: Any, # noqa: ARG001
411+
**options: Any,
410412
) -> tuple[DocstringSectionRaises | None, int]:
411413
exceptions = []
412-
block, new_offset = _read_block_items(docstring, offset=offset)
414+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
413415

414416
annotation: str | Expr
415417
for line_number, exception_lines in block:
@@ -434,10 +436,10 @@ def _read_warns_section(
434436
docstring: Docstring,
435437
*,
436438
offset: int,
437-
**options: Any, # noqa: ARG001
439+
**options: Any,
438440
) -> tuple[DocstringSectionWarns | None, int]:
439441
warns = []
440-
block, new_offset = _read_block_items(docstring, offset=offset)
442+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
441443

442444
for line_number, warning_lines in block:
443445
try:
@@ -460,14 +462,14 @@ def _read_returns_section(
460462
*,
461463
offset: int,
462464
returns_multiple_items: bool,
463-
**options: Any, # noqa: ARG001
465+
**options: Any,
464466
) -> tuple[DocstringSectionReturns | None, int]:
465467
returns = []
466468

467469
if returns_multiple_items:
468-
block, new_offset = _read_block_items(docstring, offset=offset)
470+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
469471
else:
470-
one_block, new_offset = _read_block(docstring, offset=offset)
472+
one_block, new_offset = _read_block(docstring, offset=offset, **options)
471473
block = [(new_offset, one_block.splitlines())]
472474

473475
for index, (line_number, return_lines) in enumerate(block):
@@ -525,10 +527,10 @@ def _read_yields_section(
525527
docstring: Docstring,
526528
*,
527529
offset: int,
528-
**options: Any, # noqa: ARG001
530+
**options: Any,
529531
) -> tuple[DocstringSectionYields | None, int]:
530532
yields = []
531-
block, new_offset = _read_block_items(docstring, offset=offset)
533+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
532534

533535
for index, (line_number, yield_lines) in enumerate(block):
534536
match = _RE_NAME_ANNOTATION_DESCRIPTION.match(yield_lines[0])
@@ -576,10 +578,10 @@ def _read_receives_section(
576578
docstring: Docstring,
577579
*,
578580
offset: int,
579-
**options: Any, # noqa: ARG001
581+
**options: Any,
580582
) -> tuple[DocstringSectionReceives | None, int]:
581583
receives = []
582-
block, new_offset = _read_block_items(docstring, offset=offset)
584+
block, new_offset = _read_block_items(docstring, offset=offset, **options)
583585

584586
for index, (line_number, receive_lines) in enumerate(block):
585587
match = _RE_NAME_ANNOTATION_DESCRIPTION.match(receive_lines[0])
@@ -624,9 +626,9 @@ def _read_examples_section(
624626
*,
625627
offset: int,
626628
trim_doctest_flags: bool = True,
627-
**options: Any, # noqa: ARG001
629+
**options: Any,
628630
) -> tuple[DocstringSectionExamples | None, int]:
629-
text, new_offset = _read_block(docstring, offset=offset)
631+
text, new_offset = _read_block(docstring, offset=offset, **options)
630632

631633
sub_sections: list[tuple[Literal[DocstringSectionKind.text, DocstringSectionKind.examples], str]] = []
632634
in_code_example = False
@@ -686,9 +688,9 @@ def _read_deprecated_section(
686688
docstring: Docstring,
687689
*,
688690
offset: int,
689-
**options: Any, # noqa: ARG001
691+
**options: Any,
690692
) -> tuple[DocstringSectionDeprecated | None, int]:
691-
text, new_offset = _read_block(docstring, offset=offset)
693+
text, new_offset = _read_block(docstring, offset=offset, **options)
692694

693695
# early exit if there is no text in the yield section
694696
if not text:
@@ -738,6 +740,7 @@ def parse(
738740
ignore_init_summary: bool = False,
739741
trim_doctest_flags: bool = True,
740742
returns_multiple_items: bool = True,
743+
warn_unknown_params: bool = True,
741744
**options: Any,
742745
) -> list[DocstringSection]:
743746
"""Parse a docstring.
@@ -750,6 +753,7 @@ def parse(
750753
ignore_init_summary: Whether to ignore the summary in `__init__` methods' docstrings.
751754
trim_doctest_flags: Whether to remove doctest flags from Python example blocks.
752755
returns_multiple_items: Whether the `Returns` section has multiple items.
756+
warn_unknown_params: Warn about documented parameters not appearing in the signature.
753757
**options: Additional parsing options.
754758
755759
Returns:
@@ -765,6 +769,7 @@ def parse(
765769
"ignore_init_summary": ignore_init_summary,
766770
"trim_doctest_flags": trim_doctest_flags,
767771
"returns_multiple_items": returns_multiple_items,
772+
"warn_unknown_params": warn_unknown_params,
768773
**options,
769774
}
770775

src/griffe/docstrings/numpy.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def _read_block_items(
186186
return items, new_offset - 1
187187

188188

189-
def _read_block(docstring: Docstring, *, offset: int) -> tuple[str, int]:
189+
def _read_block(docstring: Docstring, *, offset: int, **options: Any) -> tuple[str, int]: # noqa: ARG001
190190
lines = docstring.lines
191191
if offset >= len(lines):
192192
return "", offset
@@ -336,6 +336,7 @@ def _read_other_parameters_section(
336336
docstring: Docstring,
337337
*,
338338
offset: int,
339+
warn_unknown_params: bool = True, # noqa: ARG001
339340
**options: Any,
340341
) -> tuple[DocstringSectionOtherParameters | None, int]:
341342
parameters, new_offset = _read_parameters(docstring, offset=offset, warn_unknown_params=False, **options)
@@ -696,9 +697,9 @@ def _read_examples_section(
696697
*,
697698
offset: int,
698699
trim_doctest_flags: bool = True,
699-
**options: Any, # noqa: ARG001
700+
**options: Any,
700701
) -> tuple[DocstringSectionExamples | None, int]:
701-
text, new_offset = _read_block(docstring, offset=offset)
702+
text, new_offset = _read_block(docstring, offset=offset, **options)
702703

703704
sub_sections: list[tuple[Literal[DocstringSectionKind.text, DocstringSectionKind.examples], str]] = []
704705
in_code_example = False
@@ -777,6 +778,7 @@ def parse(
777778
ignore_init_summary: bool = False,
778779
trim_doctest_flags: bool = True,
779780
allow_section_blank_line: bool = False,
781+
warn_unknown_params: bool = True,
780782
**options: Any,
781783
) -> list[DocstringSection]:
782784
"""Parse a docstring.
@@ -791,6 +793,7 @@ def parse(
791793
allow_section_blank_line: Whether to continue a section if there's an empty line
792794
between items in a formatted block, like Parameters or Returns.
793795
If True, you can still create a new section using two empty lines.
796+
warn_unknown_params: Warn about documented parameters not appearing in the signature.
794797
**options: Additional parsing options.
795798
796799
Returns:
@@ -806,6 +809,7 @@ def parse(
806809
"trim_doctest_flags": trim_doctest_flags,
807810
"ignore_init_summary": ignore_init_summary,
808811
"allow_section_blank_line": allow_section_blank_line,
812+
"warn_unknown_params": warn_unknown_params,
809813
**options,
810814
}
811815

0 commit comments

Comments
 (0)