From 88886dd1c3b57a24ce250497d279d2e7f35435f8 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Fri, 30 May 2025 21:46:10 +0800 Subject: [PATCH 1/3] refactor(conventional_commits): use TypedDict for answers --- .../cz/conventional_commits/conventional_commits.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index 912770a72..d4b07d140 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -1,4 +1,5 @@ import os +from typing import TypedDict from commitizen import defaults from commitizen.cz.base import BaseCommitizen @@ -16,6 +17,15 @@ def _parse_subject(text: str) -> str: return required_validator(text.strip(".").strip(), msg="Subject is required.") +class ConventionalCommitsAnswers(TypedDict): + prefix: str + scope: str + subject: str + body: str + footer: str + is_breaking_change: bool + + class ConventionalCommitsCz(BaseCommitizen): bump_pattern = defaults.BUMP_PATTERN bump_map = defaults.BUMP_MAP @@ -136,7 +146,7 @@ def questions(self) -> Questions: }, ] - def message(self, answers: dict) -> str: + def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[override] prefix = answers["prefix"] scope = answers["scope"] subject = answers["subject"] From a895f60af291d3c9b06df32ffe4b155a29084f67 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Fri, 30 May 2025 21:46:31 +0800 Subject: [PATCH 2/3] refactor(jira): refactor message --- commitizen/cz/jira/jira.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/commitizen/cz/jira/jira.py b/commitizen/cz/jira/jira.py index 05e23e169..0d6b79b0d 100644 --- a/commitizen/cz/jira/jira.py +++ b/commitizen/cz/jira/jira.py @@ -43,18 +43,11 @@ def questions(self) -> Questions: }, ] - def message(self, answers: dict) -> str: + def message(self, answers: dict[str, str]) -> str: return " ".join( - filter( - bool, - [ - answers["message"], - answers["issues"], - answers["workflow"], - answers["time"], - answers["comment"], - ], - ) + x + for k in ("message", "issues", "workflow", "time", "comment") + if (x := answers.get(k)) ) def example(self) -> str: From 13a4b46413ceeb2fb6e2b79a979717a9b1151cda Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Fri, 30 May 2025 22:04:34 +0800 Subject: [PATCH 3/3] style: replace dict with Mapping --- commitizen/cz/base.py | 4 ++-- commitizen/cz/customize/customize.py | 5 +++-- commitizen/cz/jira/jira.py | 3 ++- tests/conftest.py | 6 +++--- tests/test_cz_base.py | 4 +++- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/commitizen/cz/base.py b/commitizen/cz/base.py index 9e803c3d0..a868fe9ce 100644 --- a/commitizen/cz/base.py +++ b/commitizen/cz/base.py @@ -1,7 +1,7 @@ from __future__ import annotations from abc import ABCMeta, abstractmethod -from collections.abc import Iterable +from collections.abc import Iterable, Mapping from typing import Any, Callable, Protocol from jinja2 import BaseLoader, PackageLoader @@ -72,7 +72,7 @@ def questions(self) -> Questions: """Questions regarding the commit message.""" @abstractmethod - def message(self, answers: dict) -> str: + def message(self, answers: Mapping[str, Any]) -> str: """Format your git message.""" @property diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 8f844501e..9d658c9fc 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -1,6 +1,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING +from collections.abc import Mapping +from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from jinja2 import Template @@ -48,7 +49,7 @@ def __init__(self, config: BaseConfig) -> None: def questions(self) -> Questions: return self.custom_settings.get("questions", [{}]) - def message(self, answers: dict) -> str: + def message(self, answers: Mapping[str, Any]) -> str: message_template = Template(self.custom_settings.get("message_template", "")) if getattr(Template, "substitute", None): return message_template.substitute(**answers) # type: ignore diff --git a/commitizen/cz/jira/jira.py b/commitizen/cz/jira/jira.py index 0d6b79b0d..0e58aabfb 100644 --- a/commitizen/cz/jira/jira.py +++ b/commitizen/cz/jira/jira.py @@ -1,4 +1,5 @@ import os +from collections.abc import Mapping from commitizen.cz.base import BaseCommitizen from commitizen.defaults import Questions @@ -43,7 +44,7 @@ def questions(self) -> Questions: }, ] - def message(self, answers: dict[str, str]) -> str: + def message(self, answers: Mapping[str, str]) -> str: return " ".join( x for k in ("message", "issues", "workflow", "time", "comment") diff --git a/tests/conftest.py b/tests/conftest.py index 1b49dcbfa..5b838d398 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ import os import re import tempfile -from collections.abc import Iterator +from collections.abc import Iterator, Mapping from pathlib import Path import pytest @@ -209,7 +209,7 @@ def questions(self) -> list: }, ] - def message(self, answers: dict) -> str: + def message(self, answers: Mapping) -> str: prefix = answers["prefix"] subject = answers.get("subject", "default message").trim() return f"{prefix}: {subject}" @@ -225,7 +225,7 @@ class MockPlugin(BaseCommitizen): def questions(self) -> defaults.Questions: return [] - def message(self, answers: dict) -> str: + def message(self, answers: Mapping) -> str: return "" diff --git a/tests/test_cz_base.py b/tests/test_cz_base.py index be93b4ca0..0ee5a23fb 100644 --- a/tests/test_cz_base.py +++ b/tests/test_cz_base.py @@ -1,3 +1,5 @@ +from collections.abc import Mapping + import pytest from commitizen.cz.base import BaseCommitizen @@ -7,7 +9,7 @@ class DummyCz(BaseCommitizen): def questions(self): return [{"type": "input", "name": "commit", "message": "Initial commit:\n"}] - def message(self, answers: dict): + def message(self, answers: Mapping): return answers["commit"]