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

typing fixes #8

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions mdformat_pyproject/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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
jorenham marked this conversation as resolved.
Show resolved Hide resolved


@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
Expand Down Expand Up @@ -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] = {}
Loading