diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index cd20e44..d9e2017 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -2,7 +2,7 @@ import pathlib import sys -from typing import Mapping, NoReturn, Optional +from typing import MutableMapping, Optional, Sequence, Union import markdown_it import mdformat @@ -21,6 +21,9 @@ cache = lru_cache() +_ConfigOptions = MutableMapping[str, Union[int, str, Sequence[str]]] + + @cache def _find_pyproject_toml_path(search_path: str) -> Optional[pathlib.Path]: """Find the pyproject.toml file that corresponds to the search path. @@ -45,7 +48,7 @@ def _find_pyproject_toml_path(search_path: str) -> Optional[pathlib.Path]: @cache -def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[Mapping]: +def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]: """Extract and validate the mdformat options from the pyproject.toml file. The options are searched inside a [tool.mdformat] key within the toml file, @@ -56,11 +59,12 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[Mapping]: if options is not None: mdformat._conf._validate_keys(options, pyproject_path) mdformat._conf._validate_values(options, pyproject_path) - return options + + return options @cache -def _reload_cli_opts() -> Mapping: +def _reload_cli_opts() -> _ConfigOptions: """Re-parse the sys.argv array to deduce which arguments were used in the CLI. If unknown arguments are found, we deduce that mdformat is being used as a @@ -93,17 +97,17 @@ def _reload_cli_opts() -> Mapping: return {key: value for key, value in vars(args).items() if value is not None} -def update_mdit(mdit: markdown_it.MarkdownIt) -> NoReturn: +def update_mdit(mdit: markdown_it.MarkdownIt) -> None: """Read the pyproject.toml file and re-create the mdformat options.""" - mdformat_options = mdit.options["mdformat"] + mdformat_options: _ConfigOptions = mdit.options["mdformat"] file_path = mdformat_options.get("filename", "-") pyproject_path = _find_pyproject_toml_path(file_path) if pyproject_path: pyproject_opts = _parse_pyproject(pyproject_path) if pyproject_opts is not None: cli_opts = _reload_cli_opts() - new_options: Mapping = {**pyproject_opts, **cli_opts} - mdformat_options.update(new_options) + mdformat_options.update(**pyproject_opts) + mdformat_options.update(**cli_opts) -RENDERERS: Mapping[str, Render] = {} +RENDERERS: MutableMapping[str, Render] = {}