Skip to content

Commit a10e9a1

Browse files
committed
refactor(containers): add unique list
1 parent 42a86cc commit a10e9a1

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

commitizen/changelog.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
Template,
4343
)
4444

45+
from commitizen.containers import UniqueList
4546
from commitizen.cz.base import ChangelogReleaseHook
46-
from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError
47+
from commitizen.exceptions import NoCommitsFoundError
4748
from commitizen.git import GitCommit, GitTag
4849
from commitizen.tags import TagRules
4950

@@ -188,13 +189,8 @@ def process_commit_message(
188189

189190

190191
def order_changelog_tree(
191-
tree: Iterable[Mapping[str, Any]], change_type_order: list[str]
192+
tree: Iterable[Mapping[str, Any]], change_type_order: UniqueList[str]
192193
) -> Generator[dict[str, Any], None, None]:
193-
if len(set(change_type_order)) != len(change_type_order):
194-
raise InvalidConfigurationError(
195-
f"Change types contain duplicates types ({change_type_order})"
196-
)
197-
198194
for entry in tree:
199195
yield {
200196
**entry,

commitizen/commands/changelog.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from commitizen import changelog, defaults, factory, git, out
1111
from commitizen.changelog_formats import get_changelog_format
1212
from commitizen.config import BaseConfig
13+
from commitizen.containers import UniqueList
1314
from commitizen.cz.base import ChangelogReleaseHook, MessageBuilderHook
1415
from commitizen.cz.utils import strip_local_version
1516
from commitizen.exceptions import (
@@ -75,11 +76,6 @@ def __init__(self, config: BaseConfig, args):
7576
self.change_type_map = (
7677
self.config.settings.get("change_type_map") or self.cz.change_type_map
7778
)
78-
self.change_type_order = (
79-
self.config.settings.get("change_type_order")
80-
or self.cz.change_type_order
81-
or defaults.CHANGE_TYPE_ORDER
82-
)
8379
self.rev_range = args.get("rev_range")
8480
self.tag_format: str = (
8581
args.get("tag_format") or self.config.settings["tag_format"]
@@ -101,6 +97,14 @@ def __init__(self, config: BaseConfig, args):
10197
self.extras = args.get("extras") or {}
10298
self.export_template_to = args.get("export_template")
10399

100+
@property
101+
def change_type_order(self) -> UniqueList[str]:
102+
return UniqueList(
103+
self.config.settings.get("change_type_order") # type: ignore
104+
or self.cz.change_type_order
105+
or defaults.CHANGE_TYPE_ORDER
106+
)
107+
104108
def _find_incremental_rev(self, latest_version: str, tags: list[GitTag]) -> str:
105109
"""Try to find the 'start_rev'.
106110

commitizen/containers.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from collections.abc import Iterable
2+
from typing import Generic, TypeVar
3+
4+
T = TypeVar("T")
5+
6+
7+
class UniqueList(Generic[T]):
8+
def __init__(self, items: list[T]):
9+
if len(items) != len(set(items)):
10+
raise ValueError("Items must be unique")
11+
self._items = items
12+
13+
def __iter__(self):
14+
return iter(self._items)
15+
16+
def __getitem__(self, index):
17+
return self._items[index]
18+
19+
def __repr__(self):
20+
return f"UniqueList({self._items})"
21+
22+
def __add__(self, other: Iterable[T]) -> "UniqueList[T]":
23+
# Support UniqueList + list or UniqueList + UniqueList
24+
combined = self._items + list(other)
25+
return UniqueList(combined)
26+
27+
def __radd__(self, other: Iterable[T]) -> "UniqueList[T]":
28+
combined = list(other) + self._items
29+
return UniqueList(combined)
30+
31+
def __len__(self) -> int:
32+
return len(self._items)
33+
34+
def __contains__(self, item: T) -> bool:
35+
return item in self._items
36+
37+
def __eq__(self, other: object) -> bool:
38+
return isinstance(other, UniqueList) and self._items == other._items

tests/test_changelog.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from commitizen.cz.conventional_commits.conventional_commits import (
1818
ConventionalCommitsCz,
1919
)
20-
from commitizen.exceptions import InvalidConfigurationError
2120
from commitizen.version_schemes import Pep440
2221

2322
COMMITS_DATA: list[dict[str, Any]] = [
@@ -1231,14 +1230,6 @@ def test_order_changelog_tree(change_type_order, expected_reordering):
12311230
assert [*entry["changes"].keys()] == [*entry["changes"].keys()]
12321231

12331232

1234-
def test_order_changelog_tree_raises():
1235-
change_type_order = ["BREAKING CHANGE", "feat", "refactor", "feat"]
1236-
with pytest.raises(InvalidConfigurationError) as excinfo:
1237-
list(changelog.order_changelog_tree(COMMITS_TREE, change_type_order))
1238-
1239-
assert "Change types contain duplicates types" in str(excinfo)
1240-
1241-
12421233
def test_render_changelog(
12431234
gitcommits, tags, changelog_content, any_changelog_format: ChangelogFormat
12441235
):

tests/test_containers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
3+
from commitizen.containers import UniqueList
4+
5+
6+
def test_unique_list():
7+
# Test initialization with unique items
8+
unique_list = UniqueList([1, 2, 3])
9+
assert list(unique_list) == [1, 2, 3]
10+
11+
# Test initialization with duplicate items
12+
with pytest.raises(ValueError, match="Items must be unique"):
13+
UniqueList([1, 1, 2])
14+
15+
# Test iteration
16+
items = [1, 2, 3]
17+
unique_list = UniqueList(items)
18+
assert [x for x in unique_list] == items
19+
20+
# Test indexing
21+
assert unique_list[0] == 1
22+
assert unique_list[1] == 2
23+
assert unique_list[2] == 3
24+
25+
# Test string representation
26+
assert repr(unique_list) == "UniqueList([1, 2, 3])"
27+
28+
# Test with different types
29+
string_list = UniqueList(["a", "b", "c"])
30+
assert list(string_list) == ["a", "b", "c"]
31+
32+
# Test add
33+
assert unique_list + [4, 5, 6] == UniqueList([1, 2, 3, 4, 5, 6])
34+
assert [4, 5, 6] + unique_list == UniqueList([4, 5, 6, 1, 2, 3])

0 commit comments

Comments
 (0)