Skip to content

refactor(changelog): better typing, yield #1453

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
30 changes: 15 additions & 15 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

import re
from collections import OrderedDict, defaultdict
from collections.abc import Iterable
from collections.abc import Generator, Iterable, Mapping
from dataclasses import dataclass
from datetime import date
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from jinja2 import (
BaseLoader,
Expand Down Expand Up @@ -84,7 +84,7 @@ def generate_tree_from_commits(
changelog_message_builder_hook: MessageBuilderHook | None = None,
changelog_release_hook: ChangelogReleaseHook | None = None,
rules: TagRules | None = None,
) -> Iterable[dict]:
) -> Generator[dict[str, Any], None, None]:
pat = re.compile(changelog_pattern)
map_pat = re.compile(commit_parser, re.MULTILINE)
body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL)
Expand Down Expand Up @@ -187,24 +187,24 @@ def process_commit_message(
changes[change_type].append(msg)


def order_changelog_tree(tree: Iterable, change_type_order: list[str]) -> Iterable:
def order_changelog_tree(
tree: Iterable[Mapping[str, Any]], change_type_order: list[str]
) -> Generator[dict[str, Any], None, None]:
if len(set(change_type_order)) != len(change_type_order):
raise InvalidConfigurationError(
f"Change types contain duplicates types ({change_type_order})"
)

sorted_tree = []
for entry in tree:
ordered_change_types = change_type_order + sorted(
set(entry["changes"].keys()) - set(change_type_order)
)
changes = [
(ct, entry["changes"][ct])
for ct in ordered_change_types
if ct in entry["changes"]
]
sorted_tree.append({**entry, **{"changes": OrderedDict(changes)}})
return sorted_tree
yield {
**entry,
"changes": OrderedDict(
(ct, entry["changes"][ct])
for ct in change_type_order
+ sorted(set(entry["changes"].keys()) - set(change_type_order))
if ct in entry["changes"]
),
}


def get_changelog_template(loader: BaseLoader, template: str) -> Template:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,22 +1219,22 @@ def test_order_changelog_tree(change_type_order, expected_reordering):
tree = changelog.order_changelog_tree(COMMITS_TREE, change_type_order)

for index, entry in enumerate(tuple(tree)):
version = tree[index]["version"]
version = entry["version"]
if version in expected_reordering:
# Verify that all keys are present
assert [*tree[index].keys()] == [*COMMITS_TREE[index].keys()]
assert [*entry.keys()] == [*COMMITS_TREE[index].keys()]
# Verify that the reorder only impacted the returned dict and not the original
expected = expected_reordering[version]
assert [*tree[index]["changes"].keys()] == expected["sorted"]
assert [*entry["changes"].keys()] == expected["sorted"]
assert [*COMMITS_TREE[index]["changes"].keys()] == expected["original"]
else:
assert [*entry["changes"].keys()] == [*tree[index]["changes"].keys()]
assert [*entry["changes"].keys()] == [*entry["changes"].keys()]


def test_order_changelog_tree_raises():
change_type_order = ["BREAKING CHANGE", "feat", "refactor", "feat"]
with pytest.raises(InvalidConfigurationError) as excinfo:
changelog.order_changelog_tree(COMMITS_TREE, change_type_order)
list(changelog.order_changelog_tree(COMMITS_TREE, change_type_order))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added list here for triggering the error.


assert "Change types contain duplicates types" in str(excinfo)

Expand Down