Skip to content

Commit 041366c

Browse files
committed
Add a registry for builtin exception models. Close pylint-dev/pylint#1432
1 parent 0d4f73d commit 041366c

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

ChangeLog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ What's New in astroid 2.2.0?
77
Release Date: TBA
88

99

10+
* Add a registry for builtin exception models. Close PyCQA/pylint#1432
11+
1012
* Add brain tips for `http.client`. Close PyCQA/pylint#2687
1113

1214
* Prevent crashing when processing ``enums`` with mixed single and double quotes.

astroid/interpreter/objectmodel.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ def py__dict__(self):
601601
return _dunder_dict(self._instance, self._instance.instance_attrs)
602602

603603

604+
# Exception instances
605+
606+
604607
class ExceptionInstanceModel(InstanceModel):
605608
@property
606609
def pyargs(self):
@@ -616,6 +619,15 @@ def py__traceback__(self):
616619
return traceback_type.instantiate_class()
617620

618621

622+
class SyntaxErrorInstanceModel(ExceptionInstanceModel):
623+
@property
624+
def pytext(self):
625+
return node_classes.Const("")
626+
627+
628+
BUILTIN_EXCEPTIONS = {"builtins.SyntaxError": SyntaxErrorInstanceModel}
629+
630+
619631
class DictModel(ObjectModel):
620632
@property
621633
def py__class__(self):

astroid/objects.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,13 @@ class ExceptionInstance(bases.Instance):
210210
the case of .args.
211211
"""
212212

213-
# pylint: disable=unnecessary-lambda
214-
special_attributes = util.lazy_descriptor(
215-
lambda: objectmodel.ExceptionInstanceModel()
216-
)
213+
@decorators.cachedproperty
214+
def special_attributes(self):
215+
qname = self.qname()
216+
instance = objectmodel.BUILTIN_EXCEPTIONS.get(
217+
qname, objectmodel.ExceptionInstanceModel
218+
)
219+
return instance()
217220

218221

219222
class DictInstance(bases.Instance):

astroid/tests/unittest_object_model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,18 @@ def test_model_py3(self):
585585
with self.assertRaises(exceptions.InferenceError):
586586
next(ast_nodes[2].infer())
587587

588+
def test_syntax_error(self):
589+
ast_node = builder.extract_node(
590+
"""
591+
try:
592+
x[42]
593+
except SyntaxError as err:
594+
err.text #@
595+
"""
596+
)
597+
inferred = next(ast_node.infer())
598+
assert isinstance(inferred, astroid.Const)
599+
588600

589601
class DictObjectModelTest(unittest.TestCase):
590602
def test__class__(self):

0 commit comments

Comments
 (0)