Skip to content

Commit 790bb06

Browse files
authored
Merge pull request #2372 from cclauss/ruff-rules-PT
Ruff rules PT for pytest
2 parents 6165d6b + 168d850 commit 790bb06

10 files changed

+43
-35
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ lint.select = [
180180
"PIE",
181181
"PLC",
182182
"PLE",
183+
"PT",
183184
"RUF",
184185
"S",
185186
"UP",

tests/unit/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_check_file_with_changes(capsys, imperfect) -> None:
7474

7575

7676
def test_sorted_imports_multiple_configs() -> None:
77-
with pytest.raises(ValueError):
77+
with pytest.raises(ValueError, match="You can either specify custom configuration options"):
7878
api.sort_code_string("import os", config=Config(line_length=80), line_length=80)
7979

8080

tests/unit/test_format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ def test_colored_printer_diff_output(capsys):
105105
def test_colorama_not_available_handled_gracefully(capsys):
106106
with pytest.raises(SystemExit) as system_exit:
107107
_ = isort.format.create_terminal_printer(color=True)
108-
assert system_exit.value.code and int(system_exit.value.code) > 0
108+
assert system_exit.value.code
109+
assert int(system_exit.value.code) > 0
109110
_, err = capsys.readouterr()
110111
assert "colorama" in err
111112
assert "colors extra" in err

tests/unit/test_isort.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def test_line_length() -> None:
231231
test_input = (
232232
"from .test import a_very_long_function_name_that_exceeds_the_normal_pep8_line_length\n"
233233
)
234-
with pytest.raises(ValueError):
234+
with pytest.raises(ValueError, match="wrap_length must be set lower than or equal to line_le"):
235235
test_output = isort.code(code=REALLY_LONG_IMPORT, line_length=80, wrap_length=99)
236236
assert (
237237
isort.code(REALLY_LONG_IMPORT, line_length=100, wrap_length=99)
@@ -3777,7 +3777,7 @@ def test_argument_parsing() -> None:
37773777
assert args["only_sections"] is True
37783778

37793779

3780-
@pytest.mark.parametrize("multiprocess", (False, True))
3780+
@pytest.mark.parametrize("multiprocess", [False, True])
37813781
def test_command_line(tmpdir, capfd, multiprocess: bool) -> None:
37823782
from isort.main import main
37833783

@@ -3805,7 +3805,7 @@ def test_command_line(tmpdir, capfd, multiprocess: bool) -> None:
38053805
assert str(tmpdir.join("file2.py")) in out
38063806

38073807

3808-
@pytest.mark.parametrize("quiet", (False, True))
3808+
@pytest.mark.parametrize("quiet", [False, True])
38093809
def test_quiet(tmpdir, capfd, quiet: bool) -> None:
38103810
if sys.platform.startswith("win"):
38113811
return
@@ -3822,7 +3822,7 @@ def test_quiet(tmpdir, capfd, quiet: bool) -> None:
38223822
assert bool(out) != quiet
38233823

38243824

3825-
@pytest.mark.parametrize("enabled", (False, True))
3825+
@pytest.mark.parametrize("enabled", [False, True])
38263826
def test_safety_skips(tmpdir, enabled: bool) -> None:
38273827
tmpdir.join("victim.py").write("# ...")
38283828
toxdir = tmpdir.mkdir(".tox")
@@ -3864,11 +3864,11 @@ def test_safety_skips(tmpdir, enabled: bool) -> None:
38643864

38653865
@pytest.mark.parametrize(
38663866
"skip_glob_assert",
3867-
(
3867+
[
38683868
([], 0, {os.sep.join(("code", "file.py"))}),
38693869
(["**/*.py"], 1, set()),
38703870
(["*/code/*.py"], 1, set()),
3871-
),
3871+
],
38723872
)
38733873
def test_skip_glob(tmpdir, skip_glob_assert: Tuple[List[str], int, Set[str]]) -> None:
38743874
skip_glob, skipped_count, file_names_expected = skip_glob_assert

tests/unit/test_literal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_invalid_syntax():
1515

1616

1717
def test_invalid_sort_type():
18-
with pytest.raises(ValueError):
18+
with pytest.raises(ValueError, match="Trying to sort using an undefined sort_type. Defined"):
1919
isort.literal.assignment("x = [1, 2, 3", "tuple-list-not-exist", "py")
2020

2121

tests/unit/test_main.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1102,12 +1102,14 @@ def main_check(args):
11021102
(tmp_path / "no_imports.py").write_text("...")
11031103

11041104
out, error = main_check([str(python_file), "--skip-gitignore", "--filter-files", "--check"])
1105-
assert "has_imports.py" in error and "no_imports.py" not in error
1105+
assert "has_imports.py" in error
1106+
assert "no_imports.py" not in error
11061107

11071108
(tmp_path / ".gitignore").write_text("has_imports.py")
11081109

11091110
out, error = main_check([str(python_file), "--check"])
1110-
assert "has_imports.py" in error and "no_imports.py" not in error
1111+
assert "has_imports.py" in error
1112+
assert "no_imports.py" not in error
11111113

11121114
out, error = main_check([str(python_file), "--skip-gitignore", "--filter-files", "--check"])
11131115
assert "Skipped" in out
@@ -1119,14 +1121,16 @@ def main_check(args):
11191121
subfolder_file.write_text(import_content)
11201122

11211123
out, error = main_check([str(tmp_path), "--skip-gitignore", "--filter-files", "--check"])
1122-
assert "has_imports.py" in error and "nested_dir/has_imports.py" not in error
1124+
assert "has_imports.py" in error
1125+
assert "nested_dir/has_imports.py" not in error
11231126

11241127
# Should work with relative path
11251128
currentdir = os.getcwd()
11261129
os.chdir(tmp_path)
11271130

11281131
out, error = main_check([".", "--skip-gitignore", "--filter-files", "--check"])
1129-
assert "has_imports.py" in error and "nested_dir/has_imports.py" not in error
1132+
assert "has_imports.py" in error
1133+
assert "nested_dir/has_imports.py" not in error
11301134

11311135
(tmp_path / ".gitignore").write_text(
11321136
"""

tests/unit/test_parse.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def test_fuzz_skip_line(line, in_quote, index, section_comments, needs_import):
8585

8686

8787
@pytest.mark.parametrize(
88-
"raw_line, expected",
89-
(
88+
("raw_line", "expected"),
89+
[
9090
("from . cimport a", "from . cimport a"),
9191
("from.cimport a", "from . cimport a"),
9292
("from..cimport a", "from .. cimport a"),
@@ -95,14 +95,14 @@ def test_fuzz_skip_line(line, in_quote, index, section_comments, needs_import):
9595
("from..import a", "from .. import a"),
9696
("import *", "import *"),
9797
("import*", "import *"),
98-
("from . import a", "from . import a"),
98+
("from . import a", "from . import a"), # noqa: PT014
9999
("from .import a", "from . import a"),
100100
("from ..import a", "from .. import a"),
101-
("from . cimport a", "from . cimport a"),
101+
("from . cimport a", "from . cimport a"), # noqa: PT014
102102
("from .cimport a", "from . cimport a"),
103103
("from ..cimport a", "from .. cimport a"),
104104
("from\t.\timport a", "from . import a"),
105-
),
105+
],
106106
)
107107
def test_normalize_line(raw_line, expected):
108108
line, returned_raw_line = parse.normalize_line(raw_line)

tests/unit/test_settings.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ def test_init(self):
1818
def test_init_unsupported_settings_fails_gracefully(self):
1919
with pytest.raises(exceptions.UnsupportedSettings):
2020
Config(apply=True)
21-
try:
21+
with pytest.raises(exceptions.UnsupportedSettings) as error:
2222
Config(apply=True)
23-
except exceptions.UnsupportedSettings as error:
24-
assert error.unsupported_settings == {"apply": {"value": True, "source": "runtime"}}
23+
assert error.value.unsupported_settings == {"apply": {"value": True, "source": "runtime"}}
2524

2625
def test_known_settings(self):
2726
assert Config(known_third_party=["one"]).known_third_party == frozenset({"one"})
@@ -35,7 +34,7 @@ def test_invalid_settings_path(self):
3534
Config(settings_path="this_couldnt_possibly_actually_exists/could_it")
3635

3736
def test_invalid_pyversion(self):
38-
with pytest.raises(ValueError):
37+
with pytest.raises(ValueError, match="The python version 10 is not supported."):
3938
Config(py_version=10)
4039

4140
def test_invalid_profile(self):
@@ -222,11 +221,11 @@ def test_as_bool():
222221
assert settings._as_bool("FALSE") is False
223222
assert settings._as_bool("faLSE") is False
224223
assert settings._as_bool("f") is False
225-
with pytest.raises(ValueError):
224+
with pytest.raises(ValueError, match="invalid truth value"):
226225
settings._as_bool("")
227-
with pytest.raises(ValueError):
226+
with pytest.raises(ValueError, match="invalid truth value falsey"):
228227
settings._as_bool("falsey")
229-
with pytest.raises(ValueError):
228+
with pytest.raises(ValueError, match="invalid truth value truthy"):
230229
settings._as_bool("truthy")
231230

232231

@@ -277,15 +276,18 @@ def test_find_all_configs(tmpdir):
277276

278277
config_info_1 = config_trie.search(str(dir1 / "test1.py"))
279278
assert config_info_1[0] == str(setup_cfg_file)
280-
assert config_info_1[0] == str(setup_cfg_file) and config_info_1[1]["profile"] == "django"
279+
assert config_info_1[0] == str(setup_cfg_file)
280+
assert config_info_1[1]["profile"] == "django"
281281

282282
config_info_2 = config_trie.search(str(dir2 / "test2.py"))
283283
assert config_info_2[0] == str(pyproject_toml_file)
284-
assert config_info_2[0] == str(pyproject_toml_file) and config_info_2[1]["profile"] == "hug"
284+
assert config_info_2[0] == str(pyproject_toml_file)
285+
assert config_info_2[1]["profile"] == "hug"
285286

286287
config_info_3 = config_trie.search(str(dir3 / "test3.py"))
287288
assert config_info_3[0] == str(isort_cfg_file)
288-
assert config_info_3[0] == str(isort_cfg_file) and config_info_3[1]["profile"] == "black"
289+
assert config_info_3[0] == str(isort_cfg_file)
290+
assert config_info_3[1]["profile"] == "black"
289291

290292
config_info_4 = config_trie.search(str(tmpdir / "file4.py"))
291293
assert config_info_4[0] == "default"

tests/unit/test_wrap.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def test_import_statement():
2222

2323

2424
@pytest.mark.parametrize(
25-
"multi_line_output, expected",
26-
(
25+
("multi_line_output", "expected"),
26+
[
2727
(
2828
WrapModes.VERTICAL_HANGING_INDENT, # type: ignore
2929
"""from a import (
@@ -35,7 +35,7 @@ def test_import_statement():
3535
"""from a import (
3636
b as c) # comment that is long enough that this import doesn't fit in one line (parens)""",
3737
),
38-
),
38+
],
3939
)
4040
def test_line__comment_with_brackets__expects_unchanged_comment(multi_line_output, expected):
4141
content = (

tests/unit/test_wrap_modes.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def test_backslash_grid():
9494
)
9595

9696

97-
@pytest.mark.parametrize("include_trailing_comma", (False, True))
98-
@pytest.mark.parametrize("line_length", (18, 19))
99-
@pytest.mark.parametrize("multi_line_output", (4, 5))
97+
@pytest.mark.parametrize("include_trailing_comma", [False, True])
98+
@pytest.mark.parametrize("line_length", [18, 19])
99+
@pytest.mark.parametrize("multi_line_output", [4, 5])
100100
def test_vertical_grid_size_near_line_length(
101101
multi_line_output: int,
102102
line_length: int,
@@ -259,7 +259,7 @@ def test_fuzz_hanging_indent(
259259
reject()
260260

261261

262-
@pytest.mark.parametrize("include_trailing_comma", (True, False))
262+
@pytest.mark.parametrize("include_trailing_comma", [True, False])
263263
def test_hanging_indent__with_include_trailing_comma__expect_same_result(include_trailing_comma):
264264
result = isort.wrap_modes.hanging_indent(
265265
statement="from datetime import ",

0 commit comments

Comments
 (0)