Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't convert e.g. log_level = "DEBUG" to 10 in TOML config file #78

Merged
merged 2 commits into from
Oct 5, 2024
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ These features will be included in the next release:

Added
-----
- Unit test for ``log_level`` option for `darkgraylib.config.load_config`.

Fixed
-----
- Keep Pylint below version 3.3.0 until we drop support for Python 3.8.
- Don't convert ``log_level = "<string>"`` in the configuration file to an integer.


2.0.1_ - 2024-08-09
Expand Down
2 changes: 1 addition & 1 deletion src/darkgraylib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def validate_config_keys(

def replace_log_level_name(config: BaseConfig) -> None:
"""Replace numeric log level in configuration with the name of the log level"""
if "log_level" in config:
if "log_level" in config and isinstance(config["log_level"], int):
config["log_level"] = logging.getLevelName(config["log_level"])


Expand Down
40 changes: 38 additions & 2 deletions src/darkgraylib/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from textwrap import dedent

import pytest
from toml import TomlDecodeError

from darkgraylib.config import (
ConfigurationError,
Expand Down Expand Up @@ -102,7 +103,7 @@ class OriginTrackingConfig(BaseConfig):
dict(
srcs=["full_example/full.py"],
expect={
"log_level": 10,
"log_level": 'DEBUG',
"revision": "main",
"src": ["src", "tests"],
},
Expand Down Expand Up @@ -351,7 +352,7 @@ class OriginTrackingConfig(BaseConfig):
confpath=None,
expect={"config": "no_pyp"},
)
def test_load_config( # pylint: disable=too-many-arguments
def test_load_config_path( # pylint: disable=too-many-arguments
tmp_path, monkeypatch, srcs, cwd, confpath, expect
):
"""``load_config()`` finds and loads configuration based on source file paths"""
Expand Down Expand Up @@ -513,3 +514,38 @@ def test_dump_config(config, expect):
result = dump_config(config, "darkgraylib")

assert result == expect


@pytest.mark.parametrize(
["config", "expect"],
[
("", {}),
("log_level = 0", {"log_level": "NOTSET"}),
("log_level = 'NOTSET'", {"log_level": "NOTSET"}),
("log_level = 1", {"log_level": "Level 1"}),
("log_level = 2", {"log_level": "Level 2"}),
("log_level = 10", {"log_level": "DEBUG"}),
("log_level = 'DEBUG'", {"log_level": "DEBUG"}),
("log_level = DEBUG", TomlDecodeError),
("log_level = 20", {"log_level": "INFO"}),
("log_level = 'INFO'", {"log_level": "INFO"}),
("log_level = 30", {"log_level": "WARNING"}),
("log_level = 'WARNING'", {"log_level": "WARNING"}),
("log_level = 40", {"log_level": "ERROR"}),
("log_level = 'ERROR'", {"log_level": "ERROR"}),
("log_level = 50", {"log_level": "CRITICAL"}),
("log_level = 'CRITICAL'", {"log_level": "CRITICAL"}),
],
)
def test_load_config(tmp_path, monkeypatch, config, expect):
"""`load_config()` can load all supported options"""
(tmp_path / "pyproject.toml").write_text(f"[tool.darkgraylib]\n{config}\n")
monkeypatch.chdir(tmp_path)
with raises_if_exception(expect):

result = load_config(None, ["."], "darkgraylib", BaseConfig)

assert result == expect
assert {k: type(v) for k, v in result.items()} == {
k: type(v) for k, v in expect.items()
}
Loading