Skip to content

Commit 650c720

Browse files
committed
refactor(bump_rule): resolve comments
1 parent b787be6 commit 650c720

File tree

4 files changed

+126
-66
lines changed

4 files changed

+126
-66
lines changed

commitizen/bump_rule.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
class VersionIncrement(IntEnum):
1313
"""An enumeration representing semantic versioning increments.
1414
15-
This class defines the three types of version increments according to semantic versioning:
15+
This class defines the four types of version increments according to semantic versioning:
16+
- NONE: For commits that don't require a version bump (docs, style, etc.)
1617
- PATCH: For backwards-compatible bug fixes
1718
- MINOR: For backwards-compatible functionality additions
1819
- MAJOR: For incompatible API changes
1920
"""
2021

22+
NONE = auto()
2123
PATCH = auto()
2224
MINOR = auto()
2325
MAJOR = auto()
@@ -26,16 +28,16 @@ def __str__(self) -> str:
2628
return self.name
2729

2830
@classmethod
29-
def safe_cast(cls, value: object) -> VersionIncrement | None:
31+
def safe_cast(cls, value: object) -> VersionIncrement:
3032
if not isinstance(value, str):
31-
return None
33+
return VersionIncrement.NONE
3234
try:
3335
return cls[value]
3436
except KeyError:
35-
return None
37+
return VersionIncrement.NONE
3638

37-
@classmethod
38-
def safe_cast_dict(cls, d: Mapping[str, object]) -> dict[str, VersionIncrement]:
39+
@staticmethod
40+
def safe_cast_dict(d: Mapping[str, object]) -> dict[str, VersionIncrement]:
3941
return {
4042
k: v
4143
for k, v in ((k, VersionIncrement.safe_cast(v)) for k, v in d.items())
@@ -45,8 +47,8 @@ def safe_cast_dict(cls, d: Mapping[str, object]) -> dict[str, VersionIncrement]:
4547
@staticmethod
4648
def get_highest_by_messages(
4749
commit_messages: Iterable[str],
48-
extract_increment: Callable[[str], VersionIncrement | None],
49-
) -> VersionIncrement | None:
50+
extract_increment: Callable[[str], VersionIncrement],
51+
) -> VersionIncrement:
5052
"""Find the highest version increment from a list of messages.
5153
5254
This function processes a list of messages and determines the highest version
@@ -76,9 +78,9 @@ def get_highest_by_messages(
7678

7779
@staticmethod
7880
def get_highest(
79-
increments: Iterable[VersionIncrement | None],
80-
) -> VersionIncrement | None:
81-
return max(filter(None, increments), default=None)
81+
increments: Iterable[VersionIncrement],
82+
) -> VersionIncrement:
83+
return max(increments, default=VersionIncrement.NONE)
8284

8385

8486
class BumpRule(Protocol):
@@ -94,7 +96,7 @@ class BumpRule(Protocol):
9496

9597
def extract_increment(
9698
self, commit_message: str, major_version_zero: bool
97-
) -> VersionIncrement | None:
99+
) -> VersionIncrement:
98100
"""Determine the version increment based on a commit message.
99101
100102
This method analyzes a commit message to determine what kind of version increment
@@ -107,24 +109,24 @@ def extract_increment(
107109
instead of MAJOR. This is useful for projects in 0.x.x versions.
108110
109111
Returns:
110-
VersionIncrement | None: The type of version increment needed:
112+
VersionIncrement: The type of version increment needed:
113+
- NONE: For commits that don't require a version bump (docs, style, etc.)
111114
- MAJOR: For breaking changes when major_version_zero is False
112115
- MINOR: For breaking changes when major_version_zero is True, or for new features
113116
- PATCH: For bug fixes, performance improvements, or refactors
114-
- None: For commits that don't require a version bump (docs, style, etc.)
115117
"""
116118

117119

118120
class ConventionalCommitBumpRule(BumpRule):
119-
_BREAKING_CHANGE_TYPES = set(["BREAKING CHANGE", "BREAKING-CHANGE"])
120-
_MINOR_CHANGE_TYPES = set(["feat"])
121-
_PATCH_CHANGE_TYPES = set(["fix", "perf", "refactor"])
121+
_BREAKING_CHANGE_TYPES = {"BREAKING CHANGE", "BREAKING-CHANGE"}
122+
_MINOR_CHANGE_TYPES = {"feat"}
123+
_PATCH_CHANGE_TYPES = {"fix", "perf", "refactor"}
122124

123125
def extract_increment(
124126
self, commit_message: str, major_version_zero: bool
125-
) -> VersionIncrement | None:
127+
) -> VersionIncrement:
126128
if not (m := self._head_pattern.match(commit_message)):
127-
return None
129+
return VersionIncrement.NONE
128130

129131
change_type = m.group("change_type")
130132
if m.group("bang") or change_type in self._BREAKING_CHANGE_TYPES:
@@ -138,7 +140,7 @@ def extract_increment(
138140
if change_type in self._PATCH_CHANGE_TYPES:
139141
return VersionIncrement.PATCH
140142

141-
return None
143+
return VersionIncrement.NONE
142144

143145
@cached_property
144146
def _head_pattern(self) -> re.Pattern:
@@ -225,9 +227,9 @@ def __init__(
225227

226228
def extract_increment(
227229
self, commit_message: str, major_version_zero: bool
228-
) -> VersionIncrement | None:
230+
) -> VersionIncrement:
229231
if not (m := self.bump_pattern.search(commit_message)):
230-
return None
232+
return VersionIncrement.NONE
231233

232234
effective_bump_map = (
233235
self.bump_map_major_version_zero if major_version_zero else self.bump_map
@@ -250,4 +252,4 @@ def extract_increment(
250252
for match_pattern, increment in effective_bump_map.items():
251253
if re.match(match_pattern, found_keyword):
252254
return increment
253-
return None
255+
return VersionIncrement.NONE

commitizen/commands/bump.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _is_initial_tag(
144144
)
145145
return bool(questionary.confirm("Is this the first tag created?").ask())
146146

147-
def _find_increment(self, commits: list[git.GitCommit]) -> VersionIncrement | None:
147+
def _find_increment(self, commits: list[git.GitCommit]) -> VersionIncrement:
148148
# Update the bump map to ensure major version doesn't increment.
149149
is_major_version_zero = self.bump_settings["major_version_zero"]
150150

@@ -170,7 +170,7 @@ def __call__(self) -> None:
170170

171171
if manual_version:
172172
for val, option in (
173-
(increment, "--increment"),
173+
(increment != VersionIncrement.NONE, "--increment"),
174174
(prerelease, "--prerelease"),
175175
(devrelease is not None, "--devrelease"),
176176
(is_local_version, "--local-version"),
@@ -225,7 +225,7 @@ def __call__(self) -> None:
225225
f"Invalid manual version: '{manual_version}'"
226226
) from exc
227227
else:
228-
if increment is None:
228+
if increment == VersionIncrement.NONE:
229229
commits = git.get_commits(current_tag.name if current_tag else None)
230230

231231
# No commits, there is no need to create an empty tag.
@@ -243,15 +243,19 @@ def __call__(self) -> None:
243243

244244
# It may happen that there are commits, but they are not eligible
245245
# for an increment, this generates a problem when using prerelease (#281)
246-
if prerelease and increment is None and not current_version.is_prerelease:
246+
if (
247+
prerelease
248+
and increment == VersionIncrement.NONE
249+
and not current_version.is_prerelease
250+
):
247251
raise NoCommitsFoundError(
248252
"[NO_COMMITS_FOUND]\n"
249253
"No commits found to generate a pre-release.\n"
250254
"To avoid this error, manually specify the type of increment with `--increment`"
251255
)
252256

253257
# we create an empty PATCH increment for empty tag
254-
if increment is None and allow_no_commit:
258+
if increment == VersionIncrement.NONE and allow_no_commit:
255259
increment = VersionIncrement.PATCH
256260

257261
new_version = current_version.bump(
@@ -270,7 +274,10 @@ def __call__(self) -> None:
270274
)
271275

272276
if get_next:
273-
if increment is None and new_tag_version == current_tag_version:
277+
if (
278+
increment == VersionIncrement.NONE
279+
and new_tag_version == current_tag_version
280+
):
274281
raise NoneIncrementExit(
275282
"[NO_COMMITS_TO_BUMP]\n"
276283
"The commits found are not eligible to be bumped"
@@ -292,7 +299,10 @@ def __call__(self) -> None:
292299
else:
293300
out.write(information)
294301

295-
if increment is None and new_tag_version == current_tag_version:
302+
if (
303+
increment == VersionIncrement.NONE
304+
and new_tag_version == current_tag_version
305+
):
296306
raise NoneIncrementExit(
297307
"[NO_COMMITS_TO_BUMP]\nThe commits found are not eligible to be bumped"
298308
)

commitizen/version_schemes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def __ne__(self, other: object) -> bool:
143143

144144
def bump(
145145
self,
146-
increment: VersionIncrement | None,
146+
increment: VersionIncrement,
147147
prerelease: Prerelease | None = None,
148148
prerelease_offset: int = 0,
149149
devrelease: int | None = None,
@@ -238,7 +238,7 @@ def generate_build_metadata(self, build_metadata: str | None) -> str:
238238

239239
return f"+{build_metadata}"
240240

241-
def increment_base(self, increment: VersionIncrement | None = None) -> str:
241+
def increment_base(self, increment: VersionIncrement) -> str:
242242
base = dict(
243243
zip_longest(
244244
(
@@ -265,7 +265,7 @@ def increment_base(self, increment: VersionIncrement | None = None) -> str:
265265

266266
def bump(
267267
self,
268-
increment: VersionIncrement | None,
268+
increment: VersionIncrement,
269269
prerelease: Prerelease | None = None,
270270
prerelease_offset: int = 0,
271271
devrelease: int | None = None,
@@ -307,7 +307,7 @@ def bump(
307307
) # type: ignore[return-value]
308308

309309
def _get_increment_base(
310-
self, increment: VersionIncrement | None, exact_increment: bool
310+
self, increment: VersionIncrement, exact_increment: bool
311311
) -> str:
312312
if (
313313
not self.is_prerelease

0 commit comments

Comments
 (0)