diff --git a/commitizen/config/base_config.py b/commitizen/config/base_config.py index 4b8f5f05f..272a38574 100644 --- a/commitizen/config/base_config.py +++ b/commitizen/config/base_config.py @@ -1,7 +1,7 @@ from __future__ import annotations from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING from commitizen.defaults import DEFAULT_SETTINGS, Settings @@ -33,11 +33,10 @@ def path(self) -> Path: def path(self, path: str | Path) -> None: self._path = Path(path) - def set_key(self, key: str, value: Any) -> Self: - """Set or update a key in the conf. + def set_key(self, key: str, value: object) -> Self: + """Set or update a key in the config file. - For now only strings are supported. - We use to update the version number. + Currently, only strings are supported for the parameter key. """ raise NotImplementedError() diff --git a/commitizen/config/json_config.py b/commitizen/config/json_config.py index be1f1c36b..a117209f8 100644 --- a/commitizen/config/json_config.py +++ b/commitizen/config/json_config.py @@ -2,7 +2,7 @@ import json from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING from commitizen.exceptions import InvalidConfigurationError from commitizen.git import smart_open @@ -30,18 +30,13 @@ def init_empty_config_content(self) -> None: with smart_open(self.path, "a", encoding=self.encoding) as json_file: json.dump({"commitizen": {}}, json_file) - def set_key(self, key: str, value: Any) -> Self: - """Set or update a key in the conf. - - For now only strings are supported. - We use to update the version number. - """ + def set_key(self, key: str, value: object) -> Self: with open(self.path, "rb") as f: - parser = json.load(f) + config_doc = json.load(f) - parser["commitizen"][key] = value + config_doc["commitizen"][key] = value with smart_open(self.path, "w", encoding=self.encoding) as f: - json.dump(parser, f, indent=2) + json.dump(config_doc, f, indent=2) return self def _parse_setting(self, data: bytes | str) -> None: diff --git a/commitizen/config/toml_config.py b/commitizen/config/toml_config.py index 2164d3f99..a3bfc4811 100644 --- a/commitizen/config/toml_config.py +++ b/commitizen/config/toml_config.py @@ -2,9 +2,9 @@ import os from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING -from tomlkit import exceptions, parse, table +from tomlkit import TOMLDocument, exceptions, parse, table from commitizen.exceptions import InvalidConfigurationError @@ -28,30 +28,26 @@ def __init__(self, *, data: bytes | str, path: Path | str) -> None: self._parse_setting(data) def init_empty_config_content(self) -> None: + config_doc = TOMLDocument() if os.path.isfile(self.path): with open(self.path, "rb") as input_toml_file: - parser = parse(input_toml_file.read()) - else: - parser = parse("") + config_doc = parse(input_toml_file.read()) - with open(self.path, "wb") as output_toml_file: - if parser.get("tool") is None: - parser["tool"] = table() - parser["tool"]["commitizen"] = table() # type: ignore[index] - output_toml_file.write(parser.as_string().encode(self.encoding)) + if config_doc.get("tool") is None: + config_doc["tool"] = table() + config_doc["tool"]["commitizen"] = table() # type: ignore[index] - def set_key(self, key: str, value: Any) -> Self: - """Set or update a key in the conf. + with open(self.path, "wb") as output_toml_file: + output_toml_file.write(config_doc.as_string().encode(self.encoding)) - For now only strings are supported. - We use to update the version number. - """ + def set_key(self, key: str, value: object) -> Self: with open(self.path, "rb") as f: - parser = parse(f.read()) + config_doc = parse(f.read()) - parser["tool"]["commitizen"][key] = value # type: ignore[index] + config_doc["tool"]["commitizen"][key] = value # type: ignore[index] with open(self.path, "wb") as f: - f.write(parser.as_string().encode(self.encoding)) + f.write(config_doc.as_string().encode(self.encoding)) + return self def _parse_setting(self, data: bytes | str) -> None: diff --git a/commitizen/config/yaml_config.py b/commitizen/config/yaml_config.py index f2a79e693..cd8ef447e 100644 --- a/commitizen/config/yaml_config.py +++ b/commitizen/config/yaml_config.py @@ -1,7 +1,7 @@ from __future__ import annotations from pathlib import Path -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING import yaml @@ -51,17 +51,12 @@ def _parse_setting(self, data: bytes | str) -> None: except (KeyError, TypeError): self.is_empty_config = True - def set_key(self, key: str, value: Any) -> Self: - """Set or update a key in the conf. - - For now only strings are supported. - We use to update the version number. - """ + def set_key(self, key: str, value: object) -> Self: with open(self.path, "rb") as yaml_file: - parser = yaml.load(yaml_file, Loader=yaml.FullLoader) + config_doc = yaml.load(yaml_file, Loader=yaml.FullLoader) - parser["commitizen"][key] = value + config_doc["commitizen"][key] = value with smart_open(self.path, "w", encoding=self.encoding) as yaml_file: - yaml.dump(parser, yaml_file, explicit_start=True) + yaml.dump(config_doc, yaml_file, explicit_start=True) return self