Skip to content

Fix name type inference when used as in a subscript #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion astroid/brain/brain_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""
import sys

from astroid import MANAGER, extract_node, inference_tip, nodes
from astroid import MANAGER, UseInferenceDefault, extract_node, inference_tip, nodes

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

Expand Down Expand Up @@ -47,6 +47,9 @@ def infer_type_sub(node, context=None):
:return: the inferred node
:rtype: nodes.NodeNG
"""
node_scope, _ = node.scope().lookup("type")
if node_scope.qname() != "builtins":
raise UseInferenceDefault()
class_src = """
class type:
def __class_getitem__(cls, key):
Expand Down
64 changes: 64 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3986,6 +3986,70 @@ def test():
with self.assertRaises(exceptions.AstroidTypeError):
inferred.getitem(nodes.Const("4"))

def test_infer_arg_called_type_is_uninferable(self):
node = extract_node(
"""
def func(type):
type #@
"""
)
inferred = next(node.infer())
assert inferred is util.Uninferable

def test_infer_arg_called_object_when_used_as_index_is_uninferable(self):
node = extract_node(
"""
def func(object):
['list'][
object #@
]
"""
)
inferred = next(node.infer())
assert inferred is util.Uninferable

@test_utils.require_version(minver="3.9")
def test_infer_arg_called_type_when_used_as_index_is_uninferable(self):
# https://github.com/PyCQA/astroid/pull/958
node = extract_node(
"""
def func(type):
['list'][
type #@
]
"""
)
inferred = next(node.infer())
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
assert inferred is util.Uninferable

@test_utils.require_version(minver="3.9")
def test_infer_arg_called_type_when_used_as_subscript_is_uninferable(self):
# https://github.com/PyCQA/astroid/pull/958
node = extract_node(
"""
def func(type):
type[0] #@
"""
)
inferred = next(node.infer())
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
assert inferred is util.Uninferable

@test_utils.require_version(minver="3.9")
def test_infer_arg_called_type_defined_in_outer_scope_is_uninferable(self):
# https://github.com/PyCQA/astroid/pull/958
node = extract_node(
"""
def outer(type):
def inner():
type[0] #@
"""
)
inferred = next(node.infer())
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
assert inferred is util.Uninferable


class GetattrTest(unittest.TestCase):
def test_yes_when_unknown(self):
Expand Down