Skip to content

Commit a0ff581

Browse files
committed
Fix overzealous inference of "type" in subscript contexts
Ref pylint-dev/pylint#4083. Ref pylint-dev/pylint#4387. When used in a nodes.Subscript, an existing inference_tip was set for Name nodes matching called type. However, when this name was redefined this led to false-positive typecheck errors in pylint such as invalid-sequence-index due to the argument being inferred as builtins.type and inference_tip preventing any further inference.
1 parent b80c3ec commit a0ff581

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

astroid/brain/brain_type.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"""
1818
import sys
1919

20-
from astroid import MANAGER, extract_node, inference_tip, nodes
20+
from astroid import MANAGER, UseInferenceDefault, extract_node, inference_tip, nodes
2121

2222
PY39 = sys.version_info >= (3, 9)
2323

@@ -47,6 +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:
51+
raise UseInferenceDefault()
5052
class_src = """
5153
class type:
5254
def __class_getitem__(cls, key):

tests/unittest_inference.py

+50
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,56 @@ def test():
39863986
with self.assertRaises(exceptions.AstroidTypeError):
39873987
inferred.getitem(nodes.Const("4"))
39883988

3989+
def test_infer_arg_called_type_is_uninferable(self):
3990+
node = extract_node(
3991+
"""
3992+
def func(type):
3993+
type #@
3994+
"""
3995+
)
3996+
inferred = next(node.infer())
3997+
assert inferred is util.Uninferable
3998+
3999+
def test_infer_arg_called_object_when_used_as_index_is_uninferable(self):
4000+
node = extract_node(
4001+
"""
4002+
def func(object):
4003+
['list'][
4004+
object #@
4005+
]
4006+
"""
4007+
)
4008+
inferred = next(node.infer())
4009+
assert inferred is util.Uninferable
4010+
4011+
@test_utils.require_version(minver="3.9")
4012+
def test_infer_arg_called_type_when_used_as_index_is_uninferable(self):
4013+
# https://github.com/PyCQA/astroid/pull/958
4014+
node = extract_node(
4015+
"""
4016+
def func(type):
4017+
['list'][
4018+
type #@
4019+
]
4020+
"""
4021+
)
4022+
inferred = next(node.infer())
4023+
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
4024+
assert inferred is util.Uninferable
4025+
4026+
@test_utils.require_version(minver="3.9")
4027+
def test_infer_arg_called_type_when_used_as_subscript_is_uninferable(self):
4028+
# https://github.com/PyCQA/astroid/pull/958
4029+
node = extract_node(
4030+
"""
4031+
def func(type):
4032+
type[0] #@
4033+
"""
4034+
)
4035+
inferred = next(node.infer())
4036+
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
4037+
assert inferred is util.Uninferable
4038+
39894039

39904040
class GetattrTest(unittest.TestCase):
39914041
def test_yes_when_unknown(self):

0 commit comments

Comments
 (0)