Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/contrib/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down
24 changes: 19 additions & 5 deletions tests/test_toml.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -151,6 +165,6 @@ def test_reload_toml_with_section_prefix(): # type: ignore
expected = config_from_dict(
{
"report.ignore_errors": False,
}
},
)
assert cfg == expected