Commits

Dobrosław Żybort  committed 985879e

Add: option to show only selected pep8 errors with 'pep8select'

  • Participants
  • Parent commits e75a847

Comments (0)

Files changed (1)

File pytest_pep8.py

     group = parser.getgroup("general")
     group.addoption('--pep8', action='store_true',
         help="perform some pep8 sanity checks on .py files")
+    parser.addini("pep8select", type="linelist",
+        help="each line specifies a glob pattern and whitespace "
+             "separated PEP8 errors or warnings which will be selected, "
+             "example: *.py W293")
     parser.addini("pep8ignore", type="linelist",
         help="each line specifies a glob pattern and whitespace "
              "separated PEP8 errors or warnings which will be ignored, "
 def pytest_sessionstart(session):
     config = session.config
     if config.option.pep8:
+        config._pep8select = Ignorer(config.getini("pep8select"))
         config._pep8ignore = Ignorer(config.getini("pep8ignore"))
         config._pep8mtimes = config.cache.get(HISTKEY, {})
         config._max_line_length = int(config.getini("pep8maxlinelength")
 def pytest_collect_file(path, parent):
     config = parent.config
     if config.option.pep8 and path.ext == '.py':
+        pep8select = config._pep8select(path)
         pep8ignore = config._pep8ignore(path)
         if pep8ignore is not None:
-            return Pep8Item(path, parent, pep8ignore, config._max_line_length)
+            return Pep8Item(path, parent, pep8select, pep8ignore,
+                            config._max_line_length)
 
 
 def pytest_sessionfinish(session):
 
 class Pep8Item(pytest.Item, pytest.File):
 
-    def __init__(self, path, parent, pep8ignore, max_line_length):
+    def __init__(self, path, parent, pep8select, pep8ignore, max_line_length):
         super(Pep8Item, self).__init__(path, parent)
         if hasattr(self, "markers"):
             self.markers.pep8 = pytest.mark.pep8
         else:
             self.keywords["pep8"] = pytest.mark.pep8
+        self.pep8select = pep8select
         self.pep8ignore = pep8ignore
         self.max_line_length = max_line_length
 
         pep8mtimes = self.config._pep8mtimes
         self._pep8mtime = self.fspath.mtime()
         old = pep8mtimes.get(str(self.fspath), (0, []))
-        if old == (self._pep8mtime, self.pep8ignore):
+        if old == (self._pep8mtime, self.pep8select, self.pep8ignore):
             pytest.skip("file(s) previously passed PEP8 checks")
 
     def runtest(self):
         call = py.io.StdCapture.call
-        found_errors, out, err = call(check_file, self.fspath, self.pep8ignore,
-                                      self.max_line_length)
+        found_errors, out, err = call(check_file, self.fspath, self.pep8select,
+                                      self.pep8ignore, self.max_line_length)
         if found_errors:
             raise Pep8Error(out, err)
         # update mtime only if test passed
         # otherwise failures would not be re-run next time
         self.config._pep8mtimes[str(self.fspath)] = (self._pep8mtime,
+                                                     self.pep8select,
                                                      self.pep8ignore)
 
     def repr_failure(self, excinfo):
         return super(Pep8Item, self).repr_failure(excinfo)
 
     def reportinfo(self):
-        if self.pep8ignore:
+        selects = ""
+        ignores = ""
+        if self.pep8select and self.pep8ignore:
+            selects = "(selecting %s)" % " ".join(self.pep8select)
             ignores = "(ignoring %s)" % " ".join(self.pep8ignore)
-        else:
-            ignores = ""
-        return (self.fspath, -1, "PEP8-check%s" % ignores)
+        elif self.pep8select:
+            selects = "(ignoring %s)" % " ".join(self.pep8select)
+        elif self.pep8ignore:
+            ignores = "(ignoring %s)" % " ".join(self.pep8ignore)
+        return (self.fspath, -1, "PEP8-check%s%s" % (selects, ignores))
 
 
 class Ignorer:
         return l
 
 
-def check_file(path, pep8ignore, max_line_length):
-    checker = pep8.Checker(str(path), ignore=pep8ignore, show_source=1,
-                           max_line_length=max_line_length)
+def check_file(path, pep8select, pep8ignore, max_line_length):
+    checker = pep8.Checker(str(path), select=pep8select, ignore=pep8ignore,
+                           show_source=1, max_line_length=max_line_length)
     return checker.check_all()