Skip to content

Commit 0a2c7b3

Browse files
committed
refactor(questions): type questions with TypedDict
1 parent 59fd3f5 commit 0a2c7b3

File tree

8 files changed

+50
-14
lines changed

8 files changed

+50
-14
lines changed

commitizen/commands/commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def prompt_commit_questions(self) -> str:
5252
# Prompt user for the commit message
5353
cz = self.cz
5454
questions = cz.questions()
55-
for question in filter(lambda q: q["type"] == "list", questions):
55+
for question in (q for q in questions if q["type"] == "list"):
5656
question["use_shortcuts"] = self.config.settings["use_shortcuts"]
5757
try:
5858
answers = questionary.prompt(questions, style=cz.style)

commitizen/cz/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from commitizen import git
1111
from commitizen.config.base_config import BaseConfig
12-
from commitizen.defaults import Questions
12+
from commitizen.question import CzQuestion
1313

1414

1515
class MessageBuilderHook(Protocol):
@@ -68,7 +68,7 @@ def __init__(self, config: BaseConfig) -> None:
6868
self.config.settings.update({"style": BaseCommitizen.default_style_config})
6969

7070
@abstractmethod
71-
def questions(self) -> Questions:
71+
def questions(self) -> Iterable[CzQuestion]:
7272
"""Questions regarding the commit message."""
7373

7474
@abstractmethod

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from commitizen import defaults
55
from commitizen.cz.base import BaseCommitizen
66
from commitizen.cz.utils import multiple_line_breaker, required_validator
7-
from commitizen.defaults import Questions
7+
from commitizen.question import CzQuestion
88

99
__all__ = ["ConventionalCommitsCz"]
1010

@@ -40,7 +40,7 @@ class ConventionalCommitsCz(BaseCommitizen):
4040
}
4141
changelog_pattern = defaults.BUMP_PATTERN
4242

43-
def questions(self) -> Questions:
43+
def questions(self) -> list[CzQuestion]:
4444
return [
4545
{
4646
"type": "list",
@@ -133,8 +133,8 @@ def questions(self) -> Questions:
133133
},
134134
{
135135
"type": "confirm",
136-
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
137136
"name": "is_breaking_change",
137+
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
138138
"default": False,
139139
},
140140
{

commitizen/cz/customize/customize.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING
44

5+
from commitizen.question import CzQuestion
6+
57
if TYPE_CHECKING:
68
from jinja2 import Template
79
else:
@@ -14,7 +16,6 @@
1416
from commitizen import defaults
1517
from commitizen.config import BaseConfig
1618
from commitizen.cz.base import BaseCommitizen
17-
from commitizen.defaults import Questions
1819
from commitizen.exceptions import MissingCzCustomizeConfigError
1920

2021
__all__ = ["CustomizeCommitsCz"]
@@ -45,8 +46,8 @@ def __init__(self, config: BaseConfig):
4546
if value := self.custom_settings.get(attr_name):
4647
setattr(self, attr_name, value)
4748

48-
def questions(self) -> Questions:
49-
return self.custom_settings.get("questions", [{}])
49+
def questions(self) -> list[CzQuestion]:
50+
return self.custom_settings.get("questions", [{}]) # type: ignore
5051

5152
def message(self, answers: dict) -> str:
5253
message_template = Template(self.custom_settings.get("message_template", ""))

commitizen/cz/jira/jira.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import os
22

33
from commitizen.cz.base import BaseCommitizen
4-
from commitizen.defaults import Questions
4+
from commitizen.question import CzQuestion
55

66
__all__ = ["JiraSmartCz"]
77

88

99
class JiraSmartCz(BaseCommitizen):
10-
def questions(self) -> Questions:
10+
def questions(self) -> list[CzQuestion]:
1111
return [
1212
{
1313
"type": "input",

commitizen/defaults.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from collections.abc import Iterable, MutableMapping, Sequence
77
from typing import Any, TypedDict
88

9+
from commitizen.question import CzQuestion
10+
911
# Type
10-
Questions = Iterable[MutableMapping[str, Any]]
12+
Questions = Iterable[MutableMapping[str, Any]] # TODO: deprecate this?
1113

1214

1315
class CzSettings(TypedDict, total=False):
@@ -16,7 +18,7 @@ class CzSettings(TypedDict, total=False):
1618
bump_map_major_version_zero: OrderedDict[str, str]
1719
change_type_order: list[str]
1820

19-
questions: Questions
21+
questions: Iterable[CzQuestion]
2022
example: str | None
2123
schema_pattern: str | None
2224
schema: str | None

commitizen/question.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Callable, Literal, TypedDict, Union
2+
3+
4+
class Choice(TypedDict, total=False):
5+
value: str
6+
name: str
7+
key: str
8+
9+
10+
class ListQuestion(TypedDict, total=False):
11+
type: Literal["list"]
12+
name: str
13+
message: str
14+
choices: list[Choice]
15+
use_shortcuts: bool
16+
17+
18+
class InputQuestion(TypedDict, total=False):
19+
type: Literal["input"]
20+
name: str
21+
message: str
22+
filter: Callable[[str], str]
23+
24+
25+
class ConfirmQuestion(TypedDict):
26+
type: Literal["confirm"]
27+
name: str
28+
message: str
29+
default: bool
30+
31+
32+
CzQuestion = Union[ListQuestion, InputQuestion, ConfirmQuestion]

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from commitizen.config import BaseConfig
1818
from commitizen.cz import registry
1919
from commitizen.cz.base import BaseCommitizen
20+
from commitizen.question import CzQuestion
2021
from tests.utils import create_file_and_commit
2122

2223
SIGNER = "GitHub Action"
@@ -222,7 +223,7 @@ def use_cz_semver(mocker):
222223

223224

224225
class MockPlugin(BaseCommitizen):
225-
def questions(self) -> defaults.Questions:
226+
def questions(self) -> list[CzQuestion]:
226227
return []
227228

228229
def message(self, answers: dict) -> str:

0 commit comments

Comments
 (0)