diff options
author | Craig Citro <craigcitro@gmail.com> | 2017-05-22 05:42:45 -0700 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2017-05-22 14:42:45 +0200 |
commit | d03b949b64ed2d4755c556a4cabb6f5acc70487b (patch) | |
tree | 076f6b5f544cf739674222ef99389f17e085de9f | |
parent | da7566aede3ef98278e9b1b350f6e0a8ea3fd1d7 (diff) | |
download | pylint-git-d03b949b64ed2d4755c556a4cabb6f5acc70487b.tar.gz |
Use the full filename for identifying __init__.py. (#1461)
Previously, `pylint.utils.expand_modules` identified `__init__.py` files by a
substring match, which led to false positives for files with names like
`flycheck__init__.py`. (While users are unlikely to choose such a filename,
tools like flycheck use these sorts of filenames for temporary files.)
The result was that `pylint` would end up linting the entire package, not just
the file in question.
The fix is straightforward -- we use `os.path.basename` instead of a substring
check, and add a test.
-rw-r--r-- | pylint/test/input/not__init__.py | 1 | ||||
-rw-r--r-- | pylint/test/unittest_lint.py | 14 | ||||
-rw-r--r-- | pylint/utils.py | 2 |
3 files changed, 16 insertions, 1 deletions
diff --git a/pylint/test/input/not__init__.py b/pylint/test/input/not__init__.py new file mode 100644 index 000000000..60e92b76c --- /dev/null +++ b/pylint/test/input/not__init__.py @@ -0,0 +1 @@ +"""test""" diff --git a/pylint/test/unittest_lint.py b/pylint/test/unittest_lint.py index a6e401cbb..0c48b49be 100644 --- a/pylint/test/unittest_lint.py +++ b/pylint/test/unittest_lint.py @@ -745,3 +745,17 @@ def test_custom_should_analyze_file(): messages = reporter.messages assert len(messages) == 1 assert 'invalid syntax' in messages[0] + + +def test_filename_with__init__(init_linter): + # This tracks a regression where a file whose name ends in __init__.py, + # such as flycheck__init__.py, would accidentally lead to linting the + # entire containing directory. + reporter = testutils.TestReporter() + linter = init_linter + linter.open() + linter.set_reporter(reporter) + filepath = join(INPUTDIR, 'not__init__.py') + linter.check([filepath]) + messages = reporter.messages + assert len(messages) == 0 diff --git a/pylint/utils.py b/pylint/utils.py index 1ace59392..4dbfd9e1a 100644 --- a/pylint/utils.py +++ b/pylint/utils.py @@ -881,7 +881,7 @@ def expand_modules(files_or_modules, black_list, black_list_re): 'basepath': filepath, 'basename': modname}) has_init = (not (modname.endswith('.__init__') or modname == '__init__') - and '__init__.py' in filepath) + and basename(filepath) == '__init__.py') if has_init or is_namespace or is_directory: for subfilepath in modutils.get_module_files(dirname(filepath), black_list, |