Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit cd7c0fd

Browse files
committed
Adopt to pytest 7.x
pytest 7.x deprecated few things and also dropped support for python 2.7 This change also add testing on python 3.9 and 3.10, and run flake8 on latest python in CI
1 parent 1d0d34a commit cd7c0fd

File tree

4 files changed

+28
-29
lines changed

4 files changed

+28
-29
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
language: python
22
matrix:
33
include:
4+
- python: 3.10
5+
env: TOX_ENV=py310,flake8
6+
- python: 3.9
7+
env: TOX_ENV=py39
48
- python: 3.8
59
env: TOX_ENV=py38
610
- python: 3.7
@@ -9,8 +13,6 @@ matrix:
913
env: TOX_ENV=py36
1014
- python: 3.5
1115
env: TOX_ENV=py35
12-
- python: 2.7
13-
env: TOX_ENV=py27
1416
install:
1517
- pip install tox
1618
script:

pytest_black.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ def pytest_addoption(parser):
2020
)
2121

2222

23-
def pytest_collect_file(path, parent):
23+
def pytest_collect_file(file_path, path, parent):
2424
config = parent.config
2525
if config.option.black and path.ext == ".py":
26-
if hasattr(BlackItem, "from_parent"):
27-
return BlackItem.from_parent(parent, fspath=path)
28-
else:
29-
return BlackItem(path, parent)
26+
return BlackFile.from_parent(parent, path=file_path)
3027

3128

3229
def pytest_configure(config):
@@ -42,10 +39,17 @@ def pytest_unconfigure(config):
4239
config.cache.set(HISTKEY, config._blackmtimes)
4340

4441

45-
class BlackItem(pytest.Item, pytest.File):
46-
def __init__(self, fspath, parent):
47-
super(BlackItem, self).__init__(fspath, parent)
48-
self._nodeid += "::BLACK"
42+
class BlackFile(pytest.File):
43+
def collect(self):
44+
""" returns a list of children (items and collectors)
45+
for this collection node.
46+
"""
47+
yield BlackItem.from_parent(self, name="black")
48+
49+
50+
class BlackItem(pytest.Item):
51+
def __init__(self, **kwargs):
52+
super(BlackItem, self).__init__(**kwargs)
4953
self.add_marker("black")
5054
try:
5155
with open("pyproject.toml") as toml_file:
@@ -61,16 +65,16 @@ def __init__(self, fspath, parent):
6165
def setup(self):
6266
pytest.importorskip("black")
6367
mtimes = getattr(self.config, "_blackmtimes", {})
64-
self._blackmtime = self.fspath.mtime()
65-
old = mtimes.get(str(self.fspath), 0)
68+
self._blackmtime = self.path.stat().st_mtime
69+
old = mtimes.get(str(self.path), 0)
6670
if self._blackmtime == old:
6771
pytest.skip("file(s) previously passed black format checks")
6872

6973
if self._skip_test():
7074
pytest.skip("file(s) excluded by pyproject.toml")
7175

7276
def runtest(self):
73-
cmd = [sys.executable, "-m", "black", "--check", "--diff", "--quiet", str(self.fspath)]
77+
cmd = [sys.executable, "-m", "black", "--check", "--diff", "--quiet", str(self.path)]
7478
try:
7579
subprocess.run(
7680
cmd, check=True, stdout=subprocess.PIPE, universal_newlines=True
@@ -79,40 +83,34 @@ def runtest(self):
7983
raise BlackError(e)
8084

8185
mtimes = getattr(self.config, "_blackmtimes", {})
82-
mtimes[str(self.fspath)] = self._blackmtime
86+
mtimes[str(self.path)] = self._blackmtime
8387

8488
def repr_failure(self, excinfo):
8589
if excinfo.errisinstance(BlackError):
8690
return excinfo.value.args[0].stdout
8791
return super(BlackItem, self).repr_failure(excinfo)
8892

8993
def reportinfo(self):
90-
return (self.fspath, -1, "Black format check")
94+
return (self.path, -1, "Black format check")
9195

9296
def _skip_test(self):
9397
return self._excluded() or (not self._included())
9498

9599
def _included(self):
96100
if "include" not in self.pyproject:
97101
return True
98-
return re.search(self.pyproject["include"], str(self.fspath))
102+
return re.search(self.pyproject["include"], str(self.path))
99103

100104
def _excluded(self):
101105
if "exclude" not in self.pyproject:
102106
return False
103-
return re.search(self.pyproject["exclude"], str(self.fspath))
107+
return re.search(self.pyproject["exclude"], str(self.path))
104108

105109
def _re_fix_verbose(self, regex):
106110
if "\n" in regex:
107111
regex = "(?x)" + regex
108112
return re.compile(regex)
109113

110-
def collect(self):
111-
""" returns a list of children (items and collectors)
112-
for this collection node.
113-
"""
114-
return (self,)
115-
116114

117115
class BlackError(Exception):
118116
pass

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def read(fname):
2323
long_description=read("README.md"),
2424
long_description_content_type="text/markdown",
2525
py_modules=["pytest_black"],
26-
python_requires=">=2.7",
26+
python_requires=">=3.5",
2727
install_requires=[
28-
"pytest>=3.5.0",
28+
"pytest>=7.0.0",
2929
# Minimum requirement on black 19.3b0 or later is not declared here as
3030
# workaround for https://github.com/pypa/pipenv/issues/3928
3131
'black; python_version >= "3.6"',
@@ -38,7 +38,6 @@ def read(fname):
3838
"Framework :: Pytest",
3939
"Intended Audience :: Developers",
4040
"Topic :: Software Development :: Testing",
41-
"Programming Language :: Python :: 2.7",
4241
"Programming Language :: Python :: 3",
4342
"Operating System :: OS Independent",
4443
"License :: OSI Approved :: MIT License",

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
ignore = E501
44

55
[tox]
6-
envlist = py27,py35,py36,py37,py38,flake8
6+
envlist = py35,py36,py37,py38,py39,py310,flake8
77

88
[testenv]
9-
deps = pytest>=3.0
9+
deps = pytest>=7.0
1010
commands = pytest {posargs:tests}
1111

1212
[testenv:flake8]

0 commit comments

Comments
 (0)