Make pylint work with new astroid exceptions, AstroidImportError and AstroidSyntaxError.

authorClaudiu Popa <pcmanticore@gmail.com>
changesetffe24a7d5a37
branchdefault
phasepublic
hiddenno
parent revision#84cb406fd1a0 Don't emit super-on-old-class on classes with unknown bases.
child revision#4e5a1be18df2 Added a new error, 'relative-beyond-top-level'.
files modified by this revision
pylint/checkers/imports.py
pylint/checkers/variables.py
pylint/lint.py
pylint/test/functional/syntax_error.txt
pylint/test/functional/unknown_encoding_py29.txt
# HG changeset patch
# User Claudiu Popa <pcmanticore@gmail.com>
# Date 1449415089 -7200
# Sun Dec 06 17:18:09 2015 +0200
# Node ID ffe24a7d5a373f348c423dfd77f392d785605f16
# Parent 84cb406fd1a0a3b571c35e5eeec5df5ee7772186
Make pylint work with new astroid exceptions, AstroidImportError and AstroidSyntaxError.

diff --git a/pylint/checkers/imports.py b/pylint/checkers/imports.py
@@ -296,11 +296,11 @@
1          if len(names) >= 2:
2              self.add_message('multiple-imports', args=', '.join(names), node=node)
3 
4          for name in names:
5              self._check_deprecated_module(node, name)
6 -            importedmodnode = self.get_imported_module(node, name)
7 +            importedmodnode = self._get_imported_module(node, name)
8              if isinstance(node.scope(), astroid.Module):
9                  self._check_position(node)
10                  self._record_import(node, importedmodnode)
11 
12              if importedmodnode is None:
@@ -318,11 +318,11 @@
13          self._check_wildcard_imports(node)
14          self._check_same_line_imports(node)
15          self._check_reimport(node, basename=basename, level=node.level)
16 
17          modnode = node.root()
18 -        importedmodnode = self.get_imported_module(node, basename)
19 +        importedmodnode = self._get_imported_module(node, basename)
20          if isinstance(node.scope(), astroid.Module):
21              self._check_position(node)
22              self._record_import(node, importedmodnode)
23          if importedmodnode is None:
24              return
@@ -456,26 +456,24 @@
25                  self.add_message('wrong-import-order', node=node,
26                                   args=('external import "%s"' % node.as_string(),
27                                         '"%s"' % local_imports[0][0].as_string()))
28          return std_imports, extern_imports, local_imports
29 
30 -    def get_imported_module(self, importnode, modname):
31 +    def _get_imported_module(self, importnode, modname):
32          try:
33              return importnode.do_import_module(modname)
34 -        except astroid.InferenceError as ex:
35 -            dotted_modname = _get_import_name(importnode, modname)
36 -            if str(ex) != modname:
37 -                args = '%r (%s)' % (dotted_modname, ex)
38 -            else:
39 -                args = repr(dotted_modname)
40 -
41 +        except astroid.AstroidBuildingException as exc:
42              for submodule in _qualified_names(modname):
43                  if submodule in self._ignored_modules:
44                      return None
45 
46 -            if not node_ignores_exception(importnode, ImportError):
47 -                self.add_message("import-error", args=args, node=importnode)
48 +            if node_ignores_exception(importnode, ImportError):
49 +                return None
50 +
51 +            dotted_modname = _get_import_name(importnode, modname)
52 +            self.add_message("import-error", args=repr(dotted_modname),
53 +                             node=importnode)
54 
55      def _check_relative_import(self, modnode, importnode, importedmodnode,
56                                 importedasname):
57          """check relative import. node is either an Import or From node, modname
58          the imported module name.
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
@@ -42,11 +42,11 @@
59 
60  def _is_from_future_import(stmt, name):
61      """Check if the name is a future import from another module."""
62      try:
63          module = stmt.do_import_module(stmt.modname)
64 -    except astroid.InferenceError:
65 +    except astroid.AstroidBuildingException:
66          return
67 
68      for local_node in module.locals.get(name, []):
69          if (isinstance(local_node, astroid.ImportFrom)
70                  and local_node.modname == FUTURE):
@@ -1012,11 +1012,11 @@
71              return
72 
73          name_parts = node.modname.split('.')
74          try:
75              module = node.do_import_module(name_parts[0])
76 -        except Exception:
77 +        except astroid.AstroidBuildingException:
78              return
79          module = self._check_module_attrs(node, module, name_parts[1:])
80          if not module:
81              return
82          for name, _ in node.names:
diff --git a/pylint/lint.py b/pylint/lint.py
@@ -901,18 +901,16 @@
83 
84      def get_ast(self, filepath, modname):
85          """return a ast(roid) representation for a module"""
86          try:
87              return MANAGER.ast_from_file(filepath, modname, source=True)
88 +        except astroid.AstroidSyntaxError as ex:
89 +            self.add_message('syntax-error',
90 +                             line=getattr(ex.error, 'lineno', 0),
91 +                             args=str(ex.error))
92          except astroid.AstroidBuildingException as ex:
93 -            if isinstance(ex.args[0], SyntaxError):
94 -                ex = ex.args[0]
95 -                self.add_message('syntax-error',
96 -                                 line=ex.lineno or 0,
97 -                                 args=ex.msg)
98 -            else:
99 -                self.add_message('parse-error', args=ex)
100 +            self.add_message('parse-error', args=ex)
101          except Exception as ex: # pylint: disable=broad-except
102              import traceback
103              traceback.print_exc()
104              self.add_message('astroid-error', args=(ex.__class__, ex))
105 
diff --git a/pylint/test/functional/syntax_error.txt b/pylint/test/functional/syntax_error.txt
@@ -1,1 +1,1 @@
106 -syntax-error:1::invalid syntax
107 \ No newline at end of file
108 +syntax-error:1::invalid syntax (<string>, line 1)
109 \ No newline at end of file
diff --git a/pylint/test/functional/unknown_encoding_py29.txt b/pylint/test/functional/unknown_encoding_py29.txt
@@ -1,1 +1,1 @@
110 -syntax-error:1::"unknown encoding: IBO-8859-1"

111 +syntax-error:1::"unknown encoding: IBO-8859-1 (<string>, line 0)"