Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

refactor(cz): better typing and refactor message method #1472

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 3 commits 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
4 changes: 2 additions & 2 deletions commitizen/cz/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
from typing import TypedDict

from commitizen import defaults
from commitizen.cz.base import BaseCommitizen
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
5 changes: 3 additions & 2 deletions commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 5 additions & 11 deletions commitizen/cz/jira/jira.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from collections.abc import Mapping

from commitizen.cz.base import BaseCommitizen
from commitizen.defaults import Questions
Expand Down Expand Up @@ -43,18 +44,11 @@ def questions(self) -> Questions:
},
]

def message(self, answers: dict) -> str:
def message(self, answers: Mapping[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:
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"
Expand All @@ -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 ""


Expand Down
4 changes: 3 additions & 1 deletion tests/test_cz_base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Mapping

import pytest

from commitizen.cz.base import BaseCommitizen
Expand All @@ -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"]


Expand Down