Skip to content

Commit 6ac6b90

Browse files
Fix crash if a variable named "type" is subscripted in a generator expression (#1285)
Co-authored-by Marc Mueller <[email protected]>
1 parent e13c340 commit 6ac6b90

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Release date: TBA
2020

2121
* Fix typing and update explanation for ``Arguments.args`` being ``None``.
2222

23+
* Fix crash if a variable named ``type`` is subscripted in a generator expression.
24+
25+
Closes PyCQA/pylint#5461
26+
2327

2428
What's New in astroid 2.9.0?
2529
============================

astroid/brain/brain_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def infer_type_sub(node, context=None):
4848
:rtype: nodes.NodeNG
4949
"""
5050
node_scope, _ = node.scope().lookup("type")
51-
if node_scope.qname() != "builtins":
51+
if not isinstance(node_scope, nodes.Module) or node_scope.qname() != "builtins":
5252
raise UseInferenceDefault()
5353
class_src = """
5454
class type:

tests/unittest_inference.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4200,6 +4200,11 @@ class Test(Parent):
42004200
assert isinstance(inferred, nodes.Const)
42014201
assert inferred.value == 123
42024202

4203+
def test_uninferable_type_subscript(self) -> None:
4204+
node = extract_node("[type for type in [] if type['id']]")
4205+
with self.assertRaises(InferenceError):
4206+
_ = next(node.infer())
4207+
42034208

42044209
class GetattrTest(unittest.TestCase):
42054210
def test_yes_when_unknown(self) -> None:

0 commit comments

Comments
 (0)