开发者

Ignore by directory using Pylint

The following is from the Pylint documentation:

--ignore=<file>
    Add <file or directory> to the black list. It should be a 
    base name, not a path. You may set this option multiple 
    times. [current: %default]

Yet, I'm not having luck getting the directory part work.

I have dire开发者_如何学Pythonctory called migrations, which has django-south migration files. As I enter --ignore=migrations, it still keeps giving me the errors/warnings in files inside the migrations directory.

Could it be that --ignore is not working for directories?

If I could even use a regular expression to match the ignored files, it would work, since django-south files are all named 0001_something, 0002_something...


Since I could not get the ignore by directory to work, I have resorted to simply putting # pylint: disable-msg-cat=WCREFI on top of each migration file, which ignores all Pylint errors, warnings, and information.


Adding the following to my .pylintrc files works with Pylint 0.25:

[MASTER]
ignore=migrations

My problems are with PyDev which (it seems) is not respecting my settings. This is due, I think, to the fact that it's running Pylint per-file, which I think bypasses 'ignore' checks - whether for modules/directories or files. The calls to Pylint from PyDev look like:

/path/to/site-packages/pylint/lint.py --include-ids=y /path/to/project/migrations/0018_migration.py


To ignore subdirectories under a directory tree named 3rdparty, we added the following ignore-patterns entry to the [MASTER] entry in .pylintrc.

# Add files or directories matching the regex patterns to the blacklist. The
# regex matches against base names, not paths.
# Ignore all .py files under the 3rdparty subdirectory.
ignore-patterns=**/3rdparty/**/*.py

This fixed the problem for Pylint 1.7.1.

We were originally confused by the "base names" clause in the comments. Apparently it does accept paths with wildcards. At least it did for us.


You can not give a path, but only the "basename" of the directory. E.g., use --ignore=lib instead of --ignore-=appengine-toolkit/gaetk/lib.

The problem is you will ignore all directories named lib.


As of right now, --ignore is not working on directories (there's an open issue on GitHub, Ignore clause not ignoring directories #2686).

It turns out that only the file name, and not the whole path is tested against the black list. (As pointed in the same pull request: geajack's comment)

The option --ignore-patterns has the same problem, but there was an attempt to fix it when I checked (Add ignore-paths to match against the full path #3266)

In your case, the best solution right now would be to use a regular expression to match the patterns you want in your files, which was also my case. As I am not really well-versed in regular expressions, I used Regex101, which I recommend.


Adding to Fabien's answer, --ignore-paths syntax looks like this.

ignore-paths=^src/experiments/.*$,
             ^src/ingredients/.*$,
             ^src/scripts/.*$,
             ^src/tests/.*$

https://github.com/PyCQA/pylint/issues/2686#issuecomment-864585778


Actually, with Pylint 2.3.1 there is an open issue.

If you set a directory into the ignore options, it won't ignore it.


Starting with Pylint 2.9, there will be a new configuration directory ignore-paths that matches a RegEx against the whole path and not the basename.

See also:

  • https://github.com/PyCQA/pylint/blob/master/doc/whatsnew/2.9.rst
  • https://github.com/PyCQA/pylint/issues/2541
  • https://github.com/PyCQA/pylint/issues/2686
  • https://github.com/PyCQA/pylint/pull/4516


It seems you need to do some monkey patching, which works for me with Pylint version 2.5.3. I just don't know why Pylint doesn't have a fix for ignoring paths.

Add this to your .pylintrc file:

init-hook=
    sys.path.append(os.getcwd());
    from pylint_ignore import PylintIgnorePaths;
    PylintIgnorePaths('my/thirdparty/subdir', 'my/other/badcode')

Then create file pylint_ignore.py:

from pylint.utils import utils


class PylintIgnorePaths:
    def __init__(self, *paths):
        self.paths = paths
        self.original_expand_modules = utils.expand_modules
        utils.expand_modules = self.patched_expand

    def patched_expand(self, *args, **kwargs):
        result, errors = self.original_expand_modules(*args, **kwargs)

        def keep_item(item):
            if any(1 for path in self.paths if item['path'].startswith(path)):
                return False

            return True

        result = list(filter(keep_item, result))

        return result, errors


You can then use Bash expansion to your advantage:

--ignore=migrations/{0000..1000}_something
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜