Skip to content

Commit 56a1f31

Browse files
committed
fixup! feat: Read config from config file or pyproject.toml
1 parent 37ab2a3 commit 56a1f31

6 files changed

Lines changed: 41 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88
<!-- insertion marker -->
9-
## [unreleased]
10-
11-
### Features
12-
13-
- Read configuration from a config file or pyproject.toml .
14-
159
## [1.1.0](https://github.com/mkdocstrings/griffe2md/releases/tag/1.1.0) - 2025-02-11
1610

1711
<small>[Compare with 1.0.2](https://github.com/mkdocstrings/griffe2md/compare/1.0.2...1.1.0)</small>

duties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def coverage(ctx: Context) -> None:
185185

186186

187187
@duty
188-
def test(ctx: Context, *cli_args: str, match: str = "") -> None:
188+
def test(ctx: Context, *cli_args: str, match: str = "") -> None: # noqa: PT028
189189
"""Run the test suite.
190190
191191
Parameters:

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ dependencies = [
3232
"griffe>=0.49",
3333
"jinja2>=3.1.2",
3434
"mdformat>=0.7.16",
35+
# YORE: EOL 3.10: Remove line.
36+
"tomli>=2.0; python_version < '3.11'",
3537
]
3638

3739
[project.urls]

src/griffe2md/config.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
"""Load configuration."""
22

3+
from __future__ import annotations
4+
35
import logging
4-
import pathlib
5-
from typing import Optional
6+
import sys
7+
from pathlib import Path
8+
from typing import Any
69

7-
import tomllib
10+
# YORE: EOL 3.10: Replace block with line 2.
11+
if sys.version_info >= (3, 11):
12+
import tomllib
13+
else:
14+
import tomli as tomllib
815

916
logger = logging.getLogger(__name__)
1017

11-
PATHS = (
12-
".config/griffe2md.toml",
13-
"config/griffe2md.toml",
18+
CONFIG_FILE_PATHS = (
19+
Path(".config/griffe2md.toml"),
20+
Path("config/griffe2md.toml"),
21+
Path("pyproject.toml"),
1422
)
1523

16-
PYPROJECT = "pyproject.toml"
17-
18-
19-
def _locate_config_file() -> Optional[pathlib.Path]:
20-
for path in PATHS:
21-
path_ = pathlib.Path(path)
22-
if path_.is_file():
23-
return path_
24-
25-
pyproj_path = pathlib.Path(PYPROJECT)
26-
if pyproj_path.is_file():
27-
return pyproj_path
2824

25+
def _locate_config_file() -> Path | None:
26+
for path in CONFIG_FILE_PATHS:
27+
if path.is_file():
28+
return path
2929
return None
3030

3131

32-
def load_config() -> Optional[dict]:
33-
"""Load the configuration if config file or config entry in pyproject.toml exists. If neither config file was found or pyproject.toml file doesn't have [tool.griffe2md] section, returns None."""
34-
config_path = _locate_config_file()
35-
if config_path is None:
32+
def load_config() -> dict[str, Any] | None:
33+
"""Load the configuration if config file or config entry in pyproject.toml exists.
34+
35+
If neither config file was found or pyproject.toml file doesn't have
36+
a `[tool.griffe2md]` section, None is returned.
37+
"""
38+
if not (config_path := _locate_config_file()):
3639
return None
3740

3841
logger.debug("Loading config from %s", config_path)
3942

40-
with open(config_path, "rb") as f:
43+
with config_path.open("rb") as f:
4144
config = tomllib.load(f)
4245

43-
if str(config_path) == PYPROJECT:
44-
return (config.get("tool") or {}).get("griffe2md")
46+
if config_path.name == "pyproject.toml":
47+
return config.get("tool", {}).get("griffe2md", None)
4548
return config

src/griffe2md/rendering.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def do_format_attribute(
284284
def do_order_members(
285285
members: Sequence[Object | Alias],
286286
order: Order,
287-
members_list: bool | list[str] | None,
287+
members_list: bool | list[str] | None, # noqa: FBT001
288288
) -> Sequence[Object | Alias]:
289289
"""Order members given an ordering method.
290290
@@ -415,7 +415,7 @@ def do_filter_objects(
415415
@lru_cache(maxsize=1)
416416
def _get_black_formatter() -> Callable[[str, int], str]:
417417
try:
418-
from black import InvalidInput, Mode, format_str
418+
from black import InvalidInput, Mode, format_str # noqa: PLC0415
419419
except ModuleNotFoundError:
420420
logger.info("Formatting signatures requires Black to be installed.")
421421
return lambda text, _: text

tests/test_config.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
"""Test config loading."""
2+
23
from pathlib import Path
34
from unittest.mock import Mock, patch
45

56
import pytest
67

78
import griffe2md.cli
8-
from griffe2md.config import PYPROJECT
9-
10-
11-
@pytest.mark.parametrize(
12-
"rel_path",
13-
[
14-
".config/griffe2md.toml",
15-
"config/griffe2md.toml",
16-
"pyproject.toml",
17-
],
18-
)
19-
def test_load_config(tmpdir, rel_path: str)->None: # noqa: ANN001
9+
from griffe2md.config import CONFIG_FILE_PATHS
10+
11+
12+
@pytest.mark.parametrize("rel_path", CONFIG_FILE_PATHS)
13+
def test_load_config(tmpdir: Path, rel_path: Path) -> None:
2014
"""Test that config is loaded."""
2115
expected_config = {"dummy": True}
2216
config_text = "dummy=true"
2317

2418
mock_write = Mock()
2519

26-
with tmpdir.as_cwd(), patch("griffe2md.cli.write_package_docs", mock_write):
27-
text = f"[tool.griffe2md]\n{config_text}" if rel_path == PYPROJECT else config_text
20+
with tmpdir.as_cwd(), patch("griffe2md.cli.write_package_docs", mock_write): # type: ignore[attr-defined]
21+
text = f"[tool.griffe2md]\n{config_text}" if rel_path.name == "pyproject.toml" else config_text
2822
config_path = Path(tmpdir) / rel_path
2923
config_path.parent.mkdir(parents=True, exist_ok=True)
3024
config_path.write_text(text, "utf-8")

0 commit comments

Comments
 (0)