diff --git a/config/__init__.py b/config/__init__.py index 9d55c80..0e2393b 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -2,6 +2,7 @@ import json import os +import sys from importlib.abc import InspectLoader from types import ModuleType from typing import Any, Dict, Iterable, List, Mapping, Optional, TextIO, Union, cast @@ -10,10 +11,19 @@ import yaml except ImportError: # pragma: no cover yaml = None -try: - import toml -except ImportError: # pragma: no cover - toml = None + +if sys.version_info[1] < 11: + try: + import toml + + toml_readtype = "rt" + except ImportError: # pragma: no cover + toml = None + toml_readtype = "" +else: + import tomllib as toml + + toml_readtype = "rb" from .configuration import Configuration @@ -861,10 +871,10 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non """Reload the TOML data.""" if read_from_file: if isinstance(data, str): - with open(data, "rt") as f: + with open(data, toml_readtype) as f: loaded = toml.load(f) else: - loaded = toml.load(data) + loaded = toml.load(data) # type: ignore [arg-type,unused-ignore] else: data = cast(str, data) loaded = toml.loads(data) diff --git a/tests/contrib/test_azure.py b/tests/contrib/test_azure.py index 5a62f5c..1554e35 100644 --- a/tests/contrib/test_azure.py +++ b/tests/contrib/test_azure.py @@ -42,7 +42,7 @@ def get_secret(self, key: str) -> FakeKeySecret: if key in self._dict: return FakeKeySecret(key, self._dict[key]) else: - raise ResourceNotFoundError() # type: ignore + raise ResourceNotFoundError() def list_properties_of_secrets(self) -> list: return [Secret(name=k, value=v) for k, v in self._dict.items()] diff --git a/tests/test_toml.py b/tests/test_toml.py index bcad7a1..2d6a1aa 100644 --- a/tests/test_toml.py +++ b/tests/test_toml.py @@ -1,9 +1,12 @@ +import tempfile + import pytest + from config import config_from_dict -import tempfile try: import toml + from config import config_from_toml except ImportError: toml = None @@ -75,7 +78,16 @@ def test_load_toml_file(): # type: ignore with tempfile.NamedTemporaryFile() as f: f.file.write(TOML.encode()) f.file.flush() - cfg = config_from_toml(open(f.name, "rt"), read_from_file=True) + + # Two different toml libraries are in use + import sys + + if sys.version_info[1] < 11: + read_type = "rt" + else: + read_type = "rb" + + cfg = config_from_toml(open(f.name, read_type), read_from_file=True) assert cfg["a1.b1.c1"] == 1 assert cfg["a1.b1"].as_dict() == {"c1": 1, "c2": 2, "c3": 3} assert cfg["a1.b2"].as_dict() == {"c1": "a", "c2": True, "c3": 1.1} @@ -132,13 +144,15 @@ def test_reload_toml_with_section_prefix(): # type: ignore f.file.flush() cfg = config_from_toml( - f.name, section_prefix="tool.coverage.", read_from_file=True + f.name, + section_prefix="tool.coverage.", + read_from_file=True, ) expected = config_from_dict( { "run.branch": False, "run.parallel": False, - } + }, ) assert cfg == expected @@ -151,6 +165,6 @@ def test_reload_toml_with_section_prefix(): # type: ignore expected = config_from_dict( { "report.ignore_errors": False, - } + }, ) assert cfg == expected