Skip to content

Commit 00a3a74

Browse files
committed
refactor(bump_rule): resolve comments
1 parent 7c18e43 commit 00a3a74

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
@@ -143,7 +143,7 @@ def _is_initial_tag(
143143
)
144144
return bool(questionary.confirm("Is this the first tag created?").ask())
145145

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

@@ -169,7 +169,7 @@ def __call__(self) -> None:
169169

170170
if manual_version:
171171
for val, option in (
172-
(increment, "--increment"),
172+
(increment != VersionIncrement.NONE, "--increment"),
173173
(prerelease, "--prerelease"),
174174
(devrelease is not None, "--devrelease"),
175175
(is_local_version, "--local-version"),
@@ -224,7 +224,7 @@ def __call__(self) -> None:
224224
f"Invalid manual version: '{manual_version}'"
225225
) from exc
226226
else:
227-
if increment is None:
227+
if increment == VersionIncrement.NONE:
228228
commits = git.get_commits(current_tag.name if current_tag else None)
229229

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

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

252256
# we create an empty PATCH increment for empty tag
253-
if increment is None and allow_no_commit:
257+
if increment == VersionIncrement.NONE and allow_no_commit:
254258
increment = VersionIncrement.PATCH
255259

256260
new_version = current_version.bump(
@@ -269,7 +273,10 @@ def __call__(self) -> None:
269273
)
270274

271275
if get_next:
272-
if increment is None and new_tag_version == current_tag_version:
276+
if (
277+
increment == VersionIncrement.NONE
278+
and new_tag_version == current_tag_version
279+
):
273280
raise NoneIncrementExit(
274281
"[NO_COMMITS_TO_BUMP]\n"
275282
"The commits found are not eligible to be bumped"
@@ -291,7 +298,10 @@ def __call__(self) -> None:
291298
else:
292299
out.write(information)
293300

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

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)