diff options
| -rw-r--r-- | CONTRIBUTORS.txt | 4 | ||||
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | doc/whatsnew/2.9.rst | 2 | ||||
| -rw-r--r-- | pylint/lint/expand_modules.py | 39 | ||||
| -rw-r--r-- | pylint/lint/pylinter.py | 16 | ||||
| -rw-r--r-- | tests/lint/unittest_expand_modules.py | 34 | 
6 files changed, 69 insertions, 30 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index ee21f60c7..c7f4771aa 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -495,3 +495,7 @@ contributors:  * ruro: contributor  * David Liu (david-yz-liu): contributor + +* Bernard Nauwelaerts: contributor + +* Fabian Damken: contributor @@ -9,6 +9,10 @@ Release date: TBA  ..    Put new features and bugfixes here and also in 'doc/whatsnew/2.9.rst' +* Added ``ignore-paths`` behaviour. Defined regex patterns are matched against full file path. + +  Close #2541 +  * Fix false negative for ``consider-using-with`` if calls like ``open()`` were used outside of assignment expressions.  * The warning for ``arguments-differ`` now signals explicitly the difference it detected diff --git a/doc/whatsnew/2.9.rst b/doc/whatsnew/2.9.rst index f6f6a5b2a..14befbb16 100644 --- a/doc/whatsnew/2.9.rst +++ b/doc/whatsnew/2.9.rst @@ -51,3 +51,5 @@ Other Changes  * The ``using-constant-test`` checker now detects constant tests consisting of list literals    like ``[]`` and ``[1, 2, 3]``. + +* ``ignore-paths`` configuration directive has been added. Defined regex patterns are matched against file path. diff --git a/pylint/lint/expand_modules.py b/pylint/lint/expand_modules.py index 2c080c804..11927eb32 100644 --- a/pylint/lint/expand_modules.py +++ b/pylint/lint/expand_modules.py @@ -1,5 +1,6 @@  import os  import sys +from typing import List, Pattern, Tuple  from astroid import modutils @@ -28,32 +29,33 @@ def get_python_path(filepath: str) -> str:              return os.getcwd() -def _basename_in_ignore_list_re(base_name, ignore_list_re): -    """Determines if the basename is matched in a regex ignorelist - -    :param str base_name: The basename of the file -    :param list ignore_list_re: A collection of regex patterns to match against. -        Successful matches are ignored. - -    :returns: `True` if the basename is ignored, `False` otherwise. -    :rtype: bool -    """ +def _is_in_ignore_list_re(element: str, ignore_list_re: List[Pattern]) -> bool: +    """determines if the element is matched in a regex ignore-list"""      for file_pattern in ignore_list_re: -        if file_pattern.match(base_name): +        if file_pattern.match(element):              return True      return False -def expand_modules(files_or_modules, ignore_list, ignore_list_re): -    """Take a list of files/modules/packages and return the list of tuple -    (file, module name) which have to be actually checked.""" +def expand_modules( +    files_or_modules: List[str], +    ignore_list: List[str], +    ignore_list_re: List[Pattern], +    ignore_list_paths_re: List[Pattern], +) -> Tuple[List[dict], List[dict]]: +    """take a list of files/modules/packages and return the list of tuple +    (file, module name) which have to be actually checked +    """      result = []      errors = []      path = sys.path.copy() +      for something in files_or_modules:          basename = os.path.basename(something) -        if basename in ignore_list or _basename_in_ignore_list_re( -            basename, ignore_list_re +        if ( +            basename in ignore_list +            or _is_in_ignore_list_re(os.path.basename(something), ignore_list_re) +            or _is_in_ignore_list_re(something, ignore_list_paths_re)          ):              continue          module_path = get_python_path(something) @@ -117,10 +119,11 @@ def expand_modules(files_or_modules, ignore_list, ignore_list_re):              ):                  if filepath == subfilepath:                      continue -                if _basename_in_ignore_list_re( +                if _is_in_ignore_list_re(                      os.path.basename(subfilepath), ignore_list_re -                ): +                ) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re):                      continue +                  modpath = _modpath_from_file(                      subfilepath, is_namespace, path=additional_search_path                  ) diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index fc7cee346..46818fb83 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -187,6 +187,17 @@ class PyLinter(                  },              ),              ( +                "ignore-paths", +                { +                    "type": "regexp_csv", +                    "metavar": "<pattern>[,<pattern>...]", +                    "dest": "ignore_list_paths_re", +                    "default": (), +                    "help": "Add files or directories matching the regex patterns to the" +                    " ignore-list. The regex matches against paths.", +                }, +            ), +            (                  "persistent",                  {                      "default": True, @@ -1046,7 +1057,10 @@ class PyLinter(      def _expand_files(self, modules):          """get modules and errors from a list of modules and handle errors"""          result, errors = expand_modules( -            modules, self.config.black_list, self.config.black_list_re +            modules, +            self.config.black_list, +            self.config.black_list_re, +            self.config.ignore_list_paths_re,          )          for error in errors:              message = modname = error["mod"] diff --git a/tests/lint/unittest_expand_modules.py b/tests/lint/unittest_expand_modules.py index 99be557bf..a6618ce50 100644 --- a/tests/lint/unittest_expand_modules.py +++ b/tests/lint/unittest_expand_modules.py @@ -7,19 +7,29 @@ from pathlib import Path  import pytest -from pylint.lint.expand_modules import _basename_in_ignore_list_re, expand_modules +from pylint.lint.expand_modules import _is_in_ignore_list_re, expand_modules -def test__basename_in_ignore_list_re_match(): -    patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")] -    assert _basename_in_ignore_list_re("unittest_utils.py", patterns) -    assert _basename_in_ignore_list_re("cheese_enchiladas.xml", patterns) +def test__is_in_ignore_list_re_match(): +    patterns = [ +        re.compile(".*enchilada.*"), +        re.compile("unittest_.*"), +        re.compile(".*tests/.*"), +    ] +    assert _is_in_ignore_list_re("unittest_utils.py", patterns) +    assert _is_in_ignore_list_re("cheese_enchiladas.xml", patterns) +    assert _is_in_ignore_list_re("src/tests/whatever.xml", patterns) -def test__basename_in_ignore_list_re_nomatch(): -    patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")] -    assert not _basename_in_ignore_list_re("test_utils.py", patterns) -    assert not _basename_in_ignore_list_re("enchilad.py", patterns) +def test__is_in_ignore_list_re_nomatch(): +    patterns = [ +        re.compile(".*enchilada.*"), +        re.compile("unittest_.*"), +        re.compile(".*tests/.*"), +    ] +    assert not _is_in_ignore_list_re("test_utils.py", patterns) +    assert not _is_in_ignore_list_re("enchilad.py", patterns) +    assert not _is_in_ignore_list_re("src/tests.py", patterns)  TEST_DIRECTORY = Path(__file__).parent.parent @@ -70,8 +80,10 @@ init_of_package = {      ],  )  def test_expand_modules(files_or_modules, expected): -    ignore_list, ignore_list_re = [], [] -    modules, errors = expand_modules(files_or_modules, ignore_list, ignore_list_re) +    ignore_list, ignore_list_re, ignore_list_paths_re = [], [], [] +    modules, errors = expand_modules( +        files_or_modules, ignore_list, ignore_list_re, ignore_list_paths_re +    )      modules.sort(key=lambda d: d["name"])      assert modules == expected      assert not errors  | 
