From 27d5d601ae2f9b3801f93293d7ca91763b345216 Mon Sep 17 00:00:00 2001 From: Austin DeLauney Date: Sat, 17 Feb 2024 10:31:52 +0800 Subject: [PATCH 1/3] Use tomllib over toml for Python 3.11+ --- config/__init__.py | 22 ++++++++++++++++------ tests/test_toml.py | 24 +++++++++++++++++++----- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index 9d55c80..65822ad 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -3,6 +3,7 @@ import json import os from importlib.abc import InspectLoader +import sys 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 # type: ignore [no-redef,unused-ignore] + + toml_readtype = "rt" + except ImportError: # pragma: no cover + toml = None # type: ignore [assignment,unused-ignore] + toml_readtype = "" +else: + import tomllib as toml # type: ignore [import-not-found,unused-ignore] + + 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/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 From 70c36e632209e8e5bcc3829ddcf4a49bc06d06ba Mon Sep 17 00:00:00 2001 From: Austin DeLauney Date: Sat, 17 Feb 2024 10:41:33 +0800 Subject: [PATCH 2/3] Removed previous pytest exclusions, and ruff'ed --- config/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/__init__.py b/config/__init__.py index 65822ad..0e2393b 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -2,8 +2,8 @@ import json import os -from importlib.abc import InspectLoader import sys +from importlib.abc import InspectLoader from types import ModuleType from typing import Any, Dict, Iterable, List, Mapping, Optional, TextIO, Union, cast @@ -14,14 +14,14 @@ if sys.version_info[1] < 11: try: - import toml # type: ignore [no-redef,unused-ignore] + import toml toml_readtype = "rt" except ImportError: # pragma: no cover - toml = None # type: ignore [assignment,unused-ignore] + toml = None toml_readtype = "" else: - import tomllib as toml # type: ignore [import-not-found,unused-ignore] + import tomllib as toml toml_readtype = "rb" From 8c00b802933115e80e075e5337a0278eae546de4 Mon Sep 17 00:00:00 2001 From: Austin DeLauney Date: Sun, 18 Feb 2024 23:46:24 +0800 Subject: [PATCH 3/3] Removed unnessecary ignore from azure tests --- tests/contrib/test_azure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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()]