Skip to content

Commit 80a10e5

Browse files
committed
fix(defaults): add non-capitalized default constants back and deprecated warning
relates #1446
1 parent a8094ae commit 80a10e5

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

commitizen/defaults.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import pathlib
4+
import warnings
45
from collections import OrderedDict
56
from collections.abc import Iterable, MutableMapping, Sequence
67
from typing import Any, TypedDict
@@ -153,3 +154,31 @@ def get_tag_regexes(
153154
**{f"${k}": v for k, v in regexs.items()},
154155
**{f"${{{k}}}": v for k, v in regexs.items()},
155156
}
157+
158+
159+
def __getattr__(name: str) -> Any:
160+
# PEP-562: deprecate module-level variable
161+
162+
# {"deprecated key": (value, "new key")}
163+
deprecated_vars = {
164+
"bump_pattern": (BUMP_PATTERN, "BUMP_PATTERN"),
165+
"bump_map": (BUMP_MAP, "BUMP_MAP"),
166+
"bump_map_major_version_zero": (
167+
BUMP_MAP_MAJOR_VERSION_ZERO,
168+
"BUMP_MAP_MAJOR_VERSION_ZERO",
169+
),
170+
"bump_message": (BUMP_MESSAGE, "BUMP_MESSAGE"),
171+
"change_type_order": (CHANGE_TYPE_ORDER, "CHANGE_TYPE_ORDER"),
172+
"encoding": (ENCODING, "ENCODING"),
173+
"name": (DEFAULT_SETTINGS["name"], "DEFAULT_SETTINGS['name']"),
174+
}
175+
if name not in deprecated_vars:
176+
value, replacement = deprecated_vars[name]
177+
warnings.warn(
178+
f"{name} is deprecated and will be removed in a future version. "
179+
f"Use {replacement} instead.",
180+
DeprecationWarning,
181+
stacklevel=2,
182+
)
183+
return value
184+
raise AttributeError(f"{name} is not an attribute of {__name__}")

tests/test_defaults.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import pytest
2+
3+
from commitizen import defaults
4+
5+
6+
def test_getattr_deprecated_vars():
7+
# Test each deprecated variable
8+
with pytest.warns(DeprecationWarning) as record:
9+
assert defaults.bump_pattern == defaults.BUMP_PATTERN
10+
assert defaults.bump_map == defaults.BUMP_MAP
11+
assert (
12+
defaults.bump_map_major_version_zero == defaults.BUMP_MAP_MAJOR_VERSION_ZERO
13+
)
14+
assert defaults.bump_message == defaults.BUMP_MESSAGE
15+
assert defaults.change_type_order == defaults.CHANGE_TYPE_ORDER
16+
assert defaults.encoding == defaults.ENCODING
17+
assert defaults.name == defaults.DEFAULT_SETTINGS["name"]
18+
19+
# Verify warning messages
20+
assert len(record) == 7
21+
for warning in record:
22+
assert "is deprecated and will be removed in a future version" in str(
23+
warning.message
24+
)
25+
26+
27+
def test_getattr_non_existent():
28+
# Test non-existent attribute
29+
with pytest.raises(AttributeError) as exc_info:
30+
_ = defaults.non_existent_attribute
31+
assert "is not an attribute of" in str(exc_info.value)

0 commit comments

Comments
 (0)