Skip to content

Commit

Permalink
Remove some deprecated constructs
Browse files Browse the repository at this point in the history
Clean up some now unused code and clean up deprecated constructs,
including splitting of pytest.Item and pytest.Collector (pytest.File)
and using pathlib.Path a bit more.
  • Loading branch information
tholo committed Mar 18, 2022
1 parent 0348600 commit d730278
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 62 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.1.1
-----

- Update classifiers to indicate older versions are no longer supported
- No longer use deprecated pytest constructs
- Bump requirements to more accurately indicate what is currently needed

1.1.0
-----

Expand Down
107 changes: 50 additions & 57 deletions pytest_flake8.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

__version__ = '0.6'
__version__ = '1.1.1'

HISTKEY = "flake8/mtimes"

Expand Down Expand Up @@ -53,39 +53,29 @@ def pytest_configure(config):
config._flake8maxlen = config.getini("flake8-max-line-length")
config._flake8maxdoclen = config.getini("flake8-max-doc-length")
config._flake8maxcomplexity = config.getini("flake8-max-complexity")
config._flake8showshource = config.getini("flake8-show-source")
config._flake8showsource = config.getini("flake8-show-source")
config._flake8statistics = config.getini("flake8-statistics")
config._flake8exts = config.getini("flake8-extensions")
config.addinivalue_line('markers', "flake8: Tests which run flake8.")
if hasattr(config, 'cache'):
config._flake8mtimes = config.cache.get(HISTKEY, {})


def pytest_collect_file(path, parent):
def pytest_collect_file(file_path, path, parent):
"""Filter files down to which ones should be checked."""
config = parent.config
if config.option.flake8 and path.ext in config._flake8exts:
if config.option.flake8 and file_path.suffix in config._flake8exts:
flake8ignore = config._flake8ignore(path)
if flake8ignore is not None:
if hasattr(Flake8Item, "from_parent"):
item = Flake8Item.from_parent(parent, fspath=path)
item.flake8ignore = flake8ignore
item.maxlength = config._flake8maxlen
item.maxdoclength = config._flake8maxdoclen
item.maxcomplexity = config._flake8maxcomplexity
item.showshource = config._flake8showshource
item.statistics = config._flake8statistics
return item
else:
return Flake8Item(
path,
parent,
flake8ignore=flake8ignore,
maxlength=config._flake8maxlen,
maxdoclength=config._flake8maxdoclen,
maxcomplexity=config._flake8maxcomplexity,
showshource=config._flake8showshource,
statistics=config._flake8statistics)
item = Flake8File.from_parent(
parent, path=file_path,
flake8ignore=flake8ignore,
maxlength=config._flake8maxlen,
maxdoclength=config._flake8maxdoclen,
maxcomplexity=config._flake8maxcomplexity,
showsource=config._flake8showsource,
statistics=config._flake8statistics)
return item


def pytest_unconfigure(config):
Expand All @@ -98,21 +88,37 @@ class Flake8Error(Exception):
""" indicates an error during flake8 checks. """


class Flake8Item(pytest.Item, pytest.File):
class Flake8File(pytest.File):

def __init__(self, fspath, parent, flake8ignore=None, maxlength=None,
maxdoclength=None,
maxcomplexity=None, showshource=None, statistics=None):
super(Flake8Item, self).__init__(fspath, parent)
self._nodeid += "::FLAKE8"
self.add_marker("flake8")
def __init__(self, *k,
flake8ignore=None, maxlength=None, maxdoclength=None,
maxcomplexity=None, showsource=None, statistics=None,
**kw):
super().__init__(*k, **kw)
self.flake8ignore = flake8ignore
self.maxlength = maxlength
self.maxdoclength = maxdoclength
self.maxcomplexity = maxcomplexity
self.showshource = showshource
self.showsource = showsource
self.statistics = statistics

def collect(self):
return [Flake8Item.from_parent(self, name="flake-8")]


class Flake8Item(pytest.Item):

def __init__(self, *k, **kwargs):
super().__init__(*k, **kwargs)
self._nodeid += "::FLAKE8"
self.add_marker("flake8")
self.flake8ignore = self.parent.flake8ignore
self.maxlength = self.parent.maxlength
self.maxdoclength = self.parent.maxdoclength
self.maxcomplexity = self.parent.maxcomplexity
self.showsource = self.parent.showsource
self.statistics = self.parent.statistics

def setup(self):
if hasattr(self.config, "_flake8mtimes"):
flake8mtimes = self.config._flake8mtimes
Expand All @@ -133,7 +139,7 @@ def runtest(self):
self.maxlength,
self.maxdoclength,
self.maxcomplexity,
self.showshource,
self.showsource,
self.statistics
)
to.flush()
Expand Down Expand Up @@ -161,9 +167,6 @@ def reportinfo(self):
ignores = ""
return (self.fspath, -1, "FLAKE8-check%s" % ignores)

def collect(self):
return iter((self,))


class Ignorer:
def __init__(self, ignorelines, coderex=re.compile(r"[EW]\d\d\d")):
Expand Down Expand Up @@ -196,7 +199,7 @@ def __call__(self, path):


def check_file(path, flake8ignore, maxlength, maxdoclenght, maxcomplexity,
showshource, statistics):
showsource, statistics):
"""Run flake8 over a single file, and return the number of failures."""
args = []
if maxlength:
Expand All @@ -205,34 +208,24 @@ def check_file(path, flake8ignore, maxlength, maxdoclenght, maxcomplexity,
args += ['--max-doc-length', maxdoclenght]
if maxcomplexity:
args += ['--max-complexity', maxcomplexity]
if showshource:
if showsource:
args += ['--show-source']
if statistics:
args += ['--statistics']
app = application.Application()
if not hasattr(app, 'parse_preliminary_options_and_args'): # flake8 >= 3.8
prelim_opts, remaining_args = app.parse_preliminary_options(args)
config_finder = config.ConfigFileFinder(
app.program,
prelim_opts.append_config,
config_file=prelim_opts.config,
ignore_config_files=prelim_opts.isolated,
)
app.find_plugins(config_finder)
app.register_plugin_options()
app.parse_configuration_and_cli(config_finder, remaining_args)
else:
app.parse_preliminary_options_and_args(args)
app.make_config_finder()
app.find_plugins()
app.register_plugin_options()
app.parse_configuration_and_cli(args)
prelim_opts, remaining_args = app.parse_preliminary_options(args)
config_finder = config.ConfigFileFinder(
app.program,
prelim_opts.append_config,
config_file=prelim_opts.config,
ignore_config_files=prelim_opts.isolated,
)
app.find_plugins(config_finder)
app.register_plugin_options()
app.parse_configuration_and_cli(config_finder, remaining_args)
if flake8ignore:
app.options.ignore = flake8ignore
app.make_formatter() # fix this
if hasattr(app, 'make_notifier'):
# removed in flake8 3.7+
app.make_notifier()
app.make_guide()
app.make_file_checker_manager()
app.run_checks([str(path)])
Expand Down
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='pytest-flake8',
version='1.1.0',
version='1.1.1',
description='pytest plugin to check FLAKE8 requirements',
long_description=open("README.rst").read(),
classifiers=[
Expand All @@ -15,8 +15,7 @@
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
Expand All @@ -38,7 +37,7 @@
'pytest11': ['flake8 = pytest_flake8'],
},
install_requires=[
'flake8>=3.5',
'pytest>=3.5',
'flake8>=4.0',
'pytest>=7.0',
],
)

0 comments on commit d730278

Please sign in to comment.