feat(config): warn on unknown configuration keys by bearomorphism · Pull Request #1961 · commitizen-tools/commitizen · GitHub
Skip to content
Open
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
31 changes: 29 additions & 2 deletions commitizen/config/base_config.py
7 changes: 5 additions & 2 deletions commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def _parse_setting(self, data: bytes | str) -> None:
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")

try:
self.settings.update(doc["commitizen"])
section = doc["commitizen"]
except KeyError:
pass
return

self.settings.update(section)
self._validate_known_keys(section)
7 changes: 5 additions & 2 deletions commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def _parse_setting(self, data: bytes | str) -> None:
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")

try:
self.settings.update(doc["tool"]["commitizen"]) # type: ignore[index,typeddict-item] # TODO: fix this
section = doc["tool"]["commitizen"] # type: ignore[index]
except exceptions.NonExistentKey:
pass
return

self.settings.update(section) # type: ignore[typeddict-item] # TODO: fix this
self._validate_known_keys(section) # type: ignore[arg-type]
7 changes: 5 additions & 2 deletions commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ def _parse_setting(self, data: bytes | str) -> None:
raise InvalidConfigurationError(f"Failed to parse {self.path}: {e}")

try:
self.settings.update(doc["commitizen"])
section = doc["commitizen"]
except (KeyError, TypeError):
pass
return

self.settings.update(section)
self._validate_known_keys(section)

def set_key(self, key: str, value: object) -> Self:
with self.path.open("rb") as yaml_file:
Expand Down
5 changes: 5 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Settings(TypedDict, total=False):
version_type: str | None
version: str | None
breaking_change_exclamation_in_title: bool
strict_config: bool


CONFIG_FILES: tuple[str, ...] = (
Expand Down Expand Up @@ -115,8 +116,12 @@ class Settings(TypedDict, total=False):
"extras": {},
"breaking_change_exclamation_in_title": False,
"message_length_limit": 0, # 0 for no limit
"strict_config": False,
}


KNOWN_SETTINGS_KEYS: frozenset[str] = frozenset(Settings.__annotations__)

MAJOR = "MAJOR"
MINOR = "MINOR"
PATCH = "PATCH"
Expand Down
25 changes: 25 additions & 0 deletions docs/config/option.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ Custom rules for committing and bumping.

See [customization](../customization/config_file.md) for more details.

## `strict_config`

When enabled, Commitizen raises an error if the configuration file contains
keys that are not recognized as valid commitizen settings (for example because
of a typo such as `update_changelog_on_bumb` instead of
`update_changelog_on_bump`).

When disabled (the default), unknown keys only produce a warning so they can be
spotted without breaking existing setups.

- Type: `bool`
- Default: `False`

**Example**

```toml title="pyproject.toml"
[tool.commitizen]
name = "cz_conventional_commits"
strict_config = true
```

If you intentionally need to keep additional plugin-specific data inside the
commitizen section, put it under the `extras` setting so it is not flagged as
unknown.

## `use_shortcuts`

Show keyboard shortcuts when selecting from a list. When enabled, each choice shows a shortcut key; press that key or use the arrow keys to select.
Expand Down
124 changes: 124 additions & 0 deletions tests/test_conf.py
Loading