Skip to content

Commit

Permalink
Add a registry for builtin exception models. Close pylint-dev/pylint#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed Jan 17, 2019
1 parent 0d4f73d commit 041366c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ What's New in astroid 2.2.0?
Release Date: TBA


* Add a registry for builtin exception models. Close PyCQA/pylint#1432

* Add brain tips for `http.client`. Close PyCQA/pylint#2687

* Prevent crashing when processing ``enums`` with mixed single and double quotes.
Expand Down
12 changes: 12 additions & 0 deletions astroid/interpreter/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,9 @@ def py__dict__(self):
return _dunder_dict(self._instance, self._instance.instance_attrs)


# Exception instances


class ExceptionInstanceModel(InstanceModel):
@property
def pyargs(self):
Expand All @@ -616,6 +619,15 @@ def py__traceback__(self):
return traceback_type.instantiate_class()


class SyntaxErrorInstanceModel(ExceptionInstanceModel):
@property
def pytext(self):
return node_classes.Const("")


BUILTIN_EXCEPTIONS = {"builtins.SyntaxError": SyntaxErrorInstanceModel}


class DictModel(ObjectModel):
@property
def py__class__(self):
Expand Down
11 changes: 7 additions & 4 deletions astroid/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ class ExceptionInstance(bases.Instance):
the case of .args.
"""

# pylint: disable=unnecessary-lambda
special_attributes = util.lazy_descriptor(
lambda: objectmodel.ExceptionInstanceModel()
)
@decorators.cachedproperty
def special_attributes(self):
qname = self.qname()
instance = objectmodel.BUILTIN_EXCEPTIONS.get(
qname, objectmodel.ExceptionInstanceModel
)
return instance()


class DictInstance(bases.Instance):
Expand Down
12 changes: 12 additions & 0 deletions astroid/tests/unittest_object_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ def test_model_py3(self):
with self.assertRaises(exceptions.InferenceError):
next(ast_nodes[2].infer())

def test_syntax_error(self):
ast_node = builder.extract_node(
"""
try:
x[42]
except SyntaxError as err:
err.text #@
"""
)
inferred = next(ast_node.infer())
assert isinstance(inferred, astroid.Const)


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

0 comments on commit 041366c

Please sign in to comment.