|
| 1 | +"""Tests for configuration options.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import inspect |
| 6 | +from dataclasses import asdict, fields |
| 7 | +from typing import TYPE_CHECKING, Any |
| 8 | + |
| 9 | +import pytest |
| 10 | +from griffe import Parser, parse_google, parse_numpy, parse_sphinx |
| 11 | + |
| 12 | +from mkdocstrings_handlers.python import AutoStyleOptions, GoogleStyleOptions, NumpyStyleOptions, SphinxStyleOptions |
| 13 | +from mkdocstrings_handlers.python._internal.handler import _filter_parser_options |
| 14 | + |
| 15 | +if TYPE_CHECKING: |
| 16 | + from collections.abc import Callable |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize( |
| 20 | + ("options_class", "parser"), |
| 21 | + [ |
| 22 | + (GoogleStyleOptions, parse_google), |
| 23 | + (NumpyStyleOptions, parse_numpy), |
| 24 | + (SphinxStyleOptions, parse_sphinx), |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_style_options_match_griffe_parser(options_class: type[Any], parser: Callable[..., object]) -> None: |
| 28 | + """Ensure style options stay in sync with Griffe parser options.""" |
| 29 | + option_names = {field.name for field in fields(options_class)} |
| 30 | + parser_parameters = inspect.signature(parser).parameters |
| 31 | + parser_option_names = set(parser_parameters) - {"docstring"} |
| 32 | + |
| 33 | + assert parser_option_names <= option_names |
| 34 | + |
| 35 | + |
| 36 | +def test_filter_style_options(caplog: pytest.LogCaptureFixture) -> None: |
| 37 | + """Ensure unsupported options are not passed to Griffe and are reported.""" |
| 38 | + options = asdict(SphinxStyleOptions()) |
| 39 | + |
| 40 | + filtered_options = _filter_parser_options(Parser.sphinx, options) |
| 41 | + |
| 42 | + assert filtered_options == { |
| 43 | + name: value for name, value in options.items() if name in inspect.signature(parse_sphinx).parameters |
| 44 | + } |
| 45 | + assert "warn_missing_types" in options |
| 46 | + for name in set(options) - set(filtered_options or {}): |
| 47 | + assert f"Ignoring unsupported sphinx docstring parser option: {name}" in caplog.text |
| 48 | + |
| 49 | + |
| 50 | +def test_filter_auto_style_options(caplog: pytest.LogCaptureFixture) -> None: |
| 51 | + """Ensure unsupported options nested in auto style options are reported.""" |
| 52 | + options = asdict(AutoStyleOptions()) |
| 53 | + |
| 54 | + filtered_options = _filter_parser_options(Parser.auto, options) |
| 55 | + |
| 56 | + assert filtered_options is not None |
| 57 | + if "warn_missing_types" in inspect.signature(parse_sphinx).parameters: |
| 58 | + assert "warn_missing_types" in filtered_options["per_style_options"]["sphinx"] |
| 59 | + else: |
| 60 | + assert "warn_missing_types" not in filtered_options["per_style_options"]["sphinx"] |
| 61 | + assert "Ignoring unsupported sphinx docstring parser option: warn_missing_types" in caplog.text |
0 commit comments