Skip to content

fix(defaults): add non-capitalized default constants back and deprecated warning #1447

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import pathlib
import warnings
from collections import OrderedDict
from collections.abc import Iterable, MutableMapping, Sequence
from typing import Any, TypedDict
Expand Down Expand Up @@ -153,3 +154,31 @@ def get_tag_regexes(
**{f"${k}": v for k, v in regexs.items()},
**{f"${{{k}}}": v for k, v in regexs.items()},
}


def __getattr__(name: str) -> Any:
# PEP-562: deprecate module-level variable

# {"deprecated key": (value, "new key")}
deprecated_vars = {
"bump_pattern": (BUMP_PATTERN, "BUMP_PATTERN"),
"bump_map": (BUMP_MAP, "BUMP_MAP"),
"bump_map_major_version_zero": (
BUMP_MAP_MAJOR_VERSION_ZERO,
"BUMP_MAP_MAJOR_VERSION_ZERO",
),
"bump_message": (BUMP_MESSAGE, "BUMP_MESSAGE"),
"change_type_order": (CHANGE_TYPE_ORDER, "CHANGE_TYPE_ORDER"),
"encoding": (ENCODING, "ENCODING"),
"name": (DEFAULT_SETTINGS["name"], "DEFAULT_SETTINGS['name']"),
}
if name in deprecated_vars:
value, replacement = deprecated_vars[name]
warnings.warn(
f"{name} is deprecated and will be removed in a future version. "
f"Use {replacement} instead.",
DeprecationWarning,
stacklevel=2,
)
return value
raise AttributeError(f"{name} is not an attribute of {__name__}")
31 changes: 31 additions & 0 deletions tests/test_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from commitizen import defaults


def test_getattr_deprecated_vars():
# Test each deprecated variable
with pytest.warns(DeprecationWarning) as record:
assert defaults.bump_pattern == defaults.BUMP_PATTERN
assert defaults.bump_map == defaults.BUMP_MAP
assert (
defaults.bump_map_major_version_zero == defaults.BUMP_MAP_MAJOR_VERSION_ZERO
)
assert defaults.bump_message == defaults.BUMP_MESSAGE
assert defaults.change_type_order == defaults.CHANGE_TYPE_ORDER
assert defaults.encoding == defaults.ENCODING
assert defaults.name == defaults.DEFAULT_SETTINGS["name"]

# Verify warning messages
assert len(record) == 7
for warning in record:
assert "is deprecated and will be removed in a future version" in str(
warning.message
)


def test_getattr_non_existent():
# Test non-existent attribute
with pytest.raises(AttributeError) as exc_info:
_ = defaults.non_existent_attribute
assert "is not an attribute of" in str(exc_info.value)