1010
1111import pytest
1212
13- __version__ = '0.6 '
13+ __version__ = '1.1.1 '
1414
1515HISTKEY = "flake8/mtimes"
1616
@@ -53,39 +53,29 @@ def pytest_configure(config):
5353 config ._flake8maxlen = config .getini ("flake8-max-line-length" )
5454 config ._flake8maxdoclen = config .getini ("flake8-max-doc-length" )
5555 config ._flake8maxcomplexity = config .getini ("flake8-max-complexity" )
56- config ._flake8showshource = config .getini ("flake8-show-source" )
56+ config ._flake8showsource = config .getini ("flake8-show-source" )
5757 config ._flake8statistics = config .getini ("flake8-statistics" )
5858 config ._flake8exts = config .getini ("flake8-extensions" )
5959 config .addinivalue_line ('markers' , "flake8: Tests which run flake8." )
6060 if hasattr (config , 'cache' ):
6161 config ._flake8mtimes = config .cache .get (HISTKEY , {})
6262
6363
64- def pytest_collect_file (path , parent ):
64+ def pytest_collect_file (file_path , path , parent ):
6565 """Filter files down to which ones should be checked."""
6666 config = parent .config
67- if config .option .flake8 and path . ext in config ._flake8exts :
67+ if config .option .flake8 and file_path . suffix in config ._flake8exts :
6868 flake8ignore = config ._flake8ignore (path )
6969 if flake8ignore is not None :
70- if hasattr (Flake8Item , "from_parent" ):
71- item = Flake8Item .from_parent (parent , fspath = path )
72- item .flake8ignore = flake8ignore
73- item .maxlength = config ._flake8maxlen
74- item .maxdoclength = config ._flake8maxdoclen
75- item .maxcomplexity = config ._flake8maxcomplexity
76- item .showshource = config ._flake8showshource
77- item .statistics = config ._flake8statistics
78- return item
79- else :
80- return Flake8Item (
81- path ,
82- parent ,
83- flake8ignore = flake8ignore ,
84- maxlength = config ._flake8maxlen ,
85- maxdoclength = config ._flake8maxdoclen ,
86- maxcomplexity = config ._flake8maxcomplexity ,
87- showshource = config ._flake8showshource ,
88- statistics = config ._flake8statistics )
70+ item = Flake8File .from_parent (
71+ parent , path = file_path ,
72+ flake8ignore = flake8ignore ,
73+ maxlength = config ._flake8maxlen ,
74+ maxdoclength = config ._flake8maxdoclen ,
75+ maxcomplexity = config ._flake8maxcomplexity ,
76+ showsource = config ._flake8showsource ,
77+ statistics = config ._flake8statistics )
78+ return item
8979
9080
9181def pytest_unconfigure (config ):
@@ -98,21 +88,37 @@ class Flake8Error(Exception):
9888 """ indicates an error during flake8 checks. """
9989
10090
101- class Flake8Item ( pytest . Item , pytest .File ):
91+ class Flake8File ( pytest .File ):
10292
103- def __init__ (self , fspath , parent , flake8ignore = None , maxlength = None ,
104- maxdoclength = None ,
105- maxcomplexity = None , showshource = None , statistics = None ):
106- super (Flake8Item , self ).__init__ (fspath , parent )
107- self ._nodeid += "::FLAKE8"
108- self .add_marker ("flake8" )
93+ def __init__ (self , * k ,
94+ flake8ignore = None , maxlength = None , maxdoclength = None ,
95+ maxcomplexity = None , showsource = None , statistics = None ,
96+ ** kw ):
97+ super ().__init__ (* k , ** kw )
10998 self .flake8ignore = flake8ignore
11099 self .maxlength = maxlength
111100 self .maxdoclength = maxdoclength
112101 self .maxcomplexity = maxcomplexity
113- self .showshource = showshource
102+ self .showsource = showsource
114103 self .statistics = statistics
115104
105+ def collect (self ):
106+ return [Flake8Item .from_parent (self , name = "flake-8" )]
107+
108+
109+ class Flake8Item (pytest .Item ):
110+
111+ def __init__ (self , * k , ** kwargs ):
112+ super ().__init__ (* k , ** kwargs )
113+ self ._nodeid += "::FLAKE8"
114+ self .add_marker ("flake8" )
115+ self .flake8ignore = self .parent .flake8ignore
116+ self .maxlength = self .parent .maxlength
117+ self .maxdoclength = self .parent .maxdoclength
118+ self .maxcomplexity = self .parent .maxcomplexity
119+ self .showsource = self .parent .showsource
120+ self .statistics = self .parent .statistics
121+
116122 def setup (self ):
117123 if hasattr (self .config , "_flake8mtimes" ):
118124 flake8mtimes = self .config ._flake8mtimes
@@ -133,7 +139,7 @@ def runtest(self):
133139 self .maxlength ,
134140 self .maxdoclength ,
135141 self .maxcomplexity ,
136- self .showshource ,
142+ self .showsource ,
137143 self .statistics
138144 )
139145 to .flush ()
@@ -161,9 +167,6 @@ def reportinfo(self):
161167 ignores = ""
162168 return (self .fspath , - 1 , "FLAKE8-check%s" % ignores )
163169
164- def collect (self ):
165- return iter ((self ,))
166-
167170
168171class Ignorer :
169172 def __init__ (self , ignorelines , coderex = re .compile (r"[EW]\d\d\d" )):
@@ -196,7 +199,7 @@ def __call__(self, path):
196199
197200
198201def check_file (path , flake8ignore , maxlength , maxdoclenght , maxcomplexity ,
199- showshource , statistics ):
202+ showsource , statistics ):
200203 """Run flake8 over a single file, and return the number of failures."""
201204 args = []
202205 if maxlength :
@@ -205,34 +208,24 @@ def check_file(path, flake8ignore, maxlength, maxdoclenght, maxcomplexity,
205208 args += ['--max-doc-length' , maxdoclenght ]
206209 if maxcomplexity :
207210 args += ['--max-complexity' , maxcomplexity ]
208- if showshource :
211+ if showsource :
209212 args += ['--show-source' ]
210213 if statistics :
211214 args += ['--statistics' ]
212215 app = application .Application ()
213- if not hasattr (app , 'parse_preliminary_options_and_args' ): # flake8 >= 3.8
214- prelim_opts , remaining_args = app .parse_preliminary_options (args )
215- config_finder = config .ConfigFileFinder (
216- app .program ,
217- prelim_opts .append_config ,
218- config_file = prelim_opts .config ,
219- ignore_config_files = prelim_opts .isolated ,
220- )
221- app .find_plugins (config_finder )
222- app .register_plugin_options ()
223- app .parse_configuration_and_cli (config_finder , remaining_args )
224- else :
225- app .parse_preliminary_options_and_args (args )
226- app .make_config_finder ()
227- app .find_plugins ()
228- app .register_plugin_options ()
229- app .parse_configuration_and_cli (args )
216+ prelim_opts , remaining_args = app .parse_preliminary_options (args )
217+ config_finder = config .ConfigFileFinder (
218+ app .program ,
219+ prelim_opts .append_config ,
220+ config_file = prelim_opts .config ,
221+ ignore_config_files = prelim_opts .isolated ,
222+ )
223+ app .find_plugins (config_finder )
224+ app .register_plugin_options ()
225+ app .parse_configuration_and_cli (config_finder , remaining_args )
230226 if flake8ignore :
231227 app .options .ignore = flake8ignore
232228 app .make_formatter () # fix this
233- if hasattr (app , 'make_notifier' ):
234- # removed in flake8 3.7+
235- app .make_notifier ()
236229 app .make_guide ()
237230 app .make_file_checker_manager ()
238231 app .run_checks ([str (path )])
0 commit comments