Skip to content

Commit 821a58b

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

2 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/griffe2md/config.py

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

33
import logging
4-
import pathlib
5-
from typing import Optional
4+
from pathlib import Path
5+
from typing import Any
66

77
import tomllib
88

99
logger = logging.getLogger(__name__)
1010

1111
PATHS = (
12-
".config/griffe2md.toml",
13-
"config/griffe2md.toml",
12+
Path(".config/griffe2md.toml"),
13+
Path("config/griffe2md.toml"),
14+
Path("pyproject.toml"),
1415
)
1516

16-
PYPROJECT = "pyproject.toml"
1717

18-
19-
def _locate_config_file() -> Optional[pathlib.Path]:
18+
def _locate_config_file() -> Path | None:
2019
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
28-
20+
if path.is_file():
21+
return path
2922
return None
3023

3124

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:
25+
def load_config() -> dict[str, Any] | None:
26+
"""Load the configuration if config file or config entry in pyproject.toml exists.
27+
28+
If neither config file was found or pyproject.toml file doesn't have
29+
a `[tool.griffe2md]` section, None is returned.
30+
"""
31+
if not (config_path := _locate_config_file()):
3632
return None
3733

3834
logger.debug("Loading config from %s", config_path)
3935

40-
with open(config_path, "rb") as f:
36+
with config_path.open("rb") as f:
4137
config = tomllib.load(f)
4238

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

tests/test_config.py

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

@@ -16,7 +17,7 @@
1617
"pyproject.toml",
1718
],
1819
)
19-
def test_load_config(tmpdir, rel_path: str)->None: # noqa: ANN001
20+
def test_load_config(tmpdir, rel_path: str) -> None: # noqa: ANN001
2021
"""Test that config is loaded."""
2122
expected_config = {"dummy": True}
2223
config_text = "dummy=true"

0 commit comments

Comments
 (0)