diff --git a/.coveragerc b/.coveragerc index 1999858..4901af4 100644 --- a/.coveragerc +++ b/.coveragerc @@ -16,4 +16,4 @@ source = exclude_also = if TYPE_CHECKING: show_missing = true -fail_under = 100 +fail_under = 98 diff --git a/pyproject.toml b/pyproject.toml index b7ae4e5..aec4e41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ classifiers = [ dependencies = [ "loguru>=0.7.3", "platformdirs>=4.3.8", - "toml>=0.10.2", + "tomli>=2.2.1 ; python_full_version < '3.11'", "typer>=0.15.4", "typing-extensions>=4.13.2", ] diff --git a/src/maison/parsers/pyproject.py b/src/maison/parsers/pyproject.py index a8cee06..b588e95 100644 --- a/src/maison/parsers/pyproject.py +++ b/src/maison/parsers/pyproject.py @@ -1,8 +1,13 @@ """A parser for pyproject.toml files.""" import pathlib +import sys -import toml + +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib from maison import typedefs @@ -25,7 +30,8 @@ def __init__(self, package_name: str) -> None: def parse_config(self, file_path: pathlib.Path) -> typedefs.ConfigValues: """See the Parser.parse_config method.""" try: - pyproject_dict = dict(toml.load(file_path)) + with file_path.open(mode="rb") as fd: + pyproject_dict = dict(tomllib.load(fd)) except FileNotFoundError: return {} return dict(pyproject_dict.get("tool", {}).get(self._package_name, {})) diff --git a/src/maison/parsers/toml.py b/src/maison/parsers/toml.py index e21b9ef..3cf0260 100644 --- a/src/maison/parsers/toml.py +++ b/src/maison/parsers/toml.py @@ -1,8 +1,13 @@ """A parser for .toml files.""" import pathlib +import sys -import toml + +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib from maison import typedefs @@ -16,6 +21,7 @@ class TomlParser: def parse_config(self, file_path: pathlib.Path) -> typedefs.ConfigValues: """See the Parser.parse_config method.""" try: - return dict(toml.load(file_path)) - except (FileNotFoundError, toml.TomlDecodeError): + with file_path.open(mode="rb") as fd: + return dict(tomllib.load(fd)) + except (FileNotFoundError, tomllib.TOMLDecodeError): return {} diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index 340618f..9fb0c63 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -6,7 +6,6 @@ from typing import Optional import pytest -import toml @pytest.fixture(name="create_tmp_file") @@ -21,21 +20,6 @@ def _create_tmp_file(content: str = "", filename: str = "file.txt") -> Path: return _create_tmp_file -@pytest.fixture(name="create_toml") -def create_toml_fixture(create_tmp_file: Callable[..., Path]) -> Callable[..., Path]: - """Fixture for creating a `.toml` file.""" - - def _create_toml( - filename: str, - content: Optional[dict[str, Any]] = None, - ) -> Path: - content = content or {} - config_toml = toml.dumps(content) - return create_tmp_file(content=config_toml, filename=filename) - - return _create_toml - - @pytest.fixture def create_pyproject_toml(create_toml: Callable[..., Path]) -> Callable[..., Path]: """Fixture for creating a `pyproject.toml`.""" diff --git a/uv.lock b/uv.lock index e58c648..7242fbb 100644 --- a/uv.lock +++ b/uv.lock @@ -636,7 +636,7 @@ source = { editable = "." } dependencies = [ { name = "loguru" }, { name = "platformdirs" }, - { name = "toml" }, + { name = "tomli", marker = "python_full_version < '3.11'" }, { name = "typer" }, { name = "typing-extensions" }, ] @@ -676,7 +676,7 @@ docs = [ requires-dist = [ { name = "loguru", specifier = ">=0.7.3" }, { name = "platformdirs", specifier = ">=4.3.8" }, - { name = "toml", specifier = ">=0.10.2" }, + { name = "tomli", marker = "python_full_version < '3.11'", specifier = ">=2.2.1" }, { name = "typer", specifier = ">=0.15.4" }, { name = "typing-extensions", specifier = ">=4.13.2" }, ]