Skip to content

Commit 25904b9

Browse files
committed
Fix scope lookup of "type" for type inference brain
1 parent ffee3a0 commit 25904b9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

astroid/brain/brain_type.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def infer_type_sub(node, context=None):
4747
:return: the inferred node
4848
:rtype: nodes.NodeNG
4949
"""
50-
if "type" in node.scope().locals:
50+
node_scope, _ = node.scope().lookup("type")
51+
if node_scope.qname() != "builtins":
5152
raise UseInferenceDefault()
5253
class_src = """
5354
class type:

tests/unittest_inference.py

+14
Original file line numberDiff line numberDiff line change
@@ -4025,6 +4025,20 @@ def func(type):
40254025
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
40264026
assert inferred is util.Uninferable
40274027

4028+
@pytest.mark.skipif(sys.version_info < (3, 8), reason="only fails on Python 3.9")
4029+
def test_infer_arg_called_type_defined_in_outer_scope_is_uninferable(self):
4030+
# Only this case seems to fail
4031+
node = extract_node(
4032+
"""
4033+
def outer(type):
4034+
def inner():
4035+
type[0] #@
4036+
"""
4037+
)
4038+
inferred = next(node.infer())
4039+
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
4040+
assert inferred is util.Uninferable
4041+
40284042

40294043
class GetattrTest(unittest.TestCase):
40304044
def test_yes_when_unknown(self):

0 commit comments

Comments
 (0)