Skip to content

Commit 904f200

Browse files
refactor: use namedtuple for return
1 parent 784e3b4 commit 904f200

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

commitizen/commands/check.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __call__(self):
7272

7373
pattern = self.cz.schema_pattern()
7474
ill_formated_commits = [
75-
(commit, check[1])
75+
(commit, check.errors)
7676
for commit in commits
7777
if not (
7878
check := self.cz.validate_commit_message(
@@ -82,7 +82,7 @@ def __call__(self):
8282
allowed_prefixes=self.allowed_prefixes,
8383
max_msg_length=self.max_msg_length,
8484
)
85-
)[0]
85+
).is_valid
8686
]
8787

8888
if ill_formated_commits:

commitizen/cz/base.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import re
44
from abc import ABCMeta, abstractmethod
5-
from typing import Any, Callable, Iterable, Protocol
5+
from typing import Any, Callable, Iterable, NamedTuple, Protocol
66

77
from jinja2 import BaseLoader, PackageLoader
88
from prompt_toolkit.styles import Style, merge_styles
@@ -24,6 +24,11 @@ def __call__(
2424
) -> dict[str, Any]: ...
2525

2626

27+
class ValidationResult(NamedTuple):
28+
is_valid: bool
29+
errors: list
30+
31+
2732
class BaseCommitizen(metaclass=ABCMeta):
2833
bump_pattern: str | None = None
2934
bump_map: dict[str, str] | None = None
@@ -41,7 +46,7 @@ class BaseCommitizen(metaclass=ABCMeta):
4146
("disabled", "fg:#858585 italic"),
4247
]
4348

44-
# The whole subject will be parsed as message by default
49+
# The whole subject will be parsed as a message by default
4550
# This allows supporting changelog for any rule system.
4651
# It can be modified per rule
4752
commit_parser: str | None = r"(?P<message>.*)"
@@ -104,21 +109,21 @@ def validate_commit_message(
104109
allow_abort: bool,
105110
allowed_prefixes: list[str],
106111
max_msg_length: int,
107-
) -> tuple[bool, list]:
112+
) -> ValidationResult:
108113
"""Validate commit message against the pattern."""
109114
if not commit_msg:
110-
return allow_abort, []
115+
return ValidationResult(allow_abort, [])
111116

112117
if pattern is None:
113-
return True, []
118+
return ValidationResult(True, [])
114119

115120
if any(map(commit_msg.startswith, allowed_prefixes)):
116-
return True, []
121+
return ValidationResult(True, [])
117122
if max_msg_length:
118123
msg_len = len(commit_msg.partition("\n")[0].strip())
119124
if msg_len > max_msg_length:
120-
return False, []
121-
return bool(re.match(pattern, commit_msg)), []
125+
return ValidationResult(False, [])
126+
return ValidationResult(bool(re.match(pattern, commit_msg)), [])
122127

123128
def format_exception_message(
124129
self, ill_formated_commits: list[tuple[git.GitCommit, list]]

tests/conftest.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717
from commitizen.config import BaseConfig
1818
from commitizen.cz import registry
19-
from commitizen.cz.base import BaseCommitizen
19+
from commitizen.cz.base import BaseCommitizen, ValidationResult
2020
from tests.utils import create_file_and_commit
2121

2222
SIGNER = "GitHub Action"
@@ -255,26 +255,31 @@ def validate_commit_message(
255255
allow_abort: bool,
256256
allowed_prefixes: list[str],
257257
max_msg_length: int,
258-
) -> tuple[bool, list]:
258+
) -> ValidationResult:
259259
"""Validate commit message against the pattern."""
260260
if not commit_msg:
261-
return allow_abort, [] if allow_abort else ["commit message is empty"]
261+
return ValidationResult(
262+
allow_abort, [] if allow_abort else ["commit message is empty"]
263+
)
262264

263265
if pattern is None:
264-
return True, []
266+
return ValidationResult(True, [])
265267

266268
if any(map(commit_msg.startswith, allowed_prefixes)):
267-
return True, []
269+
return ValidationResult(True, [])
268270
if max_msg_length:
269271
msg_len = len(commit_msg.partition("\n")[0].strip())
270272
if msg_len > max_msg_length:
271-
return False, [
272-
f"commit message is too long. Max length is {max_msg_length}"
273-
]
273+
return ValidationResult(
274+
False,
275+
[f"commit message is too long. Max length is {max_msg_length}"],
276+
)
274277
pattern_match = bool(re.match(pattern, commit_msg))
275278
if not pattern_match:
276-
return False, [f"commit message does not match pattern {pattern}"]
277-
return True, []
279+
return ValidationResult(
280+
False, [f"commit message does not match pattern {pattern}"]
281+
)
282+
return ValidationResult(True, [])
278283

279284
def format_exception_message(
280285
self, ill_formated_commits: list[tuple[git.GitCommit, list]]

tests/test_cz_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from commitizen.cz.base import BaseCommitizen
5+
from commitizen.cz.base import BaseCommitizen, ValidationResult
66

77

88
class DummyCz(BaseCommitizen):
@@ -51,7 +51,7 @@ def test_validate_commit_message(config):
5151
allow_abort=False,
5252
allowed_prefixes=[],
5353
max_msg_length=0,
54-
) == (True, [])
54+
) == ValidationResult(True, [])
5555

5656

5757
def test_info(config):

0 commit comments

Comments
 (0)