From 5dd022d1641508dbdbea0998316fcdfde3ad10fc Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Thu, 23 Oct 2025 18:49:43 +0400 Subject: [PATCH 1/5] Simplify typing and defer typing imports --- mdformat_pyproject/plugin.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index ba018eb..a2a540e 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -1,15 +1,21 @@ """Main plugin module.""" -import pathlib +from __future__ import annotations + import sys -from typing import TYPE_CHECKING, MutableMapping, Optional, Sequence, Tuple, Union +from pathlib import Path import markdown_it import mdformat +TYPE_CHECKING = False if TYPE_CHECKING: + from collections.abc import Sequence + from mdformat.renderer.typing import Render + _ConfigOptions = dict[str, int | str | Sequence[str]] + if sys.version_info >= (3, 11): import tomllib else: @@ -23,11 +29,8 @@ cache = lru_cache() -_ConfigOptions = MutableMapping[str, Union[int, str, Sequence[str]]] - - @cache -def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Path]: +def _find_pyproject_toml_path(search_path: Path) -> Path | None: """Find the pyproject.toml file that applies to the search path. The search is done ascending through the folders tree until a pyproject.toml @@ -45,7 +48,7 @@ def _find_pyproject_toml_path(search_path: pathlib.Path) -> Optional[pathlib.Pat @cache -def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]: +def _parse_pyproject(pyproject_path: Path) -> _ConfigOptions | None: """Extract and validate the mdformat options from the pyproject.toml file. The options are searched inside a [tool.mdformat] key within the toml file, @@ -63,7 +66,7 @@ def _parse_pyproject(pyproject_path: pathlib.Path) -> Optional[_ConfigOptions]: @cache -def read_toml_opts(conf_dir: pathlib.Path) -> Tuple[MutableMapping, Optional[pathlib.Path]]: +def patched_read_toml_opts(conf_dir: Path) -> tuple[dict, Path | None]: """Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml. Notice that if `.mdformat.toml` exists it is ignored. @@ -82,7 +85,7 @@ def update_mdit(mdit: markdown_it.MarkdownIt) -> None: pass -RENDERERS: MutableMapping[str, "Render"] = {} +RENDERERS: MutableMapping[str, Render] = {} # Monkey patch mdformat._conf to use our own read_toml_opts version mdformat._conf.read_toml_opts = read_toml_opts From e4af997c329a8c1abbe4974af201e05ce804952f Mon Sep 17 00:00:00 2001 From: Carles Sala Date: Mon, 27 Oct 2025 17:10:49 +0100 Subject: [PATCH 2/5] Update reference to read_toml_opts that had not been changed --- mdformat_pyproject/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index f3c20af..66aae1f 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -82,4 +82,4 @@ def update_mdit(mdit: markdown_it.MarkdownIt) -> None: RENDERERS: MutableMapping[str, Render] = {} # Monkey patch mdformat._conf to use our own read_toml_opts version -mdformat._conf.read_toml_opts = read_toml_opts +mdformat._conf.read_toml_opts = patched_read_toml_opts From e4aaa7b472d5dbe811f8ca38b2a47ca33e8e7601 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Mon, 27 Oct 2025 20:20:21 +0400 Subject: [PATCH 3/5] Fix typing import and local patched function name --- mdformat_pyproject/plugin.py | 2 +- tests/test_plugin.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index 66aae1f..9907d88 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -11,7 +11,7 @@ TYPE_CHECKING = False if TYPE_CHECKING: - from collections.abc import Sequence + from collections.abc import MutableMapping, Sequence from mdformat.renderer.typing import Render diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 1a2e911..e359b49 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -89,7 +89,7 @@ def test_read_toml_opts_with_pyproject(): - Path to the pyproject.toml file """ # run - opts, path = plugin.read_toml_opts(THIS_MODULE_PATH) + opts, path = plugin.patched_read_toml_opts(THIS_MODULE_PATH) # assert assert opts == {"wrap": 99, "number": True, "exclude": [".tox/**", ".venv/**"]} @@ -107,7 +107,7 @@ def test_read_toml_opts_without_pyproject(nonexistent_path): - None """ # run - opts, path = plugin.read_toml_opts(nonexistent_path) + opts, path = plugin.patched_read_toml_opts(nonexistent_path) # assert assert opts == {} From 00074b1294147fe8d59f6fe2a5edc30fe0b776e6 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Mon, 27 Oct 2025 20:22:21 +0400 Subject: [PATCH 4/5] Keep changes minimal --- mdformat_pyproject/plugin.py | 4 ++-- tests/test_plugin.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index 9907d88..486d68d 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -60,7 +60,7 @@ def _parse_pyproject(pyproject_path: Path) -> _ConfigOptions | None: @cache -def patched_read_toml_opts(conf_dir: Path) -> tuple[dict, Path | None]: +def read_toml_opts(conf_dir: Path) -> tuple[dict, Path | None]: """Alternative read_toml_opts that reads from pyproject.toml instead of .mdformat.toml. Notice that if `.mdformat.toml` exists it is ignored. @@ -82,4 +82,4 @@ def update_mdit(mdit: markdown_it.MarkdownIt) -> None: RENDERERS: MutableMapping[str, Render] = {} # Monkey patch mdformat._conf to use our own read_toml_opts version -mdformat._conf.read_toml_opts = patched_read_toml_opts +mdformat._conf.read_toml_opts = read_toml_opts diff --git a/tests/test_plugin.py b/tests/test_plugin.py index e359b49..1a2e911 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -89,7 +89,7 @@ def test_read_toml_opts_with_pyproject(): - Path to the pyproject.toml file """ # run - opts, path = plugin.patched_read_toml_opts(THIS_MODULE_PATH) + opts, path = plugin.read_toml_opts(THIS_MODULE_PATH) # assert assert opts == {"wrap": 99, "number": True, "exclude": [".tox/**", ".venv/**"]} @@ -107,7 +107,7 @@ def test_read_toml_opts_without_pyproject(nonexistent_path): - None """ # run - opts, path = plugin.patched_read_toml_opts(nonexistent_path) + opts, path = plugin.read_toml_opts(nonexistent_path) # assert assert opts == {} From 3b04bd386733ab45b2f4f1a61f22b9ff1bdf7b2e Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Mon, 27 Oct 2025 20:23:34 +0400 Subject: [PATCH 5/5] Remove extra type --- mdformat_pyproject/plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mdformat_pyproject/plugin.py b/mdformat_pyproject/plugin.py index 486d68d..5ad664c 100644 --- a/mdformat_pyproject/plugin.py +++ b/mdformat_pyproject/plugin.py @@ -11,7 +11,7 @@ TYPE_CHECKING = False if TYPE_CHECKING: - from collections.abc import MutableMapping, Sequence + from collections.abc import Sequence from mdformat.renderer.typing import Render @@ -79,7 +79,7 @@ def update_mdit(mdit: markdown_it.MarkdownIt) -> None: pass -RENDERERS: MutableMapping[str, Render] = {} +RENDERERS: dict[str, Render] = {} # Monkey patch mdformat._conf to use our own read_toml_opts version mdformat._conf.read_toml_opts = read_toml_opts