Skip to content

Commit

Permalink
Prepare for upcoming pytest 7.0 deprecations
Browse files Browse the repository at this point in the history
pytest-flakes is checked as part of pytest's CI. In pytest 7.0
(previously 6.3), `py.path.local`-based arguments to hooks & node ctors
are deprecated, so switch to the `pathlib` based replacements on pytest
7.
  • Loading branch information
bluetech committed Oct 25, 2021
1 parent 5e61d72 commit d8ee993
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions pytest_flakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from pyflakes.api import isPythonFile
import _ast
import re
import py
import pathlib
import pytest
import sys
import tokenize


PYTEST_GTE_7 = hasattr(pytest, 'version_tuple') and pytest.version_tuple >= (7, 0)


def assignment_monkeypatched_init(self, name, source):
Binding.__init__(self, name, source)
if name == '__tracebackhide__':
Expand Down Expand Up @@ -43,14 +46,24 @@ def __init__(self, config):
self.ignore = Ignorer(config.getini("flakes-ignore"))
self.mtimes = config.cache.get(HISTKEY, {})

def pytest_collect_file(self, path, parent):
config = parent.config
if config.option.flakes and isPythonFile(path.strpath):
flakesignore = self.ignore(path)
if flakesignore is not None:
return FlakesFile.from_parent(parent,
fspath=path,
flakesignore=flakesignore)
if PYTEST_GTE_7:
def pytest_collect_file(self, fspath, parent):
config = parent.config
if config.option.flakes and isPythonFile(str(fspath)):
flakesignore = self.ignore(fspath)
if flakesignore is not None:
return FlakesFile.from_parent(parent,
path=fspath,
flakesignore=flakesignore)
else:
def pytest_collect_file(self, path, parent):
config = parent.config
if config.option.flakes and isPythonFile(path.strpath):
flakesignore = self.ignore(pathlib.Path(path))
if flakesignore is not None:
return FlakesFile.from_parent(parent,
fspath=path,
flakesignore=flakesignore)

def pytest_sessionfinish(self, session):
session.config.cache.set(HISTKEY, self.mtimes)
Expand Down Expand Up @@ -78,13 +91,20 @@ def __init__(self, *k, **kw):

def setup(self):
flakesmtimes = self.config._flakes.mtimes
self._flakesmtime = self.fspath.mtime()
if PYTEST_GTE_7:
self._flakesmtime = self.path.stat().st_mtime
else:
self._flakesmtime = self.fspath.mtime()
old = flakesmtimes.get(self.nodeid, 0)
if old == [self._flakesmtime, self.flakesignore]:
pytest.skip("file(s) previously passed pyflakes checks")

def runtest(self):
found_errors, out = check_file(self.fspath, self.flakesignore)
if PYTEST_GTE_7:
found_errors, out = check_file(self.path, self.flakesignore)
else:
path = pathlib.Path(str(self.fspath))
found_errors, out = check_file(path, self.flakesignore)
if found_errors:
raise FlakesError("\n".join(out))
# update mtime only if test passed
Expand All @@ -101,7 +121,10 @@ def reportinfo(self):
ignores = "(ignoring %s)" % " ".join(self.flakesignore)
else:
ignores = ""
return (self.fspath, -1, "pyflakes-check%s" % ignores)
if PYTEST_GTE_7:
return (self.path, -1, "pyflakes-check%s" % ignores)
else:
return (self.fspath, -1, "pyflakes-check%s" % ignores)


class Ignorer:
Expand All @@ -125,17 +148,17 @@ def __init__(self, ignorelines, coderex=re.compile(r"[EW]\d\d\d")):
def __call__(self, path):
l = set()
for (glob, ignlist) in self.ignores:
if not glob or path.fnmatch(glob):
if not glob or path.glob(glob):
if ignlist is None:
return None
l.update(set(ignlist))
return sorted(l)


def check_file(path, flakesignore):
with tokenize.open(path.strpath) as f:
with tokenize.open(str(path)) as f:
codeString = f.read()
filename = py.builtin._totext(path)
filename = str(path)
errors = []
try:
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
Expand Down

0 comments on commit d8ee993

Please sign in to comment.