Skip to content

Commit 4dfcc94

Browse files
committed
Fixed pytest issues for tomllib
1 parent 17f8c08 commit 4dfcc94

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

config/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
except ImportError: # pragma: no cover
1212
yaml = None
1313
try:
14-
import tomllib as toml
14+
import tomllib as toml # type: ignore [import-not-found,unused-ignore]
15+
1516
toml_readtype = "rb"
1617
except ImportError: # pragma: no cover
1718
try:
18-
import toml
19-
toml_readtype = "rt"
20-
except ImportError:
21-
toml = None
22-
toml_readtype = None
19+
import toml # type: ignore [no-redef,unused-ignore]
2320

21+
toml_readtype = "rt"
22+
except ImportError: # pragma: no cover
23+
toml = None # type: ignore [assignment,unused-ignore]
24+
toml_readtype = ""
2425

2526
from .configuration import Configuration
2627
from .configuration_set import ConfigurationSet
@@ -870,7 +871,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non
870871
with open(data, toml_readtype) as f:
871872
loaded = toml.load(f)
872873
else:
873-
loaded = toml.load(data)
874+
loaded = toml.load(data) # type: ignore [arg-type,unused-ignore]
874875
else:
875876
data = cast(str, data)
876877
loaded = toml.loads(data)

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)