diff options
| author | cpopa <devnull@localhost> | 2014-08-08 22:58:04 +0300 |
|---|---|---|
| committer | cpopa <devnull@localhost> | 2014-08-08 22:58:04 +0300 |
| commit | e7ffab27ea531e3142e9a439264a0e3458e1a3ac (patch) | |
| tree | 26d093398e97f33fbcad04144478e0899f8e3155 | |
| parent | 82ea94048db995ddef0d56a3f55b54a50ed57763 (diff) | |
| download | pylint-e7ffab27ea531e3142e9a439264a0e3458e1a3ac.tar.gz | |
Don't emit 'import-error' if an import was protected by a try-except, which excepted ImportError.
| -rw-r--r-- | ChangeLog | 3 | ||||
| -rw-r--r-- | checkers/imports.py | 34 | ||||
| -rw-r--r-- | test/functional/import_error.py | 18 | ||||
| -rw-r--r-- | test/functional/import_error.txt | 2 | ||||
| -rw-r--r-- | test/input/func_f0001.py | 4 | ||||
| -rw-r--r-- | test/messages/func_f0001.txt | 2 |
6 files changed, 55 insertions, 8 deletions
@@ -38,6 +38,9 @@ ChangeLog for Pylint * Properly handle unicode format strings for Python 2. Closes issue #296. + * Don't emit 'import-error' if an import was protected by a try-except, + which excepted ImportError. + 2014-07-26 -- 1.3.0 * Allow hanging continued indentation for implicitly concatenated diff --git a/checkers/imports.py b/checkers/imports.py index 2f02a88..51976a7 100644 --- a/checkers/imports.py +++ b/checkers/imports.py @@ -27,8 +27,37 @@ from astroid.modutils import get_module_part, is_standard_module from pylint.interfaces import IAstroidChecker from pylint.utils import EmptyReport from pylint.checkers import BaseChecker -from pylint.checkers.utils import check_messages +from pylint.checkers.utils import ( + check_messages, inherit_from_std_ex, + is_builtin_object) +def _except_import_error(node): + """ + Check if the try-except node has an ImportError handler. + Return True if an ImportError handler was infered, False otherwise. + """ + if not isinstance(node, astroid.TryExcept): + return + for handler in node.handlers: + names = None + if isinstance(handler.type, astroid.Tuple): + names = [name for name in handler.type.elts + if isinstance(name, astroid.Name)] + elif isinstance(handler.type, astroid.Name): + names = [handler.type] + else: + # Don't try to infer that. + return + for name in names: + try: + for infered in name.infer(): + if (is_builtin_object(infered) and + inherit_from_std_ex(infered) and + infered.name == 'ImportError'): + return True + except astroid.InferenceError: + continue + return False def get_first_import(node, context, name, base, level): """return the node where [base.]<name> is imported or None if not found @@ -278,7 +307,8 @@ given file (report RP0402 must not be disabled)'} args = '%r (%s)' % (modname, ex) else: args = repr(modname) - self.add_message("import-error", args=args, node=importnode) + if not _except_import_error(importnode.parent): + self.add_message("import-error", args=args, node=importnode) def _check_relative_import(self, modnode, importnode, importedmodnode, importedasname): diff --git a/test/functional/import_error.py b/test/functional/import_error.py new file mode 100644 index 0000000..d20558f --- /dev/null +++ b/test/functional/import_error.py @@ -0,0 +1,18 @@ +""" Test that import errors are detected. """
+# pylint: disable=invalid-name, unused-import
+import totally_missing # [import-error]
+
+try:
+ import maybe_missing
+except ImportError:
+ maybe_missing = None
+
+try:
+ import maybe_missing_1
+except (ImportError, SyntaxError):
+ maybe_missing_1 = None
+
+try:
+ import maybe_missing_2 # [import-error]
+except ValueError:
+ maybe_missing_2 = None
diff --git a/test/functional/import_error.txt b/test/functional/import_error.txt new file mode 100644 index 0000000..af7f739 --- /dev/null +++ b/test/functional/import_error.txt @@ -0,0 +1,2 @@ +import-error:3::Unable to import 'totally_missing'
+import-error:16::Unable to import 'maybe_missing_2'
diff --git a/test/input/func_f0001.py b/test/input/func_f0001.py deleted file mode 100644 index b0e559a..0000000 --- a/test/input/func_f0001.py +++ /dev/null @@ -1,4 +0,0 @@ -"""test astroid error -""" -import whatever -__revision__ = None diff --git a/test/messages/func_f0001.txt b/test/messages/func_f0001.txt deleted file mode 100644 index 911d805..0000000 --- a/test/messages/func_f0001.txt +++ /dev/null @@ -1,2 +0,0 @@ -F: 3: Unable to import 'whatever' -W: 3: Unused import whatever |
