Skip to content

Commit 14c924e

Browse files
committed
refactor(TomlConfig): minor cleanups for DX
1 parent cc981fc commit 14c924e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

commitizen/config/toml_config.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import os
44
from pathlib import Path
5-
from typing import TYPE_CHECKING, Any
5+
from typing import TYPE_CHECKING
66

7-
from tomlkit import exceptions, parse, table
7+
from tomlkit import TOMLDocument, exceptions, parse, table
88

99
from commitizen.exceptions import InvalidConfigurationError
1010

@@ -28,30 +28,31 @@ def __init__(self, *, data: bytes | str, path: Path | str) -> None:
2828
self._parse_setting(data)
2929

3030
def init_empty_config_content(self) -> None:
31+
config_doc = TOMLDocument()
3132
if os.path.isfile(self.path):
3233
with open(self.path, "rb") as input_toml_file:
33-
parser = parse(input_toml_file.read())
34-
else:
35-
parser = parse("")
34+
config_doc = parse(input_toml_file.read())
35+
36+
if config_doc.get("tool") is None:
37+
config_doc["tool"] = table()
38+
config_doc["tool"]["commitizen"] = table() # type: ignore[index]
3639

3740
with open(self.path, "wb") as output_toml_file:
38-
if parser.get("tool") is None:
39-
parser["tool"] = table()
40-
parser["tool"]["commitizen"] = table() # type: ignore[index]
41-
output_toml_file.write(parser.as_string().encode(self.encoding))
41+
output_toml_file.write(config_doc.as_string().encode(self.encoding))
4242

43-
def set_key(self, key: str, value: Any) -> Self:
43+
def set_key(self, key: str, value: object) -> Self:
4444
"""Set or update a key in the conf.
4545
4646
For now only strings are supported.
4747
We use to update the version number.
4848
"""
4949
with open(self.path, "rb") as f:
50-
parser = parse(f.read())
50+
config_doc = parse(f.read())
5151

52-
parser["tool"]["commitizen"][key] = value # type: ignore[index]
52+
config_doc["tool"]["commitizen"][key] = value # type: ignore[index]
5353
with open(self.path, "wb") as f:
54-
f.write(parser.as_string().encode(self.encoding))
54+
f.write(config_doc.as_string().encode(self.encoding))
55+
5556
return self
5657

5758
def _parse_setting(self, data: bytes | str) -> None:

0 commit comments

Comments
 (0)