diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2014-02-04 16:44:45 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-02-04 16:44:45 +0200 |
commit | 9776d39734c50cdf0a4c267b6327048873486d33 (patch) | |
tree | 279d11564f4e6ef67725fd8f636bc11cf6098d0c | |
parent | 20236dc1a9ae84ba55d1ab7db85e81b86a34e295 (diff) | |
parent | 4670e1eb3845987880f50a305b55a5e674916e09 (diff) | |
download | pylint-9776d39734c50cdf0a4c267b6327048873486d33.tar.gz |
Merged in PCManticore/pylint/all_false_positive (pull request #80)
Check for invalid entries in a package's __all__, closes #126.
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | checkers/variables.py | 24 | ||||
-rw-r--r-- | test/regrtest_data/package_all/__init__.py | 3 | ||||
-rw-r--r-- | test/regrtest_data/package_all/notmissing.py | 2 | ||||
-rw-r--r-- | test/test_misc.py | 19 |
5 files changed, 46 insertions, 4 deletions
@@ -20,6 +20,8 @@ ChangeLog for Pylint * Enhance the check for 'used-before-assignment' to look for 'nonlocal' uses. + * Emit 'undefined-all-variable' if a package's __all__ + variable contains a missing submodule (closes #126). 2013-12-22 -- 1.1.0 * Add new check for use of deprecated pragma directives "pylint:disable-msg" diff --git a/checkers/variables.py b/checkers/variables.py index f461319..d5bb715 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -15,13 +15,15 @@ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """variables checkers for Python code """ - +import os import sys from copy import copy import astroid from astroid import are_exclusive, builtin_lookup, AstroidBuildingException +from logilab.common.modutils import file_from_modpath + from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker from pylint.checkers.utils import (PYMETHODS, is_ancestor_name, is_builtin, @@ -217,7 +219,25 @@ builtins. Remember that you should avoid to define new builtins when possible.' del not_consumed[elt_name] continue if elt_name not in node.locals: - self.add_message('E0603', args=elt_name, node=elt) + if not node.package: + self.add_message('undefined-all-variable', + args=elt_name, + node=elt) + else: + basename = os.path.splitext(node.file)[0] + if os.path.basename(basename) == '__init__': + name = node.name + "." + elt_name + try: + file_from_modpath(name.split(".")) + except ImportError: + self.add_message('undefined-all-variable', + args=elt_name, + node=elt) + except SyntaxError as exc: + # don't yield an syntax-error warning, + # because it will be later yielded + # when the file will be checked + pass # don't check unused imports in __init__ files if not self.config.init_import and node.package: return diff --git a/test/regrtest_data/package_all/__init__.py b/test/regrtest_data/package_all/__init__.py new file mode 100644 index 0000000..4e3696b --- /dev/null +++ b/test/regrtest_data/package_all/__init__.py @@ -0,0 +1,3 @@ +""" Check for E0603 for missing submodule found in __all__ """
+__revision__ = 1
+__all__ = ['notmissing', 'missing']
diff --git a/test/regrtest_data/package_all/notmissing.py b/test/regrtest_data/package_all/notmissing.py new file mode 100644 index 0000000..7cf8543 --- /dev/null +++ b/test/regrtest_data/package_all/notmissing.py @@ -0,0 +1,2 @@ +""" empty """
+__revision__ = 1
diff --git a/test/test_misc.py b/test/test_misc.py index a2cba9b..e71b26e 100644 --- a/test/test_misc.py +++ b/test/test_misc.py @@ -22,8 +22,9 @@ import contextlib from logilab.common.testlib import unittest_main from astroid import test_utils -from pylint.checkers import misc -from pylint.testutils import CheckerTestCase, Message +from pylint.checkers import misc, variables +from pylint.testutils import CheckerTestCase, Message, linter + @contextlib.contextmanager def create_file_backed_module(code): @@ -66,6 +67,20 @@ class FixmeTest(CheckerTestCase): with self.assertNoMessages(): self.checker.process_module(module) +class MissingSubmoduleTest(CheckerTestCase): + CHECKER_CLASS = variables.VariablesChecker + + def test_package_all(self): + regr_data = os.path.join(os.path.dirname(os.path.abspath(__file__)), + 'regrtest_data') + sys.path.insert(0, regr_data) + try: + linter.check(os.path.join(regr_data, 'package_all')) + got = linter.reporter.finalize().strip() + self.assertEqual(got, "E: 3: Undefined variable name " + "'missing' in __all__") + finally: + sys.path.pop(0) if __name__ == '__main__': unittest_main() |