66

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 directory 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ciantic
  • 6,064
  • 4
  • 54
  • 49
  • 2
    pylint currently only respects the last `--ignore` option in your path name. Are you using `--ignore` multiple times maybe? http://www.logilab.org/ticket/22273 – badp Mar 24 '10 at 08:05
  • Is this pydev-related? Perhaps add tags like pydev or eclipse? I'm searching for this as well. – michuelnik Apr 05 '14 at 16:59
  • Does this answer your question? [Pylint: Disable specific warnings for specific folder](https://stackoverflow.com/questions/36182847/pylint-disable-specific-warnings-for-specific-folder) – rom_pep Dec 18 '19 at 10:58

9 Answers9

49

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
marqueed
  • 1,083
  • 10
  • 12
  • I can confirm that `ignore=migrations` still works with pylint 1.3.1 – Peterino Oct 21 '14 at 13:32
  • 6
    This did *not* work for me. However, using the **module-name** did the trick. So for a package like `foo/bar` I had to use `foo.bar` instead of `bar`. – exhuma Dec 21 '17 at 16:27
16

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe Linoff
  • 761
  • 9
  • 13
  • 11
    This does not seem to work for me (in pylint 1.9.2), first because it doesn't like the `**`, (I guess they changed it to the python regexp parser). Even after making it a proper regexp the `ignore-patterns` argument seems to only operate on the name of the file being tested, and does not seem to work for excluding a directory – Peter Aug 12 '19 at 15:07
13

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

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

If you want to configure this within your pyproject.toml, the configuration would look something like this:

[tool.pylint.MASTER]
ignore-paths = '^src/experiments/.*$'

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

fresskoma
  • 25,481
  • 10
  • 85
  • 128
Jonathan Foster
  • 163
  • 2
  • 7
  • 2
    @moderator - Probably not appropriate but this answer should but the new validated one as this post is very old but the issue still recent and Jonathan Foster answer is the right one today. – Simon Provost May 15 '23 at 16:26
  • Note that if running from a shell script, it's safer to enclose the _complete_ list in single-quotes. Do _not_ quote each individual pattern (the pattern value should be a string, including commas). – Abhijit Sarkar Jun 22 '23 at 19:49
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
max
  • 29,122
  • 12
  • 52
  • 79
  • 2
    why is it like this??? why not ignore a whole directory?? I have to give all the file names or patterns for all the files in directory ??? this is not a correct way right – Lokesh Sanapalli Mar 04 '19 at 11:59
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrea Franchini
  • 548
  • 4
  • 14
3

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:

Fabian Damken
  • 1,477
  • 1
  • 15
  • 24
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Izana
  • 2,537
  • 27
  • 33
-2

You can then use Bash expansion to your advantage:

--ignore=migrations/{0000..1000}_something
badp
  • 11,409
  • 3
  • 61
  • 89
  • 1
    I think the ignore allowes only *basename*, so the `migrations/` would not work. Also I'm developing under Windows so I think Bash is out of question. – Ciantic Mar 24 '10 at 14:31
  • This post looks a little long so i'll ask again. Maybe something has changed. Is it possible to exclude the whole `migrations` directory? I'm on Windows as well. :( Thanks. – Mridang Agarwalla Oct 24 '11 at 14:21
  • 1
    Yes, this works for me. My line looks like: ignore=CVS,migrations – Harlin Mar 22 '17 at 03:45