Skip to content

Commit

Permalink
Correctly instantiate exception instances when inferring their attrib…
Browse files Browse the repository at this point in the history
…utes with objectmodel

Close pylint-dev/pylint#2776
  • Loading branch information
PCManticore committed Feb 28, 2019
1 parent 13790cc commit 8a3d8b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Release Date: TBA

Close PyCQA/pylint#2770

* Correctly instantiate exception instances when inferring their attributes with objectmodel

Close PyCQA/pylint#2776


What's New in astroid 2.2.0?
============================
Release Date: 2019-02-27
Expand Down
2 changes: 1 addition & 1 deletion astroid/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def special_attributes(self):
instance = objectmodel.BUILTIN_EXCEPTIONS.get(
qname, objectmodel.ExceptionInstanceModel
)
return instance()
return instance()(self)


class DictInstance(bases.Instance):
Expand Down
14 changes: 14 additions & 0 deletions astroid/tests/unittest_object_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,20 @@ def test_import_error(self):
assert isinstance(inferred, astroid.Const)
assert inferred.value == ""

def test_exception_instance_correctly_instantiated(self):
ast_node = builder.extract_node(
"""
try:
raise ImportError("a")
except ImportError as err:
err #@
"""
)
inferred = next(ast_node.infer())
assert isinstance(inferred, astroid.Instance)
cls = next(inferred.igetattr("__class__"))
assert isinstance(cls, astroid.ClassDef)


class DictObjectModelTest(unittest.TestCase):
def test__class__(self):
Expand Down

0 comments on commit 8a3d8b9

Please sign in to comment.