Skip to content

Commit

Permalink
Add ignore-paths configuration directive
Browse files Browse the repository at this point in the history
List of regex matching against the full path

Close pylint-dev#2541
  • Loading branch information
Bernard Nauwelaerts authored and bernardn committed Jul 13, 2020
1 parent 23f82c1 commit 95a8b51
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTORS.txt
Expand Up @@ -394,4 +394,6 @@ contributors:

* Ram Rachum (cool-RR)

* Pieter Engelbrecht
* Pieter Engelbrecht

* Bernard Nauwelaerts: contributor
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -7,6 +7,10 @@ What's New in Pylint 2.6.0?

Release date: TBA

* Added ``ignore-paths`` behaviour. Defined regex patterns are matched against full file path.

Close #2541

* Fix various scope-related bugs in ``undefined-variable`` checker

Close #1082, #3434, #3461
Expand Down
2 changes: 2 additions & 0 deletions doc/whatsnew/2.6.rst
Expand Up @@ -27,3 +27,5 @@ Other Changes
* `mixed-indentation` has been removed, it is no longer useful since TabError is included directly in python3

* Fix superfluous-parens false-positive for the walrus operator

* `ignore-paths` configuration directive has been added. Defined regex patterns are matched against file path.
16 changes: 15 additions & 1 deletion pylint/lint/pylinter.py
Expand Up @@ -179,6 +179,17 @@ def make_options():
" blacklist. The regex matches against base names, not paths.",
},
),
(
"ignore-paths",
{
"type": "regexp_csv",
"metavar": "<pattern>[,<pattern>...]",
"dest": "black_list_paths_re",
"default": (),
"help": "Add files or directories matching the regex patterns to the"
" blacklist. The regex matches against paths.",
},
),
(
"persistent",
{
Expand Down Expand Up @@ -958,7 +969,10 @@ def _expand_files(self, modules):
"""get modules and errors from a list of modules and handle errors
"""
result, errors = utils.expand_modules(
modules, self.config.black_list, self.config.black_list_re
modules,
self.config.black_list,
self.config.black_list_re,
self.config.black_list_paths_re,
)
for error in errors:
message = modname = error["mod"]
Expand Down
2 changes: 1 addition & 1 deletion pylint/utils/__init__.py
Expand Up @@ -46,9 +46,9 @@
from pylint.utils.ast_walker import ASTWalker
from pylint.utils.file_state import FileState
from pylint.utils.utils import (
_basename_in_blacklist_re,
_check_csv,
_format_option_value,
_is_in_blacklist_re,
_splitstrip,
_unquote,
decoding_stream,
Expand Down
22 changes: 13 additions & 9 deletions pylint/utils/utils.py
Expand Up @@ -88,18 +88,18 @@ def tokenize_module(module):
return list(tokenize.tokenize(readline))


def _basename_in_blacklist_re(base_name, black_list_re):
"""Determines if the basename is matched in a regex blacklist
def _is_in_blacklist_re(element, black_list_re):
"""Determines if the element is matched in a regex blacklist
:param str base_name: The basename of the file
:param str element: The element to be checked.
:param list black_list_re: A collection of regex patterns to match against.
Successful matches are blacklisted.
:returns: `True` if the basename is blacklisted, `False` otherwise.
:rtype: bool
"""
for file_pattern in black_list_re:
if file_pattern.match(base_name):
if file_pattern.match(element):
return True
return False

Expand Down Expand Up @@ -127,7 +127,7 @@ def get_python_path(filepath):
return None


def expand_modules(files_or_modules, black_list, black_list_re):
def expand_modules(files_or_modules, black_list, black_list_re, black_list_paths_re):
"""take a list of files/modules/packages and return the list of tuple
(file, module name) which have to be actually checked
"""
Expand All @@ -138,7 +138,11 @@ def expand_modules(files_or_modules, black_list, black_list_re):
for something in files_or_modules:
if os.path.basename(something) in black_list:
continue
if _basename_in_blacklist_re(os.path.basename(something), black_list_re):

if _is_in_blacklist_re(os.path.basename(something), black_list_re):
continue

if _is_in_blacklist_re(something, black_list_paths_re):
continue

module_path = get_python_path(something)
Expand Down Expand Up @@ -206,9 +210,9 @@ def expand_modules(files_or_modules, black_list, black_list_re):
):
if filepath == subfilepath:
continue
if _basename_in_blacklist_re(
os.path.basename(subfilepath), black_list_re
):
if _is_in_blacklist_re(os.path.basename(subfilepath), black_list_re):
continue
if _is_in_blacklist_re(subfilepath, black_list_paths_re):
continue

modpath = _modpath_from_file(
Expand Down
12 changes: 6 additions & 6 deletions tests/utils/unittest_utils.py
Expand Up @@ -24,16 +24,16 @@
from pylint.utils import utils


def test__basename_in_blacklist_re_match():
def test__is_in_blacklist_re_match():
patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")]
assert utils._basename_in_blacklist_re("unittest_utils.py", patterns)
assert utils._basename_in_blacklist_re("cheese_enchiladas.xml", patterns)
assert utils._is_in_blacklist_re("unittest_utils.py", patterns)
assert utils._is_in_blacklist_re("cheese_enchiladas.xml", patterns)


def test__basename_in_blacklist_re_nomatch():
def test__is_in_blacklist_re_nomatch():
patterns = [re.compile(".*enchilada.*"), re.compile("unittest_.*")]
assert not utils._basename_in_blacklist_re("test_utils.py", patterns)
assert not utils._basename_in_blacklist_re("enchilad.py", patterns)
assert not utils._is_in_blacklist_re("test_utils.py", patterns)
assert not utils._is_in_blacklist_re("enchilad.py", patterns)


def test_decoding_stream_unknown_encoding():
Expand Down

0 comments on commit 95a8b51

Please sign in to comment.