summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2021-12-07 17:06:19 -0500
committerGitHub <noreply@github.com>2021-12-07 23:06:19 +0100
commit6ac6b909bfa18883d46cd15981629e84c5bd949e (patch)
tree3362a577de547ab8afde17f7c03f3c8b37c051b0
parente13c3402b08ecdb399a9c611bac61333a431f609 (diff)
downloadastroid-git-6ac6b909bfa18883d46cd15981629e84c5bd949e.tar.gz
Fix crash if a variable named "type" is subscripted in a generator expression (#1285)
Co-authored-by Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r--ChangeLog4
-rw-r--r--astroid/brain/brain_type.py2
-rw-r--r--tests/unittest_inference.py5
3 files changed, 10 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 7d701440..3cf10278 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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: