Skip to content

Commit 2cd8418

Browse files
committed
fix: add pytest ruff rule PT and fix missing deprecation warning (#1826)
1 parent 215fe36 commit 2cd8418

12 files changed

+54
-126
lines changed

commitizen/changelog_formats/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import warnings
34
from importlib import metadata
45
from typing import TYPE_CHECKING, ClassVar, Protocol
56

@@ -99,5 +100,11 @@ def _guess_changelog_format(filename: str | None) -> type[ChangelogFormat] | Non
99100

100101
def __getattr__(name: str) -> Callable[[str], type[ChangelogFormat] | None]:
101102
if name == "guess_changelog_format":
103+
warnings.warn(
104+
"guess_changelog_format is deprecated and will be removed in v5. "
105+
"Use _guess_changelog_format instead.",
106+
DeprecationWarning,
107+
stacklevel=2,
108+
)
102109
return _guess_changelog_format
103110
raise AttributeError(f"module {__name__} has no attribute {name}")

pyproject.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ select = [
223223
"RUF022",
224224
# unused-noqa
225225
"RUF100",
226+
# flake8-pytest-style
227+
"PT",
226228
# Checks for uses of the assert keyword.
227229
"S101",
228230
# flake8-type-checking (TC)
@@ -233,7 +235,16 @@ select = [
233235
"TC005",
234236
"TC006",
235237
]
236-
ignore = ["E501", "D1", "D415"]
238+
ignore = [
239+
"E501",
240+
"D1",
241+
"D415",
242+
"PT006", # TODO(bearomorphism): enable this rule
243+
"PT007", # TODO(bearomorphism): enable this rule
244+
"PT011", # TODO(bearomorphism): enable this rule
245+
"PT022", # TODO(bearomorphism): enable this rule
246+
"PT030", # TODO(bearomorphism): enable this rule
247+
]
237248
extend-safe-fixes = [
238249
"TC", # Move imports inside/outside TYPE_CHECKING blocks
239250
"UP", # Update syntaxes for current Python version recommendations

tests/commands/test_init_command.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def pre_commit_installed(mocker: MockFixture):
133133
)
134134

135135

136-
@pytest.fixture(scope="function", params=["pyproject.toml", ".cz.json", ".cz.yaml"])
136+
@pytest.fixture(params=["pyproject.toml", ".cz.json", ".cz.yaml"])
137137
def default_choice(request, mocker: MockFixture):
138138
mocker.patch(
139139
"questionary.select",
@@ -150,7 +150,7 @@ def default_choice(request, mocker: MockFixture):
150150
"questionary.checkbox",
151151
return_value=FakeQuestion(["commit-msg", "pre-push"]),
152152
)
153-
yield request.param
153+
return request.param
154154

155155

156156
def check_cz_config(config_filepath: str):
@@ -179,15 +179,15 @@ def check_pre_commit_config(expected: list[dict[str, Any]]):
179179
@pytest.mark.usefixtures("pre_commit_installed")
180180
class TestPreCommitCases:
181181
def test_no_existing_pre_commit_config(
182-
_, default_choice: str, tmpdir, config: BaseConfig
182+
self, default_choice: str, tmpdir, config: BaseConfig
183183
):
184184
with tmpdir.as_cwd():
185185
commands.Init(config)()
186186
check_cz_config(default_choice)
187187
check_pre_commit_config([cz_hook_config])
188188

189189
def test_empty_pre_commit_config(
190-
_, default_choice: str, tmpdir, config: BaseConfig
190+
self, default_choice: str, tmpdir, config: BaseConfig
191191
):
192192
with tmpdir.as_cwd():
193193
p = tmpdir.join(pre_commit_config_filename)
@@ -198,7 +198,7 @@ def test_empty_pre_commit_config(
198198
check_pre_commit_config([cz_hook_config])
199199

200200
def test_pre_commit_config_without_cz_hook(
201-
_, default_choice: str, tmpdir, config: BaseConfig
201+
self, default_choice: str, tmpdir, config: BaseConfig
202202
):
203203
existing_hook_config = {
204204
"repo": "https://github.com/pre-commit/pre-commit-hooks",
@@ -215,7 +215,7 @@ def test_pre_commit_config_without_cz_hook(
215215
check_pre_commit_config([existing_hook_config, cz_hook_config])
216216

217217
def test_cz_hook_exists_in_pre_commit_config(
218-
_, default_choice: str, tmpdir, config: BaseConfig
218+
self, default_choice: str, tmpdir, config: BaseConfig
219219
):
220220
with tmpdir.as_cwd():
221221
p = tmpdir.join(pre_commit_config_filename)
@@ -228,8 +228,9 @@ def test_cz_hook_exists_in_pre_commit_config(
228228

229229

230230
class TestNoPreCommitInstalled:
231+
@pytest.mark.usefixtures("default_choice")
231232
def test_pre_commit_not_installed(
232-
_, mocker: MockFixture, config: BaseConfig, default_choice: str, tmpdir
233+
self, mocker: MockFixture, config: BaseConfig, tmpdir
233234
):
234235
# Assume `pre-commit` is not installed
235236
mocker.patch(

tests/test_bump_hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ def test_run_error(mocker: MockFixture):
3838

3939
def test_format_env():
4040
result = hooks._format_env("TEST_", {"foo": "bar", "bar": "baz"})
41-
assert "TEST_FOO" in result and result["TEST_FOO"] == "bar"
42-
assert "TEST_BAR" in result and result["TEST_BAR"] == "baz"
41+
assert result["TEST_FOO"] == "bar"
42+
assert result["TEST_BAR"] == "baz"

tests/test_bump_update_version_in_files.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def docker_compose_file(sample_file: SampleFileFixture) -> Path:
5757

5858

5959
@pytest.fixture(
60-
scope="function",
6160
params=(
6261
"multiple_versions_to_update_pyproject.toml",
6362
"multiple_versions_to_update_pyproject_wo_eol.toml",

tests/test_conf.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,16 @@ class TestReadCfg:
184184
@pytest.mark.parametrize(
185185
"config_files_manager", defaults.CONFIG_FILES, indirect=True
186186
)
187-
def test_load_conf(_, config_files_manager):
187+
def test_load_conf(self, config_files_manager):
188188
cfg = config.read_cfg()
189189
assert cfg.settings == _settings
190190

191-
def test_conf_returns_default_when_no_files(_, tmpdir):
191+
def test_conf_returns_default_when_no_files(self, tmpdir):
192192
with tmpdir.as_cwd():
193193
cfg = config.read_cfg()
194194
assert cfg.settings == defaults.DEFAULT_SETTINGS
195195

196-
def test_load_empty_pyproject_toml_and_cz_toml_with_config(_, tmpdir):
196+
def test_load_empty_pyproject_toml_and_cz_toml_with_config(self, tmpdir):
197197
with tmpdir.as_cwd():
198198
p = tmpdir.join("pyproject.toml")
199199
p.write("")
@@ -203,15 +203,15 @@ def test_load_empty_pyproject_toml_and_cz_toml_with_config(_, tmpdir):
203203
cfg = config.read_cfg()
204204
assert cfg.settings == _settings
205205

206-
def test_load_pyproject_toml_from_config_argument(_, tmpdir):
206+
def test_load_pyproject_toml_from_config_argument(self, tmpdir):
207207
with tmpdir.as_cwd():
208208
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
209209
_not_root_path.write(PYPROJECT)
210210

211211
cfg = config.read_cfg(filepath="./not_in_root/pyproject.toml")
212212
assert cfg.settings == _settings
213213

214-
def test_load_cz_json_not_from_config_argument(_, tmpdir):
214+
def test_load_cz_json_not_from_config_argument(self, tmpdir):
215215
with tmpdir.as_cwd():
216216
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.json")
217217
_not_root_path.write(JSON_STR)
@@ -220,7 +220,7 @@ def test_load_cz_json_not_from_config_argument(_, tmpdir):
220220
json_cfg_by_class = JsonConfig(data=JSON_STR, path=_not_root_path)
221221
assert cfg.settings == json_cfg_by_class.settings
222222

223-
def test_load_cz_yaml_not_from_config_argument(_, tmpdir):
223+
def test_load_cz_yaml_not_from_config_argument(self, tmpdir):
224224
with tmpdir.as_cwd():
225225
_not_root_path = tmpdir.mkdir("not_in_root").join(".cz.yaml")
226226
_not_root_path.write(YAML_STR)
@@ -229,7 +229,7 @@ def test_load_cz_yaml_not_from_config_argument(_, tmpdir):
229229
yaml_cfg_by_class = YAMLConfig(data=YAML_STR, path=_not_root_path)
230230
assert cfg.settings == yaml_cfg_by_class._settings
231231

232-
def test_load_empty_pyproject_toml_from_config_argument(_, tmpdir):
232+
def test_load_empty_pyproject_toml_from_config_argument(self, tmpdir):
233233
with tmpdir.as_cwd():
234234
_not_root_path = tmpdir.mkdir("not_in_root").join("pyproject.toml")
235235
_not_root_path.write("")
@@ -260,7 +260,7 @@ class TestWarnMultipleConfigFiles:
260260
],
261261
)
262262
def test_warn_multiple_config_files_same_dir(
263-
_, tmpdir, capsys, files, expected_path, should_warn
263+
self, tmpdir, capsys, files, expected_path, should_warn
264264
):
265265
"""Test warning when multiple config files exist in same directory."""
266266
with tmpdir.as_cwd():
@@ -296,7 +296,7 @@ def test_warn_multiple_config_files_same_dir(
296296
],
297297
)
298298
def test_warn_same_filename_different_directories_with_git(
299-
_, tmpdir, capsys, config_file, content
299+
self, tmpdir, capsys, config_file, content
300300
):
301301
"""Test warning when same config filename exists in the current directory and in the git root."""
302302
with tmpdir.as_cwd():
@@ -317,7 +317,7 @@ def test_warn_same_filename_different_directories_with_git(
317317
assert f"Using config file: '{config_file}'" in captured.err
318318
assert cfg.path == Path(config_file)
319319

320-
def test_no_warn_with_explicit_config_path(_, tmpdir, capsys):
320+
def test_no_warn_with_explicit_config_path(self, tmpdir, capsys):
321321
"""Test that no warning is issued when user explicitly specifies config."""
322322
with tmpdir.as_cwd():
323323
# Create multiple config files
@@ -352,7 +352,7 @@ def test_no_warn_with_explicit_config_path(_, tmpdir, capsys):
352352
],
353353
)
354354
def test_no_warn_with_single_config_file(
355-
_, tmpdir, capsys, config_file, content, with_git
355+
self, tmpdir, capsys, config_file, content, with_git
356356
):
357357
"""Test that no warning is issued when user explicitly specifies config."""
358358
with tmpdir.as_cwd():

tests/test_deprecated.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@
55

66
def test_getattr_deprecated_vars():
77
# Test each deprecated variable
8-
with pytest.warns(DeprecationWarning) as record:
8+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
99
assert defaults.bump_pattern == defaults.BUMP_PATTERN
10+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1011
assert defaults.bump_map == defaults.BUMP_MAP
12+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1113
assert (
1214
defaults.bump_map_major_version_zero == defaults.BUMP_MAP_MAJOR_VERSION_ZERO
1315
)
16+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1417
assert defaults.bump_message == defaults.BUMP_MESSAGE
18+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1519
assert defaults.change_type_order == defaults.CHANGE_TYPE_ORDER
20+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1621
assert defaults.encoding == defaults.ENCODING
22+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1723
assert defaults.name == defaults.DEFAULT_SETTINGS["name"]
24+
with pytest.warns(DeprecationWarning, match="is deprecated and will be removed"):
1825
assert (
1926
changelog_formats._guess_changelog_format
2027
== changelog_formats.guess_changelog_format
2128
)
2229

23-
# Verify warning messages
24-
assert len(record) == 7
25-
for warning in record:
26-
assert "is deprecated and will be removed" in str(warning.message)
27-
2830

2931
def test_getattr_non_existent():
3032
# Test non-existent attribute

tests/test_factory.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ class Plugin: pass
5252
)
5353

5454
sys.path.append(tmp_path.as_posix())
55-
with pytest.warns(UserWarning) as record:
55+
with pytest.warns(
56+
UserWarning,
57+
match="Legacy plugin 'cz_legacy' has been ignored: please expose it the 'commitizen.plugin' entrypoint",
58+
):
5659
discovered_plugins = discover_plugins([tmp_path.as_posix()])
5760
sys.path.pop()
5861

59-
assert (
60-
record[0].message.args[0]
61-
== "Legacy plugin 'cz_legacy' has been ignored: please expose it the 'commitizen.plugin' entrypoint"
62-
)
6362
assert "cz_legacy" not in discovered_plugins
6463

6564

tests/test_version_scheme_pep440.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,6 @@
249249
),
250250
"0.1.1.dev1",
251251
),
252-
(
253-
VersionSchemeTestArgs(
254-
current_version="0.1.1",
255-
increment="MINOR",
256-
prerelease=None,
257-
prerelease_offset=0,
258-
devrelease=None,
259-
),
260-
"0.2.0",
261-
),
262252
(
263253
VersionSchemeTestArgs(
264254
current_version="0.2.0",
@@ -733,26 +723,6 @@
733723
),
734724
"1.1.0a0",
735725
),
736-
(
737-
VersionSchemeTestArgs(
738-
current_version="1.1.0a0",
739-
increment="PATCH",
740-
prerelease="alpha",
741-
prerelease_offset=0,
742-
devrelease=None,
743-
),
744-
"1.1.0a1",
745-
),
746-
(
747-
VersionSchemeTestArgs(
748-
current_version="1.1.0a1",
749-
increment="MINOR",
750-
prerelease="alpha",
751-
prerelease_offset=0,
752-
devrelease=None,
753-
),
754-
"1.1.0a2",
755-
),
756726
(
757727
VersionSchemeTestArgs(
758728
current_version="1.1.0a2",
@@ -1020,17 +990,6 @@
1020990
),
1021991
"3.1.4rc0",
1022992
),
1023-
#
1024-
(
1025-
VersionSchemeTestArgs(
1026-
current_version="3.1.4",
1027-
increment=None,
1028-
prerelease="alpha",
1029-
prerelease_offset=0,
1030-
devrelease=None,
1031-
),
1032-
"3.1.4a0",
1033-
),
1034993
(
1035994
VersionSchemeTestArgs(
1036995
current_version="3.1.4a0",

tests/test_version_scheme_semver.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,6 @@
271271
),
272272
"0.1.1-dev1",
273273
),
274-
(
275-
VersionSchemeTestArgs(
276-
current_version="0.1.1",
277-
increment="MINOR",
278-
prerelease=None,
279-
prerelease_offset=0,
280-
devrelease=None,
281-
),
282-
"0.2.0",
283-
),
284274
(
285275
VersionSchemeTestArgs(
286276
current_version="0.2.0",

0 commit comments

Comments
 (0)