Skip to content

Commit 4dd7249

Browse files
committed
refactor(changelog): minor cleanup
1 parent e3b4465 commit 4dd7249

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

commitizen/commands/changelog.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from difflib import SequenceMatcher
66
from operator import itemgetter
77
from pathlib import Path
8-
from typing import Callable, cast
98

109
from commitizen import changelog, defaults, factory, git, out
1110
from commitizen.changelog_formats import get_changelog_format
@@ -32,9 +31,10 @@ def __init__(self, config: BaseConfig, args):
3231
if not git.is_git_project():
3332
raise NotAGitProjectError()
3433

35-
self.config: BaseConfig = config
36-
changelog_file_name = args.get("file_name") or cast(
37-
str, self.config.settings.get("changelog_file")
34+
self.config = config
35+
36+
changelog_file_name = args.get("file_name") or self.config.settings.get(
37+
"changelog_file"
3838
)
3939
if not isinstance(changelog_file_name, str):
4040
raise NotAllowed(
@@ -114,28 +114,28 @@ def _find_incremental_rev(self, latest_version: str, tags: list[GitTag]) -> str:
114114
on our experience.
115115
"""
116116
SIMILARITY_THRESHOLD = 0.89
117-
tag_ratio = map(
118-
lambda tag: (
119-
SequenceMatcher(
120-
None, latest_version, strip_local_version(tag.name)
121-
).ratio(),
122-
tag,
123-
),
124-
tags,
125-
)
126117
try:
127-
score, tag = max(tag_ratio, key=itemgetter(0))
118+
score, tag = max(
119+
(
120+
(
121+
SequenceMatcher(
122+
None, latest_version, strip_local_version(tag.name)
123+
).ratio(),
124+
tag,
125+
)
126+
for tag in tags
127+
),
128+
key=itemgetter(0),
129+
)
128130
except ValueError:
129131
raise NoRevisionError()
130132
if score < SIMILARITY_THRESHOLD:
131133
raise NoRevisionError()
132-
start_rev = tag.name
133-
return start_rev
134+
return tag.name
134135

135136
def write_changelog(
136137
self, changelog_out: str, lines: list[str], changelog_meta: changelog.Metadata
137138
):
138-
changelog_hook: Callable | None = self.cz.changelog_hook
139139
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
140140
partial_changelog: str | None = None
141141
if self.incremental:
@@ -145,8 +145,8 @@ def write_changelog(
145145
changelog_out = "".join(new_lines)
146146
partial_changelog = changelog_out
147147

148-
if changelog_hook:
149-
changelog_out = changelog_hook(changelog_out, partial_changelog)
148+
if self.cz.changelog_hook:
149+
changelog_out = self.cz.changelog_hook(changelog_out, partial_changelog)
150150

151151
changelog_file.write(changelog_out)
152152

@@ -221,14 +221,12 @@ def __call__(self):
221221
extras.update(self.extras)
222222
changelog_out = changelog.render_changelog(
223223
tree, loader=self.cz.template_loader, template=self.template, **extras
224-
)
225-
changelog_out = changelog_out.lstrip("\n")
224+
).lstrip("\n")
226225

227226
# Dry_run is executed here to avoid checking and reading the files
228227
if self.dry_run:
229-
changelog_hook: Callable | None = self.cz.changelog_hook
230-
if changelog_hook:
231-
changelog_out = changelog_hook(changelog_out, "")
228+
if self.cz.changelog_hook:
229+
changelog_out = self.cz.changelog_hook(changelog_out, "")
232230
out.write(changelog_out)
233231
raise DryRunExit()
234232

0 commit comments

Comments
 (0)