Skip to content

refactor(questions): type questions with TypedDict #1485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: refactors
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def prompt_commit_questions(self) -> str:
# Prompt user for the commit message
cz = self.cz
questions = cz.questions()
for question in filter(lambda q: q["type"] == "list", questions):
for question in (q for q in questions if q["type"] == "list"):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type inference only works this way

question["use_shortcuts"] = self.config.settings["use_shortcuts"]
try:
answers = questionary.prompt(questions, style=cz.style)
Expand Down
4 changes: 2 additions & 2 deletions commitizen/cz/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from commitizen import git
from commitizen.config.base_config import BaseConfig
from commitizen.defaults import Questions
from commitizen.question import CzQuestion


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

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

@abstractmethod
Expand Down
6 changes: 3 additions & 3 deletions commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from commitizen import defaults
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import multiple_line_breaker, required_validator
from commitizen.defaults import Questions
from commitizen.question import CzQuestion

__all__ = ["ConventionalCommitsCz"]

Expand Down Expand Up @@ -40,7 +40,7 @@ class ConventionalCommitsCz(BaseCommitizen):
}
changelog_pattern = defaults.BUMP_PATTERN

def questions(self) -> Questions:
def questions(self) -> list[CzQuestion]:
return [
{
"type": "list",
Expand Down Expand Up @@ -133,8 +133,8 @@ def questions(self) -> Questions:
},
{
"type": "confirm",
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
"name": "is_breaking_change",
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
"default": False,
},
{
Expand Down
7 changes: 4 additions & 3 deletions commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import TYPE_CHECKING

from commitizen.question import CzQuestion

if TYPE_CHECKING:
from jinja2 import Template
else:
Expand All @@ -14,7 +16,6 @@
from commitizen import defaults
from commitizen.config import BaseConfig
from commitizen.cz.base import BaseCommitizen
from commitizen.defaults import Questions
from commitizen.exceptions import MissingCzCustomizeConfigError

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

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

def message(self, answers: dict) -> str:
message_template = Template(self.custom_settings.get("message_template", ""))
Expand Down
4 changes: 2 additions & 2 deletions commitizen/cz/jira/jira.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os

from commitizen.cz.base import BaseCommitizen
from commitizen.defaults import Questions
from commitizen.question import CzQuestion

__all__ = ["JiraSmartCz"]


class JiraSmartCz(BaseCommitizen):
def questions(self) -> Questions:
def questions(self) -> list[CzQuestion]:
return [
{
"type": "input",
Expand Down
6 changes: 4 additions & 2 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
from collections.abc import Iterable, MutableMapping, Sequence
from typing import Any, TypedDict

from commitizen.question import CzQuestion

# Type
Questions = Iterable[MutableMapping[str, Any]]
Questions = Iterable[MutableMapping[str, Any]] # TODO: deprecate this?


class CzSettings(TypedDict, total=False):
Expand All @@ -16,7 +18,7 @@ class CzSettings(TypedDict, total=False):
bump_map_major_version_zero: OrderedDict[str, str]
change_type_order: list[str]

questions: Questions
questions: Iterable[CzQuestion]
example: str | None
schema_pattern: str | None
schema: str | None
Expand Down
32 changes: 32 additions & 0 deletions commitizen/question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from typing import Callable, Literal, TypedDict, Union


class Choice(TypedDict, total=False):
value: str
name: str
key: str


class ListQuestion(TypedDict, total=False):
type: Literal["list"]
name: str
message: str
choices: list[Choice]
use_shortcuts: bool


class InputQuestion(TypedDict, total=False):
type: Literal["input"]
name: str
message: str
filter: Callable[[str], str]


class ConfirmQuestion(TypedDict):
type: Literal["confirm"]
name: str
message: str
default: bool


CzQuestion = Union[ListQuestion, InputQuestion, ConfirmQuestion]
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from commitizen.config import BaseConfig
from commitizen.cz import registry
from commitizen.cz.base import BaseCommitizen
from commitizen.question import CzQuestion
from tests.utils import create_file_and_commit

SIGNER = "GitHub Action"
Expand Down Expand Up @@ -222,7 +223,7 @@ def use_cz_semver(mocker):


class MockPlugin(BaseCommitizen):
def questions(self) -> defaults.Questions:
def questions(self) -> list[CzQuestion]:
return []

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