Skip to content

Commit

Permalink
refactor: Use ChainMap instead of copying dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Nov 1, 2020
1 parent 9815d5a commit c634d2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/mkdocstrings/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
```
"""
import re
from typing import Any, Tuple
from collections import ChainMap
from typing import Any, Mapping, Tuple
from xml.etree.ElementTree import XML, Element, ParseError # noqa: S405 (we choose to trust the XML input)

import yaml
Expand Down Expand Up @@ -246,7 +247,7 @@ def get_handler_config(self, handler_name: str) -> dict:
return {}


def get_item_configs(handler_config: dict, config: dict) -> Tuple[dict, dict]:
def get_item_configs(handler_config: dict, config: dict) -> Tuple[Mapping, Mapping]:
"""
Get the selection and rendering configuration merged into the global configuration of the given handler.
Expand All @@ -257,10 +258,8 @@ def get_item_configs(handler_config: dict, config: dict) -> Tuple[dict, dict]:
Returns:
Two dictionaries: selection and rendering. The local configurations are merged into the global ones.
"""
item_selection_config = dict(handler_config.get("selection", {}))
item_selection_config.update(config.get("selection", {}))
item_rendering_config = dict(handler_config.get("rendering", {}))
item_rendering_config.update(config.get("rendering", {}))
item_selection_config = ChainMap(config.get("selection", {}), handler_config.get("selection", {}))
item_rendering_config = ChainMap(config.get("rendering", {}), handler_config.get("rendering", {}))
return item_selection_config, item_rendering_config


Expand Down
9 changes: 4 additions & 5 deletions src/mkdocstrings/handlers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os
import sys
from collections import ChainMap
from subprocess import PIPE, Popen # noqa: S404 (what other option, more secure that PIPE do we have? sockets?)
from typing import Any, List, Optional

Expand Down Expand Up @@ -65,15 +66,14 @@ class PythonRenderer(BaseRenderer):
""" # noqa: E501

def render(self, data: Any, config: dict) -> str: # noqa: D102 (ignore missing docstring)
final_config = dict(self.default_config)
final_config.update(config)
final_config = ChainMap(config, self.default_config)

template = self.env.get_template(f"{data['category']}.html")

# Heading level is a "state" variable, that will change at each step
# of the rendering recursion. Therefore, it's easier to use it as a plain value
# than as an item in a dictionary.
heading_level = final_config.pop("heading_level")
heading_level = final_config["heading_level"]

return template.render(
**{"config": final_config, data["category"]: data, "heading_level": heading_level, "root": True},
Expand Down Expand Up @@ -192,8 +192,7 @@ def collect(self, identifier: str, config: dict) -> Any:
Returns:
The collected object-tree.
"""
final_config = dict(self.default_config)
final_config.update(config)
final_config = ChainMap(config, self.default_config)

log.debug("Preparing input")
json_input = json.dumps({"objects": [{"path": identifier, **final_config}]})
Expand Down

0 comments on commit c634d2c

Please sign in to comment.