diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | astroid/brain/brain_type.py | 2 | ||||
-rw-r--r-- | tests/unittest_inference.py | 5 |
3 files changed, 10 insertions, 1 deletions
@@ -20,6 +20,10 @@ Release date: TBA * Fix typing and update explanation for ``Arguments.args`` being ``None``. +* Fix crash if a variable named ``type`` is subscripted in a generator expression. + + Closes PyCQA/pylint#5461 + What's New in astroid 2.9.0? ============================ diff --git a/astroid/brain/brain_type.py b/astroid/brain/brain_type.py index d0eddd22..9d694e62 100644 --- a/astroid/brain/brain_type.py +++ b/astroid/brain/brain_type.py @@ -48,7 +48,7 @@ def infer_type_sub(node, context=None): :rtype: nodes.NodeNG """ node_scope, _ = node.scope().lookup("type") - if node_scope.qname() != "builtins": + if not isinstance(node_scope, nodes.Module) or node_scope.qname() != "builtins": raise UseInferenceDefault() class_src = """ class type: diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index b18f0925..e660f7cb 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -4200,6 +4200,11 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): assert isinstance(inferred, nodes.Const) assert inferred.value == 123 + def test_uninferable_type_subscript(self) -> None: + node = extract_node("[type for type in [] if type['id']]") + with self.assertRaises(InferenceError): + _ = next(node.infer()) + class GetattrTest(unittest.TestCase): def test_yes_when_unknown(self) -> None: |