diff --git a/CHANGES.rst b/CHANGES.rst index 19330fe..076390a 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,11 @@ Change Log ========== +5.0.1 (Unreleased) +================== + +- Support reading TOML files via ``tomllib`` on Python 3.11+ + 5.0.0 (13 August 2024) ======================== diff --git a/README.rst b/README.rst index 87a3aa7..8a11aec 100644 --- a/README.rst +++ b/README.rst @@ -28,7 +28,9 @@ Installation ``pip install goodconf`` or ``pip install goodconf[yaml]`` / ``pip install goodconf[toml]`` if parsing/generating YAML/TOML -files is required. +files is required. When running on Python 3.11+ the ``[toml]`` +extra is only required for generating TOML files as parsing +is supported natively. Quick Start diff --git a/goodconf/__init__.py b/goodconf/__init__.py index 8e32570..c2c87db 100644 --- a/goodconf/__init__.py +++ b/goodconf/__init__.py @@ -71,10 +71,16 @@ def _load_config(path: str) -> dict[str, Any]: yaml = ruamel.yaml.YAML(typ="safe", pure=True) loader = yaml.load elif ext == ".toml": - import tomlkit + try: + import tomllib + + def load(stream): + return tomllib.loads(f.read()) + except ImportError: # Fallback for Python < 3.11 + import tomlkit - def load(stream): - return tomlkit.load(f).unwrap() + def load(stream): + return tomlkit.load(f).unwrap() loader = load diff --git a/tests/test_file_helpers.py b/tests/test_file_helpers.py index 298af60..0bd363e 100644 --- a/tests/test_file_helpers.py +++ b/tests/test_file_helpers.py @@ -1,5 +1,5 @@ import os - +import sys import pytest from goodconf import _find_file, _load_config @@ -12,14 +12,16 @@ def test_json(tmpdir): def test_load_toml(tmpdir): - pytest.importorskip("tomlkit") + if sys.version_info < (3, 11): + pytest.importorskip("tomlkit") conf = tmpdir.join("conf.toml") conf.write('a = "b"\nc = 3') assert _load_config(str(conf)) == {"a": "b", "c": 3} def test_load_empty_toml(tmpdir): - pytest.importorskip("tomlkit") + if sys.version_info < (3, 11): + pytest.importorskip("tomlkit") conf = tmpdir.join("conf.toml") conf.write("") assert _load_config(str(conf)) == {}