Skip to content

Commit

Permalink
Wrap AttributeInferenceErrors in AstroidTypeErrors for ClassDef.getitem.
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed Apr 17, 2017
1 parent 5092504 commit b6cf7ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion astroid/scoped_nodes.py
Expand Up @@ -1520,7 +1520,15 @@ def getitem(self, index, context=None):
This is basically looking up the method in the metaclass and calling it.
"""
methods = dunder_lookup.lookup(self, '__getitem__')
try:
methods = dunder_lookup.lookup(self, '__getitem__')
except exceptions.AttributeInferenceError as exc:
util.reraise(
exceptions.AstroidTypeError(
node=self, error=exc,
context=context
)
)

method = methods[0]

Expand Down
12 changes: 12 additions & 0 deletions astroid/tests/unittest_inference.py
Expand Up @@ -3470,6 +3470,18 @@ def blurb(self):
self.assertIsInstance(inferred, nodes.Const)
self.assertEqual(inferred.value, 25)

def test_getitem_of_class_raised_type_error(self):
# Test that we wrap an AttributeInferenceError
# and reraise it as a TypeError in Class.getitem
node = extract_node('''
def test():
yield
test()
''')
inferred = next(node.infer())
with self.assertRaises(exceptions.AstroidTypeError):
inferred.getitem(nodes.Const('4'))


class GetattrTest(unittest.TestCase):

Expand Down

0 comments on commit b6cf7ab

Please sign in to comment.