Skip to content

Commit af9572b

Browse files
committed
Drop dependency on the toml package
Since we require Python 3.11 or later, we can use the built-in tomllib module instead. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 2215f8b commit af9572b

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ dependencies = [
3838
"numpy >= 1.24.2, < 2",
3939
"protobuf >= 4.21.6, < 5",
4040
"pydantic >= 1.9, < 2",
41-
"toml >= 0.10",
4241
"tqdm >= 4.38.0, < 5",
4342
"typing_extensions >= 4.4.0, < 5",
4443
"watchfiles >= 0.15.0",

src/frequenz/sdk/actor/_config_managing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import logging
77
import os
8+
import tomllib
89
from typing import Any, Dict, Optional, Set
910

10-
import toml
1111
from frequenz.channels import Sender
1212
from frequenz.channels.util import FileWatcher
1313

@@ -58,7 +58,8 @@ def _read_config(self) -> Dict[str, Any]:
5858
A dictionary containing configuration variables.
5959
"""
6060
try:
61-
return toml.load(self._conf_file)
61+
with open(self._conf_file, "rb") as toml_file:
62+
return tomllib.load(toml_file)
6263
except ValueError as err:
6364
logging.error("Can't read config file, err: %s", err)
6465
raise

tests/config/test_config.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"""Test for Config"""
55
import pathlib
66
import re
7+
8+
# pylint: disable = no-name-in-module
9+
import tomllib
710
from typing import Any, Dict, List, Optional, Set
811

912
import pytest
10-
11-
# pylint: disable = no-name-in-module
12-
import toml
1313
from pydantic import BaseModel, StrictBool, StrictFloat, StrictInt
1414

1515
from frequenz.sdk.config import Config
@@ -54,29 +54,32 @@ def config_file(self, tmp_path: pathlib.Path) -> pathlib.Path:
5454
file_path.write_text(TestConfig.conf_content)
5555
return file_path
5656

57-
def test_get(self, config_file: pathlib.Path) -> None:
57+
@pytest.fixture()
58+
def conf_vars(self, config_file: pathlib.Path) -> dict[str, Any]:
59+
"""Load the created test config file."""
60+
with config_file.open("rb") as file:
61+
return tomllib.load(file)
62+
63+
def test_get(self, conf_vars: dict[str, Any]) -> None:
5864
"""Test get function"""
59-
conf_vars = toml.load(config_file)
6065
config = Config(conf_vars=conf_vars)
6166

6267
assert config.get("logging_lvl") == "DEBUG"
6368
assert config.get("var1") == "1"
6469
assert config.get("var2") is None
6570
assert config.get("var2", default=0) == 0
6671

67-
def test_getitem(self, config_file: pathlib.Path) -> None:
72+
def test_getitem(self, conf_vars: dict[str, Any]) -> None:
6873
"""Test getitem function"""
69-
conf_vars = toml.load(config_file)
7074
config = Config(conf_vars=conf_vars)
7175

7276
assert config["logging_lvl"] == "DEBUG"
7377
assert config["var1"] == "1"
7478
with pytest.raises(KeyError, match="Unknown config name var2"):
7579
assert config["var2"]
7680

77-
def test_contains(self, config_file: pathlib.Path) -> None:
81+
def test_contains(self, conf_vars: dict[str, Any]) -> None:
7882
"""Test contains function"""
79-
conf_vars = toml.load(config_file)
8083
config = Config(conf_vars=conf_vars)
8184

8285
assert "logging_lvl" in config
@@ -111,11 +114,10 @@ def test_contains(self, config_file: pathlib.Path) -> None:
111114
],
112115
)
113116
def test_get_as_success(
114-
self, key: str, expected_type: Any, value: Any, config_file: pathlib.Path
117+
self, key: str, expected_type: Any, value: Any, conf_vars: dict[str, Any]
115118
) -> None:
116119
"""Test get_as function with proper arguments"""
117120

118-
conf_vars = toml.load(config_file)
119121
config = Config(conf_vars=conf_vars)
120122
result = config.get_as(key, expected_type)
121123
assert result == value
@@ -132,10 +134,9 @@ def test_get_as_success(
132134
],
133135
)
134136
def test_get_as_validation_error(
135-
self, key: str, expected_type: Any, config_file: pathlib.Path
137+
self, key: str, expected_type: Any, conf_vars: dict[str, Any]
136138
) -> None:
137139
"""Test get_as function which raise ValidationError"""
138-
conf_vars = toml.load(config_file)
139140
config = Config(conf_vars=conf_vars)
140141

141142
err_msg = (
@@ -158,11 +159,10 @@ def test_get_dict_values_success(
158159
key_prefix: str,
159160
expected_values_type: Any,
160161
value: Any,
161-
config_file: pathlib.Path,
162+
conf_vars: dict[str, Any],
162163
) -> None:
163164
"""Test get_as function with proper arguments"""
164165

165-
conf_vars = toml.load(config_file)
166166
config = Config(conf_vars=conf_vars)
167167
result = config.get_dict(key_prefix, expected_values_type)
168168
assert result == value
@@ -176,11 +176,10 @@ def test_get_dict_values_success(
176176
],
177177
)
178178
def test_get_dict_success(
179-
self, key_prefix: str, expected_values_type: Any, config_file: pathlib.Path
179+
self, key_prefix: str, expected_values_type: Any, conf_vars: dict[str, Any]
180180
) -> None:
181181
"""Test get_as function with proper arguments"""
182182

183-
conf_vars = toml.load(config_file)
184183
config = Config(conf_vars=conf_vars)
185184

186185
err_msg_re = (

0 commit comments

Comments
 (0)