Skip to content

Commit

Permalink
Merge pull request #42 from bluetech/pytest7
Browse files Browse the repository at this point in the history
Prepare for upcoming pytest 7.0 deprecations
  • Loading branch information
asmeurer committed Oct 25, 2021
2 parents 4d8eb07 + 68e5368 commit 738430c
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 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(str(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 All @@ -73,21 +86,25 @@ class FlakesItem(pytest.Item):

def __init__(self, *k, **kw):
super().__init__(*k, **kw)
if hasattr(self, 'add_marker'):
self.add_marker("flakes")
else:
self.keywords["flakes"] = True
self.add_marker("flakes")
self.flakesignore = self.parent.flakesignore

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 @@ -104,13 +121,10 @@ def reportinfo(self):
ignores = "(ignoring %s)" % " ".join(self.flakesignore)
else:
ignores = ""
return (self.fspath, -1, "pyflakes-check%s" % ignores)

def collect(self):
""" returns a list of children (items and collectors)
for this collection node.
"""
return (self,)
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 @@ -134,20 +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):
if not hasattr(tokenize, 'open'):
codeString = path.read()
else:
with tokenize.open(path.strpath) as f:
codeString = f.read()
filename = py.builtin._totext(path)
with tokenize.open(str(path)) as f:
codeString = f.read()
filename = str(path)
errors = []
try:
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
Expand Down

0 comments on commit 738430c

Please sign in to comment.