Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Deprecate 'selection' and 'rendering' YAML keys #420

Merged
merged 2 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions mkdocs.yml
Expand Up @@ -91,11 +91,10 @@ plugins:
- https://docs.python.org/3/objects.inv
- https://docs.python-requests.org/en/master/objects.inv # demonstration purpose in the docs
- https://mkdocstrings.github.io/autorefs/objects.inv
selection:
options:
docstring_style: google
docstring_options:
ignore_init_summary: yes
rendering:
merge_init_into_class: yes
show_submodules: no
watch:
Expand Down
17 changes: 13 additions & 4 deletions src/mkdocstrings/extension.py
Expand Up @@ -24,6 +24,7 @@
import re
from collections import ChainMap
from typing import Any, Mapping, MutableMapping, MutableSequence, Tuple
from warnings import warn
from xml.etree.ElementTree import Element

import yaml
Expand Down Expand Up @@ -173,13 +174,21 @@ def _process_block(
handler_config = self._handlers.get_handler_config(handler_name)
handler = self._handlers.get_handler(handler_name, handler_config)

selection, rendering = get_item_configs(handler_config, config)
options = ChainMap(config.get("options", {}), handler_config.get("options", {}))
if not options:
selection, rendering = get_item_configs(handler_config, config)
if selection or rendering:
warn(
"'selection' and 'rendering' are deprecated and merged into a single 'options' YAML key",
DeprecationWarning,
)
options = ChainMap(*selection.maps, *rendering.maps) # type: ignore[attr-defined]
if heading_level:
rendering = ChainMap(rendering, {"heading_level": heading_level}) # like setdefault
options = ChainMap(options, {"heading_level": heading_level}) # like setdefault

log.debug("Collecting data")
try:
data: CollectorItem = handler.collect(identifier, selection)
data: CollectorItem = handler.collect(identifier, options)
except CollectionError as exception:
log.error(str(exception))
if PluginError is SystemExit: # When MkDocs 1.2 is sufficiently common, this can be dropped.
Expand All @@ -193,7 +202,7 @@ def _process_block(

log.debug("Rendering templates")
try:
rendered = handler.render(data, rendering)
rendered = handler.render(data, options)
except TemplateNotFound as exc:
theme_name = self._config["theme_name"]
log.error(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_extension.py
Expand Up @@ -135,3 +135,15 @@ def test_dont_register_every_identifier_as_anchor(plugin):
for identifier in ids:
assert identifier not in autorefs._url_map # noqa: WPS437
assert identifier not in autorefs._abs_url_map # noqa: WPS437


def test_use_deprecated_yaml_keys(ext_markdown):
"""Check that using the deprecated 'selection' and 'rendering' YAML keys emits a deprecation warning."""
with pytest.warns(DeprecationWarning, match="single 'options' YAML key"):
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n rendering:\n heading_level: 2")


def test_use_new_options_yaml_key(ext_markdown):
"""Check that using the new 'options' YAML key works as expected."""
assert "h1" in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 1")
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 2")