Skip to content

Commit cfdc327

Browse files
adam-grant-hendryLee-W
authored andcommitted
fix: add missing encoding parameter
Add `encoding` parameter to `open` and `smart_open` method calls where missed. Use `defaults.encoding` as default.
1 parent 682bcc8 commit cfdc327

File tree

7 files changed

+23
-14
lines changed

7 files changed

+23
-14
lines changed

commitizen/bump.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import OrderedDict
66
from string import Template
77

8-
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message
8+
from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message, encoding
99
from commitizen.exceptions import CurrentVersionNotFoundError
1010
from commitizen.git import GitCommit, smart_open
1111
from commitizen.version_schemes import DEFAULT_SCHEME, Version, VersionScheme
@@ -48,9 +48,9 @@ def update_version_in_files(
4848
current_version: str,
4949
new_version: str,
5050
files: list[str],
51-
encoding: str,
5251
*,
5352
check_consistency=False,
53+
encoding: str = encoding,
5454
) -> None:
5555
"""Change old version to the new one in every file given.
5656
@@ -71,7 +71,7 @@ def update_version_in_files(
7171
current_version,
7272
new_version,
7373
regex,
74-
encoding,
74+
encoding=encoding,
7575
)
7676

7777
if check_consistency and not current_version_found:
@@ -91,7 +91,7 @@ def _bump_with_regex(
9191
current_version: str,
9292
new_version: str,
9393
regex: str,
94-
encoding: str,
94+
encoding: str = encoding,
9595
) -> tuple[bool, str]:
9696
current_version_found = False
9797
lines = []

commitizen/changelog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
from commitizen import out
3838
from commitizen.bump import normalize_tag
39+
from commitizen.defaults import encoding
3940
from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError
4041
from commitizen.git import GitCommit, GitTag
4142
from commitizen.version_schemes import (
@@ -214,7 +215,7 @@ def parse_title_type_of_line(value: str) -> str | None:
214215

215216

216217
def get_metadata(
217-
filepath: str, scheme: VersionScheme = Pep440, encoding: str = "utf-8"
218+
filepath: str, scheme: VersionScheme = Pep440, encoding: str = encoding
218219
) -> dict:
219220
unreleased_start: int | None = None
220221
unreleased_end: int | None = None

commitizen/changelog_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from collections import defaultdict
1414
from typing import Generator, Iterable
1515

16+
from commitizen.defaults import encoding
17+
1618
MD_VERSION_RE = r"^##\s(?P<version>[a-zA-Z0-9.+]+)\s?\(?(?P<date>[0-9-]+)?\)?"
1719
MD_CHANGE_TYPE_RE = r"^###\s(?P<change_type>[a-zA-Z0-9.+\s]+)"
1820
MD_MESSAGE_RE = (
@@ -36,7 +38,7 @@
3638
]
3739

3840

39-
def find_version_blocks(filepath: str, encoding: str) -> Generator:
41+
def find_version_blocks(filepath: str, encoding: str = encoding) -> Generator:
4042
"""Find version block (version block: contains all the information about a version.)
4143
4244
E.g:

commitizen/commands/bump.py

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

33
import os
4-
from logging import getLogger
54
import warnings
5+
from logging import getLogger
66

77
import questionary
88

@@ -23,7 +23,7 @@
2323
NoVersionSpecifiedError,
2424
)
2525
from commitizen.providers import get_provider
26-
from commitizen.version_schemes import get_version_scheme, InvalidVersion
26+
from commitizen.version_schemes import InvalidVersion, get_version_scheme
2727

2828
logger = getLogger("commitizen")
2929

@@ -298,8 +298,8 @@ def __call__(self): # noqa: C901
298298
str(current_version),
299299
str(new_version),
300300
version_files,
301-
self.encoding,
302301
check_consistency=self.check_consistency,
302+
encoding=self.encoding,
303303
)
304304

305305
provider.set_version(str(new_version))

commitizen/commands/changelog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __call__(self):
145145
changelog_meta = changelog.get_metadata(
146146
self.file_name,
147147
self.scheme,
148-
self.encoding,
148+
encoding=self.encoding,
149149
)
150150
latest_version = changelog_meta.get("latest_version")
151151
if latest_version:

commitizen/commands/commit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __call__(self):
8383
out.info(f"\n{m}\n")
8484

8585
if write_message_to_file:
86-
with smart_open(write_message_to_file, "w") as file:
86+
with smart_open(write_message_to_file, "w", encoding=self.encoding) as file:
8787
file.write(m)
8888

8989
if dry_run:

tests/test_changelog_parser.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ def existing_changelog_file(tmpdir):
4747

4848

4949
def test_read_changelog_blocks(existing_changelog_file):
50-
blocks = changelog_parser.find_version_blocks(existing_changelog_file, "utf-8")
50+
blocks = changelog_parser.find_version_blocks(
51+
existing_changelog_file, encoding="utf-8"
52+
)
5153
blocks = list(blocks)
5254
amount_of_blocks = len(blocks)
5355
assert amount_of_blocks == 2
@@ -127,7 +129,9 @@ def test_transform_change_type_fail():
127129

128130

129131
def test_generate_block_tree(existing_changelog_file):
130-
blocks = changelog_parser.find_version_blocks(existing_changelog_file, "utf-8")
132+
blocks = changelog_parser.find_version_blocks(
133+
existing_changelog_file, encoding="utf-8"
134+
)
131135
block = next(blocks)
132136
tree = changelog_parser.generate_block_tree(block)
133137
assert tree == {
@@ -157,7 +161,9 @@ def test_generate_block_tree(existing_changelog_file):
157161

158162

159163
def test_generate_full_tree(existing_changelog_file):
160-
blocks = changelog_parser.find_version_blocks(existing_changelog_file, "utf-8")
164+
blocks = changelog_parser.find_version_blocks(
165+
existing_changelog_file, encoding="utf-8"
166+
)
161167
tree = list(changelog_parser.generate_full_tree(blocks))
162168

163169
assert tree == [

0 commit comments

Comments
 (0)