Skip to content

Commit f0bdd55

Browse files
committed
refactor(bump): TypedDict for bump argument
1 parent 32836b2 commit f0bdd55

File tree

4 files changed

+97
-74
lines changed

4 files changed

+97
-74
lines changed

commitizen/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ def main() -> None:
680680
)
681681
sys.excepthook = no_raise_debug_excepthook
682682

683-
args.func(conf, arguments)()
683+
args.func(conf, arguments)() # type: ignore
684684

685685

686686
if __name__ == "__main__":

commitizen/commands/bump.py

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,65 @@
3737
logger = getLogger("commitizen")
3838

3939

40+
class BumpArguments(Settings, total=False):
41+
allow_no_commit: bool | None
42+
annotated_tag_message: str | None
43+
annotated_tag: bool
44+
build_metadata: str | None
45+
changelog_to_stdout: bool
46+
changelog: bool
47+
check_consistency: bool
48+
devrelease: int | None
49+
dry_run: bool
50+
file_name: str
51+
files_only: bool | None
52+
get_next: bool
53+
git_output_to_stderr: bool
54+
gpg_sign: bool
55+
increment_mode: str
56+
increment: Increment | None
57+
local_version: bool
58+
manual_version: str | None
59+
no_verify: bool
60+
prerelease: Prerelease | None
61+
retry: bool
62+
yes: bool
63+
64+
4065
class Bump:
4166
"""Show prompt for the user to create a guided commit."""
4267

43-
def __init__(self, config: BaseConfig, arguments: dict) -> None:
68+
def __init__(self, config: BaseConfig, arguments: BumpArguments) -> None:
4469
if not git.is_git_project():
4570
raise NotAGitProjectError()
4671

4772
self.config: BaseConfig = config
4873
self.encoding = config.settings["encoding"]
49-
self.arguments: dict = arguments
50-
self.bump_settings: dict = {
51-
**config.settings,
52-
**{
53-
key: arguments[key]
54-
for key in [
55-
"tag_format",
56-
"prerelease",
57-
"increment",
58-
"increment_mode",
59-
"bump_message",
60-
"gpg_sign",
61-
"annotated_tag",
62-
"annotated_tag_message",
63-
"major_version_zero",
64-
"prerelease_offset",
65-
"template",
66-
"file_name",
67-
]
68-
if arguments.get(key) is not None
74+
self.arguments = arguments
75+
self.bump_settings = cast(
76+
BumpArguments,
77+
{
78+
**config.settings,
79+
**{
80+
k: v
81+
for k, v in {
82+
"annotated_tag_message": arguments.get("annotated_tag_message"),
83+
"annotated_tag": arguments.get("annotated_tag"),
84+
"bump_message": arguments.get("bump_message"),
85+
"file_name": arguments.get("file_name"),
86+
"gpg_sign": arguments.get("gpg_sign"),
87+
"increment_mode": arguments.get("increment_mode"),
88+
"increment": arguments.get("increment"),
89+
"major_version_zero": arguments.get("major_version_zero"),
90+
"prerelease_offset": arguments.get("prerelease_offset"),
91+
"prerelease": arguments.get("prerelease"),
92+
"tag_format": arguments.get("tag_format"),
93+
"template": arguments.get("template"),
94+
}.items()
95+
if v is not None
96+
},
6997
},
70-
}
98+
)
7199
self.cz = factory.committer_factory(self.config)
72100
self.changelog_flag = arguments["changelog"]
73101
self.changelog_config = self.config.settings.get("update_changelog_on_bump")
@@ -120,11 +148,10 @@ def _is_initial_tag(
120148

121149
def _find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
122150
# Update the bump map to ensure major version doesn't increment.
123-
is_major_version_zero: bool = self.bump_settings["major_version_zero"]
124151
# self.cz.bump_map = defaults.bump_map_major_version_zero
125152
bump_map = (
126153
self.cz.bump_map_major_version_zero
127-
if is_major_version_zero
154+
if self.bump_settings["major_version_zero"]
128155
else self.cz.bump_map
129156
)
130157
bump_pattern = self.cz.bump_pattern
@@ -144,23 +171,14 @@ def __call__(self) -> None:
144171
except TypeError:
145172
raise NoVersionSpecifiedError()
146173

147-
bump_commit_message: str | None = self.bump_settings["bump_message"]
148-
version_files: list[str] = self.bump_settings["version_files"]
149-
major_version_zero: bool = self.bump_settings["major_version_zero"]
150-
prerelease_offset: int = self.bump_settings["prerelease_offset"]
151-
152-
dry_run: bool = self.arguments["dry_run"]
153-
is_yes: bool = self.arguments["yes"]
154-
increment: Increment | None = self.arguments["increment"]
155-
prerelease: Prerelease | None = self.arguments["prerelease"]
156-
devrelease: int | None = self.arguments["devrelease"]
157-
is_files_only: bool | None = self.arguments["files_only"]
158-
is_local_version: bool = self.arguments["local_version"]
174+
increment = self.arguments["increment"]
175+
prerelease = self.arguments["prerelease"]
176+
devrelease = self.arguments["devrelease"]
177+
is_local_version = self.arguments["local_version"]
159178
manual_version = self.arguments["manual_version"]
160179
build_metadata = self.arguments["build_metadata"]
161-
increment_mode: str = self.arguments["increment_mode"]
162-
get_next: bool = self.arguments["get_next"]
163-
allow_no_commit: bool | None = self.arguments["allow_no_commit"]
180+
get_next = self.arguments["get_next"]
181+
allow_no_commit = self.arguments["allow_no_commit"]
164182

165183
if manual_version:
166184
if increment:
@@ -182,15 +200,15 @@ def __call__(self) -> None:
182200
"--build-metadata cannot be combined with MANUAL_VERSION"
183201
)
184202

185-
if major_version_zero:
203+
if self.bump_settings["major_version_zero"]:
186204
raise NotAllowed(
187205
"--major-version-zero cannot be combined with MANUAL_VERSION"
188206
)
189207

190208
if get_next:
191209
raise NotAllowed("--get-next cannot be combined with MANUAL_VERSION")
192210

193-
if major_version_zero:
211+
if self.bump_settings["major_version_zero"]:
194212
if not current_version.release[0] == 0:
195213
raise NotAllowed(
196214
f"--major-version-zero is meaningless for current version {current_version}"
@@ -215,7 +233,7 @@ def __call__(self) -> None:
215233
else:
216234
# If user specified changelog_to_stdout, they probably want the
217235
# changelog to be generated as well, this is the most intuitive solution
218-
self.changelog_flag = (
236+
self.changelog_flag = bool(
219237
self.changelog_flag
220238
or bool(self.changelog_to_stdout)
221239
or self.changelog_config
@@ -227,7 +245,7 @@ def __call__(self) -> None:
227245
current_tag, "name", rules.normalize_tag(current_version)
228246
)
229247

230-
is_initial = self._is_initial_tag(current_tag, is_yes)
248+
is_initial = self._is_initial_tag(current_tag, self.arguments["yes"])
231249

232250
if manual_version:
233251
try:
@@ -273,16 +291,16 @@ def __call__(self) -> None:
273291
new_version = current_version.bump(
274292
increment,
275293
prerelease=prerelease,
276-
prerelease_offset=prerelease_offset,
294+
prerelease_offset=self.bump_settings["prerelease_offset"],
277295
devrelease=devrelease,
278296
is_local_version=is_local_version,
279297
build_metadata=build_metadata,
280-
exact_increment=increment_mode == "exact",
298+
exact_increment=self.arguments["increment_mode"] == "exact",
281299
)
282300

283301
new_tag_version = rules.normalize_tag(new_version)
284302
message = bump.create_commit_message(
285-
current_version, new_version, bump_commit_message
303+
current_version, new_version, self.bump_settings["bump_message"]
286304
)
287305

288306
if get_next:
@@ -314,6 +332,7 @@ def __call__(self) -> None:
314332
)
315333

316334
files: list[str] = []
335+
dry_run = self.arguments["dry_run"]
317336
if self.changelog_flag:
318337
args = {
319338
"unreleased_version": new_tag_version,
@@ -342,7 +361,7 @@ def __call__(self) -> None:
342361
bump.update_version_in_files(
343362
str(current_version),
344363
str(new_version),
345-
version_files,
364+
self.bump_settings["version_files"],
346365
check_consistency=self.check_consistency,
347366
encoding=self.encoding,
348367
)
@@ -366,7 +385,7 @@ def __call__(self) -> None:
366385
else None,
367386
)
368387

369-
if is_files_only:
388+
if self.arguments["files_only"]:
370389
raise ExpectedExit()
371390

372391
# FIXME: check if any changes have been staged
@@ -395,11 +414,15 @@ def __call__(self) -> None:
395414

396415
c = git.tag(
397416
new_tag_version,
398-
signed=self.bump_settings.get("gpg_sign", False)
399-
or bool(self.config.settings.get("gpg_sign", False)),
400-
annotated=self.bump_settings.get("annotated_tag", False)
401-
or bool(self.config.settings.get("annotated_tag", False))
402-
or bool(self.bump_settings.get("annotated_tag_message", False)),
417+
signed=bool(
418+
self.bump_settings.get("gpg_sign")
419+
or self.config.settings.get("gpg_sign")
420+
),
421+
annotated=bool(
422+
self.bump_settings.get("annotated_tag")
423+
or self.config.settings.get("annotated_tag")
424+
or self.bump_settings.get("annotated_tag_message")
425+
),
403426
msg=self.bump_settings.get("annotated_tag_message", None),
404427
# TODO: also get from self.config.settings?
405428
)

commitizen/defaults.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,36 @@ class CzSettings(TypedDict, total=False):
2929

3030

3131
class Settings(TypedDict, total=False):
32-
name: str
33-
version: str | None
34-
version_files: list[str]
35-
version_provider: str | None
36-
version_scheme: str | None
37-
version_type: str | None
38-
tag_format: str
39-
legacy_tag_formats: Sequence[str]
40-
ignored_tag_formats: Sequence[str]
41-
bump_message: str | None
42-
retry_after_failure: bool
4332
allow_abort: bool
4433
allowed_prefixes: list[str]
34+
always_signoff: bool
35+
bump_message: str | None
4536
changelog_file: str
4637
changelog_format: str | None
4738
changelog_incremental: bool
48-
changelog_start_rev: str | None
4939
changelog_merge_prerelease: bool
50-
update_changelog_on_bump: bool
51-
use_shortcuts: bool
52-
style: list[tuple[str, str]]
40+
changelog_start_rev: str | None
5341
customize: CzSettings
42+
encoding: str
43+
extras: dict[str, Any]
44+
ignored_tag_formats: Sequence[str]
45+
legacy_tag_formats: Sequence[str]
5446
major_version_zero: bool
55-
pre_bump_hooks: list[str] | None
47+
name: str
5648
post_bump_hooks: list[str] | None
49+
pre_bump_hooks: list[str] | None
5750
prerelease_offset: int
58-
encoding: str
59-
always_signoff: bool
51+
retry_after_failure: bool
52+
style: list[tuple[str, str]]
53+
tag_format: str
6054
template: str | None
61-
extras: dict[str, Any]
55+
update_changelog_on_bump: bool
56+
use_shortcuts: bool
57+
version_files: list[str]
58+
version_provider: str | None
59+
version_scheme: str | None
60+
version_type: str | None
61+
version: str | None
6262

6363

6464
CONFIG_FILES: list[str] = [

tests/commands/test_bump_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ def test_is_initial_tag(mocker: MockFixture, tmp_commitizen_project):
17211721
"extras": None,
17221722
}
17231723

1724-
bump_cmd = bump.Bump(config, arguments)
1724+
bump_cmd = bump.Bump(config, arguments) # type: ignore
17251725

17261726
# Test case 1: No current tag, not yes mode
17271727
mocker.patch("questionary.confirm", return_value=mocker.Mock(ask=lambda: True))

0 commit comments

Comments
 (0)