Skip to content

Commit 9fc7bbc

Browse files
committed
Ruff: lint pytest
1 parent 12aadb1 commit 9fc7bbc

File tree

11 files changed

+51
-46
lines changed

11 files changed

+51
-46
lines changed

pkg_resources/tests/test_resources.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ def test_spaces_between_multiple_versions(self):
700700
(req,) = parse_requirements('foo >= 1.0, < 3')
701701

702702
@pytest.mark.parametrize(
703-
['lower', 'upper'],
703+
'lower, upper',
704704
[
705705
('1.2-rc1', '1.2rc1'),
706706
('0.4', '0.4.0'),
@@ -724,7 +724,7 @@ def testVersionEquality(self, lower, upper):
724724
"""
725725

726726
@pytest.mark.parametrize(
727-
['lower', 'upper'],
727+
'lower, upper',
728728
[
729729
('2.1', '2.1.1'),
730730
('2a1', '2b0'),

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ check = [
114114

115115
# local
116116

117-
# workaround for businho/pytest-ruff#28
118-
"ruff >= 0.5.2; sys_platform != 'cygwin'",
117+
# changed defaults for PT001 and PT023 astral-sh/ruff#13292
118+
"ruff >= 0.7.0; sys_platform != 'cygwin'",
119119
]
120120

121121
cover = [

ruff.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extend-select = [
2020
"I", # isort
2121
"PERF", # Perflint
2222
"PGH", # pygrep-hooks (blanket-* rules)
23+
"PT", # flake8-pytest-style
2324
"PYI", # flake8-pyi
2425
"RUF10", # unused-noqa & redirected-noqa
2526
"TRY", # tryceratops
@@ -28,6 +29,11 @@ extend-select = [
2829
]
2930
ignore = [
3031
"PERF203", # try-except-in-loop, micro-optimisation with many false-positive. Worth checking but don't block CI
32+
"PT004", # deprecated https://github.com/astral-sh/ruff/issues/8796#issuecomment-2057143531
33+
"PT005", # deprecated https://github.com/astral-sh/ruff/issues/8796#issuecomment-2057143531
34+
"PT007", # temporarily disabled, TODO: configure and standardize to preference
35+
"PT011", # temporarily disabled, TODO: tighten expected error
36+
"PT012", # pytest-raises-with-multiple-statements, avoid extra dummy methods for a few lines, sometimes we explicitly assert in case of no error
3137
"TRY003", # raise-vanilla-args, avoid multitude of exception classes
3238
"TRY301", # raise-within-try, it's handy
3339
"UP015", # redundant-open-modes, explicit is preferred
@@ -75,6 +81,9 @@ sections.delayed = ["distutils"]
7581
[lint.flake8-annotations]
7682
ignore-fully-untyped = true
7783

84+
[lint.flake8-pytest-style]
85+
parametrize-names-type = "csv"
86+
7887
[format]
7988
# Enable preview to get hugged parenthesis unwrapping and other nice surprises
8089
# See https://github.com/jaraco/skeleton/pull/133#issuecomment-2239538373

setuptools/command/install_lib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def copy_tree(
101101
preserve_symlinks: bool = False, # type: ignore[override]
102102
level: object = 1,
103103
) -> list[str]:
104-
assert preserve_mode and preserve_times and not preserve_symlinks
104+
assert preserve_mode
105+
assert preserve_times
106+
assert not preserve_symlinks
105107
exclude = self.get_exclusions()
106108

107109
if not exclude:

setuptools/tests/config/test_apply_pyprojecttoml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_no_explicit_content_type_for_missing_extension(tmp_path):
209209

210210

211211
@pytest.mark.parametrize(
212-
('pyproject_text', 'expected_maintainers_meta_value'),
212+
'pyproject_text, expected_maintainers_meta_value',
213213
(
214214
pytest.param(
215215
PEP621_EXAMPLE,

setuptools/tests/integration/test_pip_install_sdist.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,22 @@ def venv_python(tmp_path):
104104

105105

106106
@pytest.fixture(autouse=True)
107-
def _prepare(tmp_path, venv_python, monkeypatch, request):
107+
def _prepare(tmp_path, venv_python, monkeypatch):
108108
download_path = os.getenv("DOWNLOAD_PATH", str(tmp_path))
109109
os.makedirs(download_path, exist_ok=True)
110110

111111
# Environment vars used for building some of the packages
112112
monkeypatch.setenv("USE_MYPYC", "1")
113113

114-
def _debug_info():
115-
# Let's provide the maximum amount of information possible in the case
116-
# it is necessary to debug the tests directly from the CI logs.
117-
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
118-
print("Temporary directory:")
119-
map(print, tmp_path.glob("*"))
120-
print("Virtual environment:")
121-
run([venv_python, "-m", "pip", "freeze"])
114+
yield
122115

123-
request.addfinalizer(_debug_info)
116+
# Let's provide the maximum amount of information possible in the case
117+
# it is necessary to debug the tests directly from the CI logs.
118+
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
119+
print("Temporary directory:")
120+
map(print, tmp_path.glob("*"))
121+
print("Virtual environment:")
122+
run([venv_python, "-m", "pip", "freeze"])
124123

125124

126125
@pytest.mark.parametrize('package, version', EXAMPLES)

setuptools/tests/test_bdist_egg.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818

1919

20-
@pytest.fixture(scope='function')
20+
@pytest.fixture
2121
def setup_context(tmpdir):
2222
with (tmpdir / 'setup.py').open('w') as f:
2323
f.write(SETUP_PY)
@@ -28,7 +28,9 @@ def setup_context(tmpdir):
2828

2929

3030
class Test:
31-
def test_bdist_egg(self, setup_context, user_override):
31+
@pytest.mark.usefixtures("user_override")
32+
@pytest.mark.usefixtures("setup_context")
33+
def test_bdist_egg(self):
3234
dist = Distribution(
3335
dict(
3436
script_name='setup.py',
@@ -50,7 +52,9 @@ def test_bdist_egg(self, setup_context, user_override):
5052
os.environ.get('PYTHONDONTWRITEBYTECODE', False),
5153
reason="Byte code disabled",
5254
)
53-
def test_exclude_source_files(self, setup_context, user_override):
55+
@pytest.mark.usefixtures("user_override")
56+
@pytest.mark.usefixtures("setup_context")
57+
def test_exclude_source_files(self):
5458
dist = Distribution(
5559
dict(
5660
script_name='setup.py',

setuptools/tests/test_easy_install.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,14 @@ def test_multiproc_atexit(self):
407407
logging.basicConfig(level=logging.INFO, stream=sys.stderr)
408408
log.info('this should not break')
409409

410-
@pytest.fixture()
410+
@pytest.fixture
411411
def foo_package(self, tmpdir):
412412
egg_file = tmpdir / 'foo-1.0.egg-info'
413413
with egg_file.open('w') as f:
414414
f.write('Name: foo\n')
415415
return str(tmpdir)
416416

417-
@pytest.fixture()
417+
@pytest.fixture
418418
def install_target(self, tmpdir):
419419
target = str(tmpdir)
420420
with mock.patch('sys.path', sys.path + [target]):
@@ -472,6 +472,12 @@ def distutils_package():
472472
yield
473473

474474

475+
@pytest.mark.usefixtures("distutils_package")
476+
class TestDistutilsPackage:
477+
def test_bdist_egg_available_on_distutils_pkg(self):
478+
run_setup('setup.py', ['bdist_egg'])
479+
480+
475481
@pytest.fixture
476482
def mock_index():
477483
# set up a server which will simulate an alternate package index.
@@ -484,11 +490,6 @@ def mock_index():
484490
return p_index
485491

486492

487-
class TestDistutilsPackage:
488-
def test_bdist_egg_available_on_distutils_pkg(self, distutils_package):
489-
run_setup('setup.py', ['bdist_egg'])
490-
491-
492493
class TestInstallRequires:
493494
def test_setup_install_includes_dependencies(self, tmp_path, mock_index):
494495
"""

setuptools/tests/test_packageindex.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http.client
2+
import re
23
import urllib.error
34
import urllib.request
45
from inspect import cleandoc
@@ -24,11 +25,8 @@ def test_regex(self):
2425
def test_bad_url_bad_port(self):
2526
index = setuptools.package_index.PackageIndex()
2627
url = 'http://127.0.0.1:0/nonesuch/test_package_index'
27-
try:
28+
with pytest.raises(Exception, match=re.escape(url)):
2829
v = index.open_url(url)
29-
except Exception as exc:
30-
assert url in str(exc)
31-
else:
3230
assert isinstance(v, urllib.error.HTTPError)
3331

3432
def test_bad_url_typo(self):
@@ -37,15 +35,10 @@ def test_bad_url_typo(self):
3735
# in its home URL
3836
index = setuptools.package_index.PackageIndex(hosts=('www.example.com',))
3937

40-
url = (
41-
'url:%20https://svn.plone.org/svn'
42-
'/collective/inquant.contentmirror.plone/trunk'
43-
)
44-
try:
38+
url = 'url:%20https://svn.plone.org/svn/collective/inquant.contentmirror.plone/trunk'
39+
40+
with pytest.raises(Exception, match=re.escape(url)):
4541
v = index.open_url(url)
46-
except Exception as exc:
47-
assert url in str(exc)
48-
else:
4942
assert isinstance(v, urllib.error.HTTPError)
5043

5144
def test_bad_url_bad_status_line(self):
@@ -56,12 +49,8 @@ def _urlopen(*args):
5649

5750
index.opener = _urlopen
5851
url = 'http://example.com'
59-
try:
52+
with pytest.raises(Exception, match=r'line'):
6053
index.open_url(url)
61-
except Exception as exc:
62-
assert 'line' in str(exc)
63-
else:
64-
raise AssertionError('Should have raise here!')
6554

6655
def test_bad_url_double_scheme(self):
6756
"""

setuptools/tests/test_setuptools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@pytest.fixture(autouse=True)
2323
def isolated_dir(tmpdir_cwd):
24-
yield
24+
return
2525

2626

2727
def makeSetup(**args):
@@ -257,7 +257,8 @@ def can_symlink(tmpdir):
257257
os.remove(link_fn)
258258

259259

260-
def test_findall_missing_symlink(tmpdir, can_symlink):
260+
@pytest.mark.usefixtures("can_symlink")
261+
def test_findall_missing_symlink(tmpdir):
261262
with tmpdir.as_cwd():
262263
os.symlink('foo', 'bar')
263264
found = list(setuptools.findall())

0 commit comments

Comments
 (0)