1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
From 8285cfc4e5fdc115ff94a8bac4e49015ca62f3db Mon Sep 17 00:00:00 2001
From: Alexandre Detiste <alexandre.detiste@gmail.com>
Date: Sat, 17 May 2025 17:44:02 +0200
Subject: [PATCH] use stdlib tomllib instead of standalone tomli on Python 3.11+
diff --git a/docs/scripts/gen_cli_data.py b/docs/scripts/gen_cli_data.py
index 1e4d6d80..195e35bf 100644
--- a/docs/scripts/gen_cli_data.py
+++ b/docs/scripts/gen_cli_data.py
@@ -15,7 +15,10 @@
from typing import Optional
from typing import Protocol
-import tomli
+try:
+ import tomllib
+except ImportError:
+ import tomli as tomllib
import tomli_w
sys.path.append(Path(__file__).parent.as_posix())
@@ -43,7 +46,7 @@ class Command(NamedTuple):
def add_config_bogus_defaults(output: str) -> str:
"""Give bogus defaults to certain config values."""
- config = tomli.loads(output)
+ config = tomllib.loads(output)
# TODO: replace local username with a default value
out = tomli_w.dumps(config)
out = add_path_placeholders(out)
diff --git a/pyproject.toml b/pyproject.toml
index 5fe89d2d..d28e1a0a 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -40,7 +40,7 @@ dependencies = [
"httpx[socks]>=0.28.1",
"packaging>=22.0",
"pydantic>=2.7.0",
- "tomli>=2.0.1",
+ "tomli>=2.0.1; python_version < '3.11'",
"tomli-w>=1.0.0",
"platformdirs>=2.5.4",
"strenum>=0.4.15",
diff --git a/tests/test_config.py b/tests/test_config.py
index a4411101..a3eb7b80 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -6,7 +6,10 @@
from typing import Union
import pytest
-import tomli
+try:
+ import tomllib
+except:
+ import tomli as tomllib
from inline_snapshot import snapshot
from pydantic import BaseModel
from pydantic import Field
@@ -285,7 +288,7 @@ def test_config_dump_to_file_masked(tmp_path: Path) -> None:
conf.dump_to_file(conf_file, secrets=SecretMode.MASK)
toml_str = conf_file.read_text()
- config_dict = tomli.loads(toml_str)
+ config_dict = tomllib.loads(toml_str)
assert config_dict["api"]["password"] == snapshot("**********")
assert config_dict["api"]["auth_token"] == snapshot("**********")
diff --git a/zabbix_cli/config/utils.py b/zabbix_cli/config/utils.py
index adf8068c..8f4ad065 100644
--- a/zabbix_cli/config/utils.py
+++ b/zabbix_cli/config/utils.py
@@ -21,11 +21,14 @@
def load_config_toml(filename: Path) -> dict[str, Any]:
"""Load a TOML configuration file."""
- import tomli
+ try:
+ import tomllib
+ except ImportError:
+ import tomli as tomllib
try:
- return tomli.loads(filename.read_text())
- except tomli.TOMLDecodeError as e:
+ return tomllib.loads(filename.read_text())
+ except tomllib.TOMLDecodeError as e:
raise ConfigError(f"Error decoding TOML file {filename}: {e}") from e
except OSError as e:
raise ConfigError(f"Error reading TOML file {filename}: {e}") from e
|