summaryrefslogtreecommitdiff
path: root/astroid/mixins.py
diff options
context:
space:
mode:
authorCeridwen <ceridwenv@gmail.com>2015-11-02 00:10:54 -0500
committerCeridwen <ceridwenv@gmail.com>2015-11-02 00:10:54 -0500
commit1b3a23614a3648978e8ed6258280f661caf73690 (patch)
treefe3b110b67e85dd1e44e7903fc62f77fbf090fa7 /astroid/mixins.py
parenta6baa5f092e933bc5b84849264df988069cee7d9 (diff)
downloadastroid-git-1b3a23614a3648978e8ed6258280f661caf73690.tar.gz
This bookmark adds structured exceptions to astroid.
Major changes: * AstroidError has an __init__ that accepts arbitrary keyword-only arguments for adding information to exceptions, and a __str__ that lazily uses exception attributes to generate a message. The first positional argument to an exception is assigned to .message. The new API should be fully backwards compatible in general. * Some exceptions are combined or renamed; the old names are still available. * The OperationErrors used by pylint are now BadOperationMessages and located in util.py. * The AstroidBuildingException in _data_build stores the SyntaxError in its .error attribute rather than args[0]. * Many places where exceptions are raised have new, hopefully more useful error messages. The only major issue remaining is how to propagate information into decorators.
Diffstat (limited to 'astroid/mixins.py')
-rw-r--r--astroid/mixins.py20
1 files changed, 15 insertions, 5 deletions
diff --git a/astroid/mixins.py b/astroid/mixins.py
index 9f5f9538..6ee7b4db 100644
--- a/astroid/mixins.py
+++ b/astroid/mixins.py
@@ -129,11 +129,19 @@ class ImportFromMixin(FilterStmtsMixin):
return mymodule.import_module(modname, level=level,
relative_only=level and level >= 1)
except exceptions.AstroidBuildingException as ex:
- if isinstance(ex.args[0], SyntaxError):
- util.reraise(exceptions.InferenceError(str(ex)))
- util.reraise(exceptions.InferenceError(modname))
+ if isinstance(getattr(ex, 'error', None), SyntaxError):
+ util.reraise(exceptions.InferenceError(
+ 'Could not import {modname} because of SyntaxError:\n'
+ '{syntax_error}', modname=modname, syntax_error=ex.error,
+ import_node=self))
+ util.reraise(exceptions.InferenceError('Could not import {modname}.',
+ modname=modname,
+ import_node=self))
except SyntaxError as ex:
- util.reraise(exceptions.InferenceError(str(ex)))
+ util.reraise(exceptions.InferenceError(
+ 'Could not import {modname} because of SyntaxError:\n'
+ '{syntax_error}', modname=modname, syntax_error=ex,
+ import_node=self))
def real_name(self, asname):
"""get name from 'as' name"""
@@ -145,4 +153,6 @@ class ImportFromMixin(FilterStmtsMixin):
_asname = name
if asname == _asname:
return name
- raise exceptions.NotFoundError(asname)
+ raise exceptions.AttributeInferenceError(
+ 'Could not find original name for {attribute} in {target!r}',
+ target=self, attribute=asname)