Skip to content

Commit 2f28254

Browse files
authored
Use tomllib over toml for Python 3.11+ (#90)
* Use tomllib over toml for Python 3.11+ * Removed previous pytest exclusions, and ruff'ed * Removed unnessecary ignore from azure tests
1 parent 32778bb commit 2f28254

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

config/__init__.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
import sys
56
from importlib.abc import InspectLoader
67
from types import ModuleType
78
from typing import Any, Dict, Iterable, List, Mapping, Optional, TextIO, Union, cast
@@ -10,10 +11,19 @@
1011
import yaml
1112
except ImportError: # pragma: no cover
1213
yaml = None
13-
try:
14-
import toml
15-
except ImportError: # pragma: no cover
16-
toml = None
14+
15+
if sys.version_info[1] < 11:
16+
try:
17+
import toml
18+
19+
toml_readtype = "rt"
20+
except ImportError: # pragma: no cover
21+
toml = None
22+
toml_readtype = ""
23+
else:
24+
import tomllib as toml
25+
26+
toml_readtype = "rb"
1727

1828

1929
from .configuration import Configuration
@@ -861,10 +871,10 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
861871
"""Reload the TOML data."""
862872
if read_from_file:
863873
if isinstance(data, str):
864-
with open(data, "rt") as f:
874+
with open(data, toml_readtype) as f:
865875
loaded = toml.load(f)
866876
else:
867-
loaded = toml.load(data)
877+
loaded = toml.load(data) # type: ignore [arg-type,unused-ignore]
868878
else:
869879
data = cast(str, data)
870880
loaded = toml.loads(data)

tests/contrib/test_azure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_secret(self, key: str) -> FakeKeySecret:
4242
if key in self._dict:
4343
return FakeKeySecret(key, self._dict[key])
4444
else:
45-
raise ResourceNotFoundError() # type: ignore
45+
raise ResourceNotFoundError()
4646

4747
def list_properties_of_secrets(self) -> list:
4848
return [Secret(name=k, value=v) for k, v in self._dict.items()]

tests/test_toml.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import tempfile
2+
13
import pytest
4+
25
from config import config_from_dict
3-
import tempfile
46

57
try:
68
import toml
9+
710
from config import config_from_toml
811
except ImportError:
912
toml = None
@@ -75,7 +78,16 @@ def test_load_toml_file(): # type: ignore
7578
with tempfile.NamedTemporaryFile() as f:
7679
f.file.write(TOML.encode())
7780
f.file.flush()
78-
cfg = config_from_toml(open(f.name, "rt"), read_from_file=True)
81+
82+
# Two different toml libraries are in use
83+
import sys
84+
85+
if sys.version_info[1] < 11:
86+
read_type = "rt"
87+
else:
88+
read_type = "rb"
89+
90+
cfg = config_from_toml(open(f.name, read_type), read_from_file=True)
7991
assert cfg["a1.b1.c1"] == 1
8092
assert cfg["a1.b1"].as_dict() == {"c1": 1, "c2": 2, "c3": 3}
8193
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
132144
f.file.flush()
133145

134146
cfg = config_from_toml(
135-
f.name, section_prefix="tool.coverage.", read_from_file=True
147+
f.name,
148+
section_prefix="tool.coverage.",
149+
read_from_file=True,
136150
)
137151
expected = config_from_dict(
138152
{
139153
"run.branch": False,
140154
"run.parallel": False,
141-
}
155+
},
142156
)
143157

144158
assert cfg == expected
@@ -151,6 +165,6 @@ def test_reload_toml_with_section_prefix(): # type: ignore
151165
expected = config_from_dict(
152166
{
153167
"report.ignore_errors": False,
154-
}
168+
},
155169
)
156170
assert cfg == expected

0 commit comments

Comments
 (0)