Skip to content

Commit 2a8ab1a

Browse files
committed
Fix scope lookup of "type" for type inference brain
1 parent a0ff581 commit 2a8ab1a

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
@@ -4036,6 +4036,20 @@ def func(type):
40364036
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
40374037
assert inferred is util.Uninferable
40384038

4039+
@test_utils.require_version(minver="3.9")
4040+
def test_infer_arg_called_type_defined_in_outer_scope_is_uninferable(self):
4041+
# https://github.com/PyCQA/astroid/pull/958
4042+
node = extract_node(
4043+
"""
4044+
def outer(type):
4045+
def inner():
4046+
type[0] #@
4047+
"""
4048+
)
4049+
inferred = next(node.infer())
4050+
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
4051+
assert inferred is util.Uninferable
4052+
40394053

40404054
class GetattrTest(unittest.TestCase):
40414055
def test_yes_when_unknown(self):

0 commit comments

Comments
 (0)