Skip to content

Commit

Permalink
Fix overzealous inference of "type" in subscript contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
nelfin committed Apr 22, 2021
1 parent d09c210 commit 22a4200
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 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, extract_node, inference_tip, nodes, UseInferenceDefault

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

Expand Down Expand Up @@ -47,6 +47,8 @@ def infer_type_sub(node, context=None):
:return: the inferred node
:rtype: nodes.NodeNG
"""
if "type" in node.scope().locals:
raise UseInferenceDefault()
class_src = """
class type:
def __class_getitem__(cls, key):
Expand Down
13 changes: 13 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -4012,6 +4012,19 @@ def func(type):
assert not isinstance(inferred, nodes.ClassDef) # was inferred as builtins.type
assert inferred is util.Uninferable

@pytest.mark.skipif(sys.version_info < (3, 8), reason="only fails on Python 3.9")
def test_infer_arg_called_type_when_used_as_subscript_is_uninferable(self):
# Only this case seems to fail
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


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

0 comments on commit 22a4200

Please sign in to comment.