Skip to content

Commit

Permalink
style(ruff): enable pyupgrade (UP) rules and fix them all
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre authored and Lee-W committed Oct 17, 2023
1 parent 12b214e commit a48d263
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 88 deletions.
2 changes: 1 addition & 1 deletion commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _bump_with_regex(
current_version_found = False
lines = []
pattern = re.compile(regex)
with open(version_filepath, "r", encoding=encoding) as f:
with open(version_filepath, encoding=encoding) as f:
for line in f:
if pattern.search(line):
bumped_line = line.replace(current_version, new_version)
Expand Down
6 changes: 3 additions & 3 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import re
from collections import OrderedDict, defaultdict
from datetime import date
from typing import TYPE_CHECKING, Callable, Iterable, Type, cast
from typing import TYPE_CHECKING, Callable, Iterable, cast

from jinja2 import Environment, PackageLoader

Expand Down Expand Up @@ -74,7 +74,7 @@ def tag_included_in_changelog(
return True


def get_version_tags(scheme: Type[BaseVersion], tags: list[GitTag]) -> list[GitTag]:
def get_version_tags(scheme: type[BaseVersion], tags: list[GitTag]) -> list[GitTag]:
valid_tags: list[GitTag] = []
for tag in tags:
try:
Expand Down Expand Up @@ -230,7 +230,7 @@ def get_metadata(
"latest_version_position": None,
}

with open(filepath, "r", encoding=encoding) as changelog_file:
with open(filepath, encoding=encoding) as changelog_file:
for index, line in enumerate(changelog_file):
line = line.strip().lower()

Expand Down
2 changes: 1 addition & 1 deletion commitizen/changelog_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def find_version_blocks(filepath: str, encoding: str = encoding) -> Generator:
```
"""
with open(filepath, "r", encoding=encoding) as f:
with open(filepath, encoding=encoding) as f:
block: list = []
for line in f:
line = line.strip("\n")
Expand Down
8 changes: 3 additions & 5 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,9 @@ def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool
else:
out.info(f"Tag {current_tag_version} could not be found. ")
out.info(
(
"Possible causes:\n"
"- version in configuration is not the current version\n"
"- tag_format is missing, check them using 'git tag --list'\n"
)
"Possible causes:\n"
"- version in configuration is not the current version\n"
"- tag_format is missing, check them using 'git tag --list'\n"
)
is_initial = questionary.confirm("Is this the first tag created?").ask()
return is_initial
Expand Down
2 changes: 1 addition & 1 deletion commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def __call__(self):

lines = []
if self.incremental and os.path.isfile(self.file_name):
with open(self.file_name, "r", encoding=self.encoding) as changelog_file:
with open(self.file_name, encoding=self.encoding) as changelog_file:
lines = changelog_file.readlines()

self.write_changelog(changelog_out, lines, changelog_meta)
12 changes: 5 additions & 7 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import sys
from typing import Any, List
from typing import Any

from commitizen import factory, git, out
from commitizen.config import BaseConfig
Expand Down Expand Up @@ -35,7 +35,7 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
# we need to distinguish between None and [], which is a valid value

allowed_prefixes = arguments.get("allowed_prefixes")
self.allowed_prefixes: List[str] = (
self.allowed_prefixes: list[str] = (
allowed_prefixes
if allowed_prefixes is not None
else config.settings["allowed_prefixes"]
Expand All @@ -56,10 +56,8 @@ def _valid_command_argument(self):
self.commit_msg: str | None = sys.stdin.read()
elif num_exclusive_args_provided != 1:
raise InvalidCommandArgumentError(
(
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
"See 'cz check -h' for more information"
)
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
"See 'cz check -h' for more information"
)

def __call__(self):
Expand Down Expand Up @@ -98,7 +96,7 @@ def _get_commits(self):
# Get commit message from file (--commit-msg-file)
if self.commit_msg_file is not None:
# Enter this branch if commit_msg_file is "".
with open(self.commit_msg_file, "r", encoding=self.encoding) as commit_file:
with open(self.commit_msg_file, encoding=self.encoding) as commit_file:
msg = commit_file.read()
# Get commit message from command line (--message)
elif self.commit_msg is not None:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def read_backup_message(self) -> str:
raise NoCommitBackupError()

# Read commit message from backup
with open(self.temp_file, "r", encoding=self.encoding) as f:
with open(self.temp_file, encoding=self.encoding) as f:
return f.read().strip()

def prompt_commit_questions(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/json_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class JsonConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super(JsonConfig, self).__init__()
super().__init__()
self.is_empty_config = False
self.add_path(path)
self._parse_setting(data)
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/toml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class TomlConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super(TomlConfig, self).__init__()
super().__init__()
self.is_empty_config = False
self.add_path(path)
self._parse_setting(data)
Expand Down
2 changes: 1 addition & 1 deletion commitizen/config/yaml_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class YAMLConfig(BaseConfig):
def __init__(self, *, data: bytes | str, path: Path | str):
super(YAMLConfig, self).__init__()
super().__init__()
self.is_empty_config = False
self.add_path(path)
self._parse_setting(data)
Expand Down
2 changes: 1 addition & 1 deletion commitizen/cz/conventional_commits/conventional_commits.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def schema_pattern(self) -> str:
def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "conventional_commits_info.txt")
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
with open(filepath, encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content

Expand Down
4 changes: 2 additions & 2 deletions commitizen/cz/customize/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CustomizeCommitsCz(BaseCommitizen):
change_type_order = defaults.change_type_order

def __init__(self, config: BaseConfig):
super(CustomizeCommitsCz, self).__init__(config)
super().__init__(config)

if "customize" not in self.config.settings:
raise MissingCzCustomizeConfigError()
Expand Down Expand Up @@ -81,7 +81,7 @@ def info(self) -> str | None:
info_path = self.custom_settings.get("info_path")
info = self.custom_settings.get("info")
if info_path:
with open(info_path, "r", encoding=self.config.settings["encoding"]) as f:
with open(info_path, encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content
elif info:
Expand Down
2 changes: 1 addition & 1 deletion commitizen/cz/jira/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ def schema_pattern(self) -> str:
def info(self) -> str:
dir_path = os.path.dirname(os.path.realpath(__file__))
filepath = os.path.join(dir_path, "jira_info.txt")
with open(filepath, "r", encoding=self.config.settings["encoding"]) as f:
with open(filepath, encoding=self.config.settings["encoding"]) as f:
content = f.read()
return content
6 changes: 1 addition & 5 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from __future__ import annotations

import pathlib
import sys
from collections import OrderedDict
from typing import Any, Iterable, MutableMapping

if sys.version_info < (3, 8):
from typing_extensions import TypedDict
else:
from typing import TypedDict
from typing import TypedDict

# Type
Questions = Iterable[MutableMapping[str, Any]]
Expand Down
7 changes: 1 addition & 6 deletions commitizen/version_schemes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import warnings
from itertools import zip_longest
from typing import TYPE_CHECKING, ClassVar, Type, cast
from typing import TYPE_CHECKING, ClassVar, Protocol, Type, cast, runtime_checkable

import importlib_metadata as metadata
from packaging.version import InvalidVersion # noqa: F401: Rexpose the common exception
Expand All @@ -14,11 +14,6 @@
from commitizen.defaults import MAJOR, MINOR, PATCH
from commitizen.exceptions import VersionSchemeUnknown

if sys.version_info >= (3, 8):
from typing import Protocol, runtime_checkable
else:
from typing_extensions import Protocol, runtime_checkable

if TYPE_CHECKING:
# TypeAlias is Python 3.10+ but backported in typing-extensions
if sys.version_info >= (3, 10):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ addopts = "--strict-markers"

[tool.ruff]
line-length = 88
select = ["E", "F", "UP"]
ignore = [
"E501",
"D1",
Expand Down
28 changes: 14 additions & 14 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ def test_bump_files_only(mocker: MockFixture, tmp_commitizen_project):
tag_exists = git.tag_exist("0.3.0")
assert tag_exists is False

with open(tmp_version_file, "r", encoding="utf-8") as f:
with open(tmp_version_file, encoding="utf-8") as f:
assert "0.3.0" in f.read()

with open(tmp_commitizen_cfg_file, "r", encoding="utf-8") as f:
with open(tmp_commitizen_cfg_file, encoding="utf-8") as f:
assert "0.3.0" in f.read()


Expand All @@ -431,7 +431,7 @@ def test_bump_local_version(mocker: MockFixture, tmp_commitizen_project):
tag_exists = git.tag_exist("4.5.1+0.2.0")
assert tag_exists is True

with open(tmp_version_file, "r", encoding="utf-8") as f:
with open(tmp_version_file, encoding="utf-8") as f:
assert "4.5.1+0.2.0" in f.read()


Expand Down Expand Up @@ -511,7 +511,7 @@ def test_bump_with_changelog_arg(mocker: MockFixture, changelog_path):
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True

with open(changelog_path, "r", encoding="utf-8") as f:
with open(changelog_path, encoding="utf-8") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
Expand All @@ -529,7 +529,7 @@ def test_bump_with_changelog_config(mocker: MockFixture, changelog_path, config_
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True

with open(changelog_path, "r", encoding="utf-8") as f:
with open(changelog_path, encoding="utf-8") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
Expand Down Expand Up @@ -572,7 +572,7 @@ def test_bump_with_changelog_to_stdout_arg(mocker: MockFixture, capsys, changelo
tag_exists = git.tag_exist("0.2.0")
assert tag_exists is True

with open(changelog_path, "r", encoding="utf-8") as f:
with open(changelog_path, encoding="utf-8") as f:
out = f.read()
assert out.startswith("#")
assert "0.2.0" in out
Expand Down Expand Up @@ -911,7 +911,7 @@ def test_bump_command_prelease_scheme_via_cli(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0-a0" in f.read()

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
Expand All @@ -923,7 +923,7 @@ def test_bump_command_prelease_scheme_via_cli(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0" in f.read()


Expand All @@ -944,7 +944,7 @@ def test_bump_command_prelease_scheme_via_config(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0-a0" in f.read()

testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
Expand All @@ -955,7 +955,7 @@ def test_bump_command_prelease_scheme_via_config(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0-a1" in f.read()

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
Expand All @@ -967,7 +967,7 @@ def test_bump_command_prelease_scheme_via_config(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0" in f.read()


Expand All @@ -988,7 +988,7 @@ def test_bump_command_prelease_scheme_check_old_tags(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0-a0" in f.read()

testargs = ["cz", "bump", "--prerelease", "alpha"]
Expand All @@ -999,7 +999,7 @@ def test_bump_command_prelease_scheme_check_old_tags(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0-a1" in f.read()

# PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
Expand All @@ -1011,7 +1011,7 @@ def test_bump_command_prelease_scheme_check_old_tags(
assert tag_exists is True

for version_file in [tmp_version_file, tmp_commitizen_cfg_file]:
with open(version_file, "r") as f:
with open(version_file) as f:
assert "0.2.0" in f.read()


Expand Down
Loading

0 comments on commit a48d263

Please sign in to comment.