Skip to content

Commit f8f8daf

Browse files
refactor: address review comments on PR #1932
- tests/test_cz_conventional_commits.py: use type annotations instead of cast() for answers dicts (avoids ty limitation with dict literal inference) - config/__init__.py: widen read_cfg to accept str | Path | None - tests/test_conf.py: revert str() wrapper now that read_cfg accepts Path - changelog.py: add comment explaining ty limitation requiring cast - version_schemes.py: add comment explaining ty limitation requiring cast Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a3728cf commit f8f8daf

5 files changed

Lines changed: 12 additions & 12 deletions

File tree

commitizen/changelog.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def process_commit_message(
194194
for msg in messages:
195195
if not isinstance(msg, dict):
196196
continue
197+
# cast needed: ty cannot narrow dict type from Iterable union
197198
msg_dict = cast("dict[str, Any]", msg)
198199
change_type = msg_dict.pop("change_type", None)
199200
if change_type_map and change_type:

commitizen/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _create_config_from_path(path: Path) -> BaseConfig:
3131
return create_config(data=path.read_bytes(), path=path)
3232

3333

34-
def read_cfg(filepath: str | None = None) -> BaseConfig:
34+
def read_cfg(filepath: str | Path | None = None) -> BaseConfig:
3535
if filepath is not None:
3636
conf_path = Path(filepath)
3737
if not conf_path.is_file():

commitizen/version_schemes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def _get_prerelease(self) -> str:
385385
return ".".join(prerelease_parts)
386386

387387

388+
# cast needed: ty cannot resolve type[Pep440] as type[VersionProtocol]
388389
DEFAULT_SCHEME: VersionScheme = cast("VersionScheme", Pep440)
389390

390391
SCHEMES_ENTRYPOINT = "commitizen.scheme"

tests/test_conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def test_no_warn_with_explicit_config_path(self, tmp_path, monkeypatch, capsys):
346346
(tmp_path / ".cz.json").write_text(JSON_STR)
347347

348348
# Read config with explicit path
349-
cfg = config.read_cfg(str(Path(".cz.json")))
349+
cfg = config.read_cfg(Path(".cz.json"))
350350

351351
# No warning should be issued
352352
captured = capsys.readouterr()

tests/test_cz_conventional_commits.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import cast
2-
31
import pytest
42

53
from commitizen.cz.conventional_commits.conventional_commits import (
@@ -61,29 +59,29 @@ def test_choices_all_have_keyboard_shortcuts(config):
6159

6260
def test_small_answer(config):
6361
conventional_commits = ConventionalCommitsCz(config)
64-
answers = {
62+
answers: ConventionalCommitsAnswers = {
6563
"prefix": "fix",
6664
"scope": "users",
6765
"subject": "email pattern corrected",
6866
"is_breaking_change": False,
6967
"body": "",
7068
"footer": "",
7169
}
72-
message = conventional_commits.message(cast("ConventionalCommitsAnswers", answers))
70+
message = conventional_commits.message(answers)
7371
assert message == "fix(users): email pattern corrected"
7472

7573

7674
def test_long_answer(config):
7775
conventional_commits = ConventionalCommitsCz(config)
78-
answers = {
76+
answers: ConventionalCommitsAnswers = {
7977
"prefix": "fix",
8078
"scope": "users",
8179
"subject": "email pattern corrected",
8280
"is_breaking_change": False,
8381
"body": "complete content",
8482
"footer": "closes #24",
8583
}
86-
message = conventional_commits.message(cast("ConventionalCommitsAnswers", answers))
84+
message = conventional_commits.message(answers)
8785
assert (
8886
message
8987
== "fix(users): email pattern corrected\n\ncomplete content\n\ncloses #24"
@@ -92,15 +90,15 @@ def test_long_answer(config):
9290

9391
def test_breaking_change_in_footer(config):
9492
conventional_commits = ConventionalCommitsCz(config)
95-
answers = {
93+
answers: ConventionalCommitsAnswers = {
9694
"prefix": "fix",
9795
"scope": "users",
9896
"subject": "email pattern corrected",
9997
"is_breaking_change": True,
10098
"body": "complete content",
10199
"footer": "migrate by renaming user to users",
102100
}
103-
message = conventional_commits.message(cast("ConventionalCommitsAnswers", answers))
101+
message = conventional_commits.message(answers)
104102
print(message)
105103
assert (
106104
message
@@ -145,15 +143,15 @@ def test_breaking_change_message_formats(
145143
breaking_change_exclamation_in_title
146144
)
147145
conventional_commits = ConventionalCommitsCz(config)
148-
answers = {
146+
answers: ConventionalCommitsAnswers = {
149147
"prefix": "feat",
150148
"scope": scope,
151149
"subject": "email pattern corrected",
152150
"is_breaking_change": True,
153151
"body": "complete content",
154152
"footer": "migrate by renaming user to users",
155153
}
156-
message = conventional_commits.message(cast("ConventionalCommitsAnswers", answers))
154+
message = conventional_commits.message(answers)
157155
assert message == expected_message
158156

159157

0 commit comments

Comments
 (0)