From 417117825f83afa031b7b6c97621cc8ab05dab66 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 9a2b9016b7..f8ae5283ef 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -1,5 +1,6 @@ import os import re +from typing import TypedDict from commitizen import defaults from commitizen.cz.base import BaseCommitizen @@ -27,6 +28,15 @@ def parse_subject(text): return required_validator(text, 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 @@ -147,7 +157,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 005fc9d9c6ece03f55b364df4d88ac6aa58e2bca 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 f43de2177c..50ee70204c 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 2af4c711d3be40bc654053fbff43e5f96a21ae4f 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 43455a74ca..7dc87abf9e 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 53ada4b2c0..5e271a0552 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): 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 50ee70204c..bce9e335e8 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 60c586f2e6..97bb1e2c7f 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 4ee1cc6eda..7d88f9dd14 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"]