Skip to content

Commit 07564b4

Browse files
committed
feat: Allow disabling "missing type/annotation" warnings
Issue-mkdocstrings-437: mkdocstrings/mkdocstrings#437
1 parent 34d8051 commit 07564b4

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

docs/reference/docstrings.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ The parser accepts a few options:
128128
- `returns_type_in_property_summary`: Whether to parse the return type of properties at the beginning of their summary: `str: Summary of the property`. Default: false.
129129
- `trim_doctest_flags`: Remove the [doctest flags] written as comments in `pycon` snippets within a docstring. These flags are used to alter the behavior of [doctest] when testing docstrings, and should not be visible in your docs. Default: true.
130130
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
131+
- `warn_missing_types`: Warn about missing type or annotation for parameters, return values, etc. Default: true.
132+
- `warnings`: Generally enable/disable warnings when parsing docstrings. Default: true.
131133

132134
### Sections {#google-sections}
133135

@@ -823,6 +825,8 @@ The parser accepts a few options:
823825
- `ignore_init_summary`: Ignore the first line in `__init__` methods' docstrings. Useful when merging `__init__` docstring into class' docstrings with mkdocstrings-python's [`merge_init_into_class`][merge_init] option. Default: false.
824826
- `trim_doctest_flags`: Remove the [doctest flags] written as comments in `pycon` snippets within a docstring. These flags are used to alter the behavior of [doctest] when testing docstrings, and should not be visible in your docs. Default: true.
825827
- `warn_unknown_params`: Warn about parameters documented in docstrings that do not appear in the signature. Default: true.
828+
- `warn_missing_types`: Warn about missing type or annotation for parameters, return values, etc. Default: true.
829+
- `warnings`: Generally enable/disable warnings when parsing docstrings. Default: true.
826830

827831
### Sections {#numpydoc-sections}
828832

src/griffe/_internal/docstrings/google.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def _read_parameters(
189189
*,
190190
offset: int,
191191
warn_unknown_params: bool = True,
192+
warn_missing_types: bool = True,
192193
warnings: bool = True,
193194
**options: Any,
194195
) -> tuple[list[DocstringParameter], int]:
@@ -232,7 +233,7 @@ def _read_parameters(
232233
except (AttributeError, KeyError):
233234
default = None
234235

235-
if warnings and annotation is None:
236+
if warnings and warn_missing_types and annotation is None:
236237
docstring_warning(docstring, line_number, f"No type or annotation for parameter '{name}'")
237238

238239
if warnings and warn_unknown_params:
@@ -638,6 +639,7 @@ def _read_returns_section(
638639
offset: int,
639640
returns_multiple_items: bool = True,
640641
returns_named_value: bool = True,
642+
warn_missing_types: bool = True,
641643
warnings: bool = True,
642644
**options: Any,
643645
) -> tuple[DocstringSectionReturns | None, int]:
@@ -668,7 +670,7 @@ def _read_returns_section(
668670
# Try to retrieve the annotation from the docstring parent.
669671
annotation = _annotation_from_parent(docstring, gen_index=2, multiple=len(block) > 1, index=index)
670672

671-
if warnings and annotation is None:
673+
if warnings and warn_missing_types and annotation is None:
672674
returned_value = repr(name) if name else index + 1
673675
docstring_warning(docstring, line_number, f"No type or annotation for returned value {returned_value}")
674676

@@ -683,6 +685,7 @@ def _read_yields_section(
683685
offset: int,
684686
returns_multiple_items: bool = True,
685687
returns_named_value: bool = True,
688+
warn_missing_types: bool = True,
686689
warnings: bool = True,
687690
**options: Any,
688691
) -> tuple[DocstringSectionYields | None, int]:
@@ -713,7 +716,7 @@ def _read_yields_section(
713716
# Try to retrieve the annotation from the docstring parent.
714717
annotation = _annotation_from_parent(docstring, gen_index=0, multiple=len(block) > 1, index=index)
715718

716-
if warnings and annotation is None:
719+
if warnings and warn_missing_types and annotation is None:
717720
yielded_value = repr(name) if name else index + 1
718721
docstring_warning(docstring, line_number, f"No type or annotation for yielded value {yielded_value}")
719722

@@ -728,6 +731,7 @@ def _read_receives_section(
728731
offset: int,
729732
receives_multiple_items: bool = True,
730733
receives_named_value: bool = True,
734+
warn_missing_types: bool = True,
731735
warnings: bool = True,
732736
**options: Any,
733737
) -> tuple[DocstringSectionReceives | None, int]:
@@ -758,7 +762,7 @@ def _read_receives_section(
758762
# Try to retrieve the annotation from the docstring parent.
759763
annotation = _annotation_from_parent(docstring, gen_index=1, multiple=len(block) > 1, index=index)
760764

761-
if warnings and annotation is None:
765+
if warnings and warn_missing_types and annotation is None:
762766
received_value = repr(name) if name else index + 1
763767
docstring_warning(docstring, line_number, f"No type or annotation for received value {received_value}")
764768

@@ -861,6 +865,7 @@ def parse_google(
861865
receives_multiple_items: bool = True,
862866
receives_named_value: bool = True,
863867
warn_unknown_params: bool = True,
868+
warn_missing_types: bool = True,
864869
warnings: bool = True,
865870
**options: Any,
866871
) -> list[DocstringSection]:
@@ -888,6 +893,7 @@ def parse_google(
888893
returns_type_in_property_summary: Whether to parse the return type of properties
889894
at the beginning of their summary: `str: Summary of the property`.
890895
warn_unknown_params: Warn about documented parameters not appearing in the signature.
896+
warn_missing_types: Warn about missing types/annotations for parameters, return values, etc.
891897
warnings: Whether to log warnings at all.
892898
**options: Additional parsing options.
893899
@@ -909,6 +915,7 @@ def parse_google(
909915
"receives_multiple_items": receives_multiple_items,
910916
"receives_named_value": receives_named_value,
911917
"warn_unknown_params": warn_unknown_params,
918+
"warn_missing_types": warn_missing_types,
912919
"warnings": warnings,
913920
**options,
914921
}

src/griffe/_internal/docstrings/numpy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ def _read_parameters(
225225
*,
226226
offset: int,
227227
warn_unknown_params: bool = True,
228+
warn_missing_types: bool = True,
228229
warnings: bool = True,
229230
**options: Any,
230231
) -> tuple[list[DocstringParameter], int]:
@@ -263,7 +264,7 @@ def _read_parameters(
263264
annotation = docstring.parent.parameters[name].annotation # type: ignore[union-attr]
264265
break
265266
else:
266-
if warnings:
267+
if warnings and warn_missing_types:
267268
docstring_warning(docstring, new_offset, f"No types or annotations for parameters {names}")
268269
else:
269270
annotation = parse_docstring_annotation(annotation, docstring, log_level=LogLevel.debug)
@@ -897,6 +898,7 @@ def parse_numpy(
897898
ignore_init_summary: bool = False,
898899
trim_doctest_flags: bool = True,
899900
warn_unknown_params: bool = True,
901+
warn_missing_types: bool = True,
900902
warnings: bool = True,
901903
**options: Any,
902904
) -> list[DocstringSection]:
@@ -910,6 +912,7 @@ def parse_numpy(
910912
ignore_init_summary: Whether to ignore the summary in `__init__` methods' docstrings.
911913
trim_doctest_flags: Whether to remove doctest flags from Python example blocks.
912914
warn_unknown_params: Warn about documented parameters not appearing in the signature.
915+
warn_missing_types: Warn about missing types/annotations for parameters, return values, etc.
913916
warnings: Whether to log warnings at all.
914917
**options: Additional parsing options.
915918
@@ -927,6 +930,7 @@ def parse_numpy(
927930
"trim_doctest_flags": trim_doctest_flags,
928931
"ignore_init_summary": ignore_init_summary,
929932
"warn_unknown_params": warn_unknown_params,
933+
"warn_missing_types": warn_missing_types,
930934
"warnings": warnings,
931935
**options,
932936
}

0 commit comments

Comments
 (0)