From d730278626fe02a3545e9fc79999fa5ec9c30a29 Mon Sep 17 00:00:00 2001 From: Thorsten Lockert Date: Fri, 18 Mar 2022 15:11:20 -0700 Subject: [PATCH] Remove some deprecated constructs 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. --- CHANGELOG | 7 ++++ pytest_flake8.py | 107 ++++++++++++++++++++++------------------------- setup.py | 9 ++-- 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d4b6a86..36e1fd4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 ----- diff --git a/pytest_flake8.py b/pytest_flake8.py index 6aa31c7..2555f8a 100644 --- a/pytest_flake8.py +++ b/pytest_flake8.py @@ -10,7 +10,7 @@ import pytest -__version__ = '0.6' +__version__ = '1.1.1' HISTKEY = "flake8/mtimes" @@ -53,7 +53,7 @@ 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.") @@ -61,31 +61,21 @@ def pytest_configure(config): 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): @@ -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 @@ -133,7 +139,7 @@ def runtest(self): self.maxlength, self.maxdoclength, self.maxcomplexity, - self.showshource, + self.showsource, self.statistics ) to.flush() @@ -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")): @@ -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: @@ -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)]) diff --git a/setup.py b/setup.py index be31595..09af752 100644 --- a/setup.py +++ b/setup.py @@ -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=[ @@ -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", @@ -38,7 +37,7 @@ 'pytest11': ['flake8 = pytest_flake8'], }, install_requires=[ - 'flake8>=3.5', - 'pytest>=3.5', + 'flake8>=4.0', + 'pytest>=7.0', ], )