Skip to content

Commit 3e9e789

Browse files
committed
refactor(config): add CzSettings and Questions TypedDict
1 parent 71a13ee commit 3e9e789

File tree

7 files changed

+50
-20
lines changed

7 files changed

+50
-20
lines changed

commitizen/commands/init.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __call__(self):
4949
out.line(f"Config file {self.config.path} already exists")
5050

5151
def _ask_config_path(self) -> str:
52-
name = questionary.select(
52+
name: str = questionary.select(
5353
"Please choose a supported config file: (default: pyproject.toml)",
5454
choices=config_files,
5555
default="pyproject.toml",
@@ -58,7 +58,7 @@ def _ask_config_path(self) -> str:
5858
return name
5959

6060
def _ask_name(self) -> str:
61-
name = questionary.select(
61+
name: str = questionary.select(
6262
"Please choose a cz (commit rule): (default: cz_conventional_commits)",
6363
choices=list(registry.keys()),
6464
default="cz_conventional_commits",

commitizen/cz/base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
from commitizen import git
77
from commitizen.config.base_config import BaseConfig
8+
from commitizen.defaults import Questions
89

910

1011
class BaseCommitizen(metaclass=ABCMeta):
1112
bump_pattern: Optional[str] = None
12-
bump_map: Optional[dict] = None
13+
bump_map: Optional[Dict[str, str]] = None
1314
default_style_config: List[Tuple[str, str]] = [
1415
("qmark", "fg:#ff9d00 bold"),
1516
("question", "bold"),
@@ -45,7 +46,7 @@ def __init__(self, config: BaseConfig):
4546
self.config.settings.update({"style": BaseCommitizen.default_style_config})
4647

4748
@abstractmethod
48-
def questions(self) -> list:
49+
def questions(self) -> Questions:
4950
"""Questions regarding the commit message."""
5051

5152
@abstractmethod

commitizen/cz/conventional_commits/conventional_commits.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
22
import re
33
from collections import OrderedDict
4-
from typing import Any, Dict, List
54

65
from commitizen import defaults
76
from commitizen.cz.base import BaseCommitizen
87
from commitizen.cz.utils import multiple_line_breaker, required_validator
8+
from commitizen.defaults import Questions
99

1010
__all__ = ["ConventionalCommitsCz"]
1111

@@ -49,8 +49,8 @@ class ConventionalCommitsCz(BaseCommitizen):
4949
"perf": "Perf",
5050
}
5151

52-
def questions(self) -> List[Dict[str, Any]]:
53-
questions: List[Dict[str, Any]] = [
52+
def questions(self) -> Questions:
53+
questions: Questions = [
5454
{
5555
"type": "list",
5656
"name": "prefix",

commitizen/cz/customize/customize.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
except ImportError:
44
from string import Template # type: ignore
55

6-
from typing import Any, Dict, List, Optional
6+
from typing import Optional
77

88
from commitizen import defaults
99
from commitizen.config import BaseConfig
1010
from commitizen.cz.base import BaseCommitizen
11+
from commitizen.defaults import Questions
1112
from commitizen.exceptions import MissingCzCustomizeConfigError
1213

1314
__all__ = ["CustomizeCommitsCz"]
@@ -37,11 +38,11 @@ def __init__(self, config: BaseConfig):
3738
if custom_change_type_order:
3839
self.change_type_order = custom_change_type_order
3940

40-
def questions(self) -> List[Dict[str, Any]]:
41-
return self.custom_settings.get("questions")
41+
def questions(self) -> Questions:
42+
return self.custom_settings.get("questions", [{}])
4243

4344
def message(self, answers: dict) -> str:
44-
message_template = Template(self.custom_settings.get("message_template"))
45+
message_template = Template(self.custom_settings.get("message_template", ""))
4546
if getattr(Template, "substitute", None):
4647
return message_template.substitute(**answers) # type: ignore
4748
else:

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
2-
from typing import Any, Dict, List
32

43
from commitizen.cz.base import BaseCommitizen
4+
from commitizen.defaults import Questions
55

66
__all__ = ["JiraSmartCz"]
77

88

99
class JiraSmartCz(BaseCommitizen):
10-
def questions(self) -> List[Dict[str, Any]]:
10+
def questions(self) -> Questions:
1111
questions = [
1212
{
1313
"type": "input",

commitizen/defaults.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
from collections import OrderedDict
2-
from typing import Any, List, Optional, Tuple, TypedDict
3-
1+
import collections
2+
import pathlib
3+
from typing import (
4+
Any,
5+
Iterable,
6+
List,
7+
MutableMapping,
8+
Optional,
9+
OrderedDict,
10+
Tuple,
11+
TypedDict,
12+
Union,
13+
)
414

515
# Type
16+
Questions = Iterable[MutableMapping[str, Any]]
17+
18+
19+
class CzSettings(TypedDict, total=False):
20+
bump_pattern: str
21+
bump_map: OrderedDict[str, str]
22+
change_type_order: List[str]
23+
24+
questions: Questions
25+
example: Optional[str]
26+
schema_pattern: Optional[str]
27+
schema: Optional[str]
28+
info_path: Union[str, pathlib.Path]
29+
info: str
30+
message_template: str
31+
32+
633
class Settings(TypedDict, total=False):
734
name: str
835
version: Optional[str]
@@ -15,7 +42,7 @@ class Settings(TypedDict, total=False):
1542
update_changelog_on_bump: bool
1643
use_shortcuts: bool
1744
style: Optional[List[Tuple[str, str]]]
18-
customize: Any
45+
customize: CzSettings
1946

2047

2148
name: str = "cz_conventional_commits"
@@ -46,7 +73,7 @@ class Settings(TypedDict, total=False):
4673
PATCH = "PATCH"
4774

4875
bump_pattern = r"^(BREAKING[\-\ ]CHANGE|feat|fix|refactor|perf)(\(.+\))?(!)?"
49-
bump_map = OrderedDict(
76+
bump_map = collections.OrderedDict(
5077
(
5178
(r"^.+!$", MAJOR),
5279
(r"^BREAKING[\-\ ]CHANGE", MAJOR),
@@ -56,9 +83,10 @@ class Settings(TypedDict, total=False):
5683
(r"^perf", PATCH),
5784
)
5885
)
86+
change_type_order = ["BREAKING CHANGE", "feat", "fix", "refactor", "perf"]
87+
5988
bump_message = "bump: version $current_version → $new_version"
6089

61-
change_type_order = ["BREAKING CHANGE", "feat", "fix", "refactor", "perf"]
6290

6391
commit_parser = r"^(?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?:\s(?P<message>.*)?" # noqa
6492
version_parser = r"(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?(\w+)?)"

commitizen/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class GitObject:
1414
def __eq__(self, other) -> bool:
1515
if not hasattr(other, "rev"):
1616
return False
17-
return self.rev == other.rev
17+
return self.rev == other.rev # type: ignore
1818

1919

2020
class GitCommit(GitObject):

0 commit comments

Comments
 (0)