Skip to content

Commit 2c3d5d0

Browse files
committed
style: type untyped methods
1 parent 4892945 commit 2c3d5d0

17 files changed

+81
-36
lines changed

commitizen/changelog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Metadata:
6363
latest_version_position: int | None = None
6464
latest_version_tag: str | None = None
6565

66-
def __post_init__(self):
66+
def __post_init__(self) -> None:
6767
if self.latest_version and not self.latest_version_tag:
6868
# Test syntactic sugar
6969
# latest version tag is optional if same as latest version
@@ -169,8 +169,8 @@ def process_commit_message(
169169
commit: GitCommit,
170170
changes: dict[str | None, list],
171171
change_type_map: dict[str, str] | None = None,
172-
):
173-
message: dict = {
172+
) -> None:
173+
message: dict[str, Any] = {
174174
"sha1": commit.rev,
175175
"parents": commit.parents,
176176
"author": commit.author,

commitizen/commands/changelog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def _find_incremental_rev(self, latest_version: str, tags: Iterable[GitTag]) ->
138138

139139
def _write_changelog(
140140
self, changelog_out: str, lines: list[str], changelog_meta: changelog.Metadata
141-
):
141+
) -> None:
142142
with smart_open(self.file_name, "w", encoding=self.encoding) as changelog_file:
143143
partial_changelog: str | None = None
144144
if self.incremental:

commitizen/commands/example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
class Example:
66
"""Show an example so people understands the rules."""
77

8-
def __init__(self, config: BaseConfig, *args):
8+
def __init__(self, config: BaseConfig, *args: object):
99
self.config: BaseConfig = config
1010
self.cz = factory.committer_factory(self.config)
1111

12-
def __call__(self):
12+
def __call__(self) -> None:
1313
out.write(self.cz.example())

commitizen/commands/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def is_pre_commit_installed(self) -> bool:
7979

8080

8181
class Init:
82-
def __init__(self, config: BaseConfig, *args):
82+
def __init__(self, config: BaseConfig, *args: object):
8383
self.config: BaseConfig = config
8484
self.encoding = config.settings["encoding"]
8585
self.cz = factory.committer_factory(self.config)

commitizen/commands/schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
class Schema:
66
"""Show structure of the rule."""
77

8-
def __init__(self, config: BaseConfig, *args):
8+
def __init__(self, config: BaseConfig, *args: object):
99
self.config: BaseConfig = config
1010
self.cz = factory.committer_factory(self.config)
1111

12-
def __call__(self):
12+
def __call__(self) -> None:
1313
out.write(self.cz.schema())

commitizen/config/base_config.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
from __future__ import annotations
22

3+
import sys
34
from pathlib import Path
5+
from typing import TYPE_CHECKING, Any
46

57
from commitizen.defaults import DEFAULT_SETTINGS, Settings
68

9+
if TYPE_CHECKING:
10+
import sys
11+
12+
# Self is Python 3.11+ but backported in typing-extensions
13+
if sys.version_info < (3, 11):
14+
from typing_extensions import Self
15+
else:
16+
from typing import Self
17+
718

819
class BaseConfig:
920
def __init__(self) -> None:
@@ -28,7 +39,7 @@ def path(self, path: str | Path) -> None:
2839
"""
2940
self._path = Path(path)
3041

31-
def set_key(self, key, value):
42+
def set_key(self, key: str, value: Any) -> Self:
3243
"""Set or update a key in the conf.
3344
3445
For now only strings are supported.

commitizen/config/json_config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
from __future__ import annotations
22

33
import json
4+
import sys
45
from pathlib import Path
6+
from typing import TYPE_CHECKING, Any
57

68
from commitizen.exceptions import InvalidConfigurationError
79
from commitizen.git import smart_open
810

911
from .base_config import BaseConfig
1012

13+
if TYPE_CHECKING:
14+
import sys
15+
16+
# Self is Python 3.11+ but backported in typing-extensions
17+
if sys.version_info < (3, 11):
18+
from typing_extensions import Self
19+
else:
20+
from typing import Self
21+
1122

1223
class JsonConfig(BaseConfig):
1324
def __init__(self, *, data: bytes | str, path: Path | str):
1425
super().__init__()
1526
self.is_empty_config = False
16-
self.path = path # type: ignore
27+
self.path: Path = path # type: ignore
1728
self._parse_setting(data)
1829

19-
def init_empty_config_content(self):
30+
def init_empty_config_content(self) -> None:
2031
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
2132
json.dump({"commitizen": {}}, json_file)
2233

23-
def set_key(self, key, value):
34+
def set_key(self, key: str, value: Any) -> Self:
2435
"""Set or update a key in the conf.
2536
2637
For now only strings are supported.

commitizen/config/toml_config.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
from __future__ import annotations
22

33
import os
4+
import sys
45
from pathlib import Path
6+
from typing import TYPE_CHECKING, Any
57

68
from tomlkit import exceptions, parse, table
79

810
from commitizen.exceptions import InvalidConfigurationError
911

1012
from .base_config import BaseConfig
1113

14+
if TYPE_CHECKING:
15+
import sys
16+
17+
# Self is Python 3.11+ but backported in typing-extensions
18+
if sys.version_info < (3, 11):
19+
from typing_extensions import Self
20+
else:
21+
from typing import Self
22+
1223

1324
class TomlConfig(BaseConfig):
1425
def __init__(self, *, data: bytes | str, path: Path | str):
1526
super().__init__()
1627
self.is_empty_config = False
17-
self.path = path # type: ignore
28+
self.path: Path = path # type: ignore
1829
self._parse_setting(data)
1930

20-
def init_empty_config_content(self):
31+
def init_empty_config_content(self) -> None:
2132
if os.path.isfile(self.path):
2233
with open(self.path, "rb") as input_toml_file:
2334
parser = parse(input_toml_file.read())
@@ -27,10 +38,10 @@ def init_empty_config_content(self):
2738
with open(self.path, "wb") as output_toml_file:
2839
if parser.get("tool") is None:
2940
parser["tool"] = table()
30-
parser["tool"]["commitizen"] = table()
41+
parser["tool"]["commitizen"] = table() # type: ignore
3142
output_toml_file.write(parser.as_string().encode(self.encoding))
3243

33-
def set_key(self, key, value):
44+
def set_key(self, key: str, value: Any) -> Self:
3445
"""Set or update a key in the conf.
3546
3647
For now only strings are supported.
@@ -39,7 +50,7 @@ def set_key(self, key, value):
3950
with open(self.path, "rb") as f:
4051
parser = parse(f.read())
4152

42-
parser["tool"]["commitizen"][key] = value
53+
parser["tool"]["commitizen"][key] = value # type: ignore
4354
with open(self.path, "wb") as f:
4455
f.write(parser.as_string().encode(self.encoding))
4556
return self

commitizen/config/yaml_config.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
22

3+
import sys
34
from pathlib import Path
5+
from typing import TYPE_CHECKING, Any
46

57
import yaml
68

@@ -9,15 +11,24 @@
911

1012
from .base_config import BaseConfig
1113

14+
if TYPE_CHECKING:
15+
import sys
16+
17+
# Self is Python 3.11+ but backported in typing-extensions
18+
if sys.version_info < (3, 11):
19+
from typing_extensions import Self
20+
else:
21+
from typing import Self
22+
1223

1324
class YAMLConfig(BaseConfig):
1425
def __init__(self, *, data: bytes | str, path: Path | str):
1526
super().__init__()
1627
self.is_empty_config = False
17-
self.path = path # type: ignore
28+
self.path: Path = path # type: ignore
1829
self._parse_setting(data)
1930

20-
def init_empty_config_content(self):
31+
def init_empty_config_content(self) -> None:
2132
with smart_open(self.path, "a", encoding=self.encoding) as json_file:
2233
yaml.dump({"commitizen": {}}, json_file, explicit_start=True)
2334

@@ -41,7 +52,7 @@ def _parse_setting(self, data: bytes | str) -> None:
4152
except (KeyError, TypeError):
4253
self.is_empty_config = True
4354

44-
def set_key(self, key, value):
55+
def set_key(self, key: str, value: Any) -> Self:
4556
"""Set or update a key in the conf.
4657
4758
For now only strings are supported.

commitizen/cz/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ def message(self, answers: dict) -> str:
7676
"""Format your git message."""
7777

7878
@property
79-
def style(self):
79+
def style(self) -> Style:
8080
return merge_styles(
8181
[
8282
Style(BaseCommitizen.default_style_config),
8383
Style(self.config.settings["style"]),
8484
]
85-
)
85+
) # type: ignore[return-value]
8686

8787
def example(self) -> str:
8888
"""Example of the commit message."""

commitizen/git.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def get_commits(
215215
]
216216

217217

218-
def get_filenames_in_commit(git_reference: str = ""):
218+
def get_filenames_in_commit(git_reference: str = "") -> list[str]:
219219
"""Get the list of files that were committed in the requested git reference.
220220
221221
:param git_reference: a git reference as accepted by `git show`, default: the last commit
@@ -312,7 +312,7 @@ def get_core_editor() -> str | None:
312312
return None
313313

314314

315-
def smart_open(*args, **kwargs):
315+
def smart_open(*args, **kwargs): # type: ignore[no-untyped-def,unused-ignore]
316316
"""Open a file with the EOL style determined from Git."""
317317
return open(*args, newline=EOLType.for_open(), **kwargs)
318318

commitizen/hooks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import annotations
22

33
import os
4+
from collections.abc import Mapping
45

56
from commitizen import cmd, out
67
from commitizen.exceptions import RunHookError
78

89

9-
def run(hooks, _env_prefix="CZ_", **env):
10+
def run(hooks: str | list[str], _env_prefix: str = "CZ_", **env: object) -> None:
1011
if isinstance(hooks, str):
1112
hooks = [hooks]
1213

@@ -24,7 +25,7 @@ def run(hooks, _env_prefix="CZ_", **env):
2425
raise RunHookError(f"Running hook '{hook}' failed")
2526

2627

27-
def _format_env(prefix: str, env: dict[str, str]) -> dict[str, str]:
28+
def _format_env(prefix: str, env: Mapping[str, object]) -> dict[str, str]:
2829
"""_format_env() prefixes all given environment variables with the given
2930
prefix so it can be passed directly to cmd.run()."""
3031
penv = dict(os.environ)

commitizen/providers/base_provider.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_version(self) -> str:
2929
"""
3030

3131
@abstractmethod
32-
def set_version(self, version: str):
32+
def set_version(self, version: str) -> None:
3333
"""
3434
Set the new current version
3535
"""
@@ -58,15 +58,15 @@ def get_version(self) -> str:
5858
document = json.loads(self.file.read_text())
5959
return self.get(document)
6060

61-
def set_version(self, version: str):
61+
def set_version(self, version: str) -> None:
6262
document = json.loads(self.file.read_text())
6363
self.set(document, version)
6464
self.file.write_text(json.dumps(document, indent=self.indent) + "\n")
6565

6666
def get(self, document: dict[str, Any]) -> str:
6767
return document["version"] # type: ignore
6868

69-
def set(self, document: dict[str, Any], version: str):
69+
def set(self, document: dict[str, Any], version: str) -> None:
7070
document["version"] = version
7171

7272

@@ -79,13 +79,13 @@ def get_version(self) -> str:
7979
document = tomlkit.parse(self.file.read_text())
8080
return self.get(document)
8181

82-
def set_version(self, version: str):
82+
def set_version(self, version: str) -> None:
8383
document = tomlkit.parse(self.file.read_text())
8484
self.set(document, version)
8585
self.file.write_text(tomlkit.dumps(document))
8686

8787
def get(self, document: tomlkit.TOMLDocument) -> str:
8888
return document["project"]["version"] # type: ignore
8989

90-
def set(self, document: tomlkit.TOMLDocument, version: str):
90+
def set(self, document: tomlkit.TOMLDocument, version: str) -> None:
9191
document["project"]["version"] = version # type: ignore

commitizen/providers/cargo_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get(self, document: tomlkit.TOMLDocument) -> str:
2828
...
2929
return document["workspace"]["package"]["version"] # type: ignore
3030

31-
def set(self, document: tomlkit.TOMLDocument, version: str):
31+
def set(self, document: tomlkit.TOMLDocument, version: str) -> None:
3232
try:
3333
document["workspace"]["package"]["version"] = version # type: ignore
3434
return

commitizen/providers/commitizen_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ class CommitizenProvider(VersionProvider):
1111
def get_version(self) -> str:
1212
return self.config.settings["version"] # type: ignore
1313

14-
def set_version(self, version: str):
14+
def set_version(self, version: str) -> None:
1515
self.config.set_key("version", version)

commitizen/providers/poetry_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ class PoetryProvider(TomlProvider):
1515
def get(self, pyproject: tomlkit.TOMLDocument) -> str:
1616
return pyproject["tool"]["poetry"]["version"] # type: ignore
1717

18-
def set(self, pyproject: tomlkit.TOMLDocument, version: str):
18+
def set(self, pyproject: tomlkit.TOMLDocument, version: str) -> None:
1919
pyproject["tool"]["poetry"]["version"] = version # type: ignore

commitizen/providers/scm_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def get_version(self) -> str:
2323
return "0.0.0"
2424
return str(versions[-1])
2525

26-
def set_version(self, version: str):
26+
def set_version(self, version: str) -> None:
2727
# Not necessary
2828
pass

0 commit comments

Comments
 (0)