Skip to content

Commit 8d5cefa

Browse files
Permit subscripting additional builtin classes (#1572)
Treat other classes such as enumerate and staticmethod identically to list, dict, tuple, set, frozenset.
1 parent 5067f08 commit 8d5cefa

File tree

4 files changed

+7
-6
lines changed

4 files changed

+7
-6
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Release date: TBA
2626

2727
Closes #1559
2828

29+
* On Python versions >= 3.9, ``astroid`` now understands subscripting
30+
builtin classes such as ``enumerate`` or ``staticmethod``.
31+
2932
* Rename ``ModuleSpec`` -> ``module_type`` constructor parameter to match attribute
3033
name and improve typing. Use ``type`` instead.
3134

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ def getitem(self, index, context=None):
27632763
# AttributeError
27642764
if (
27652765
isinstance(method, node_classes.EmptyNode)
2766-
and self.name in {"list", "dict", "set", "tuple", "frozenset"}
2766+
and self.pytype() == "builtins.type"
27672767
and PY39_PLUS
27682768
):
27692769
return self

tests/unittest_brain.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,10 +1271,9 @@ def test_invalid_type_subscript(self):
12711271

12721272
@test_utils.require_version(minver="3.9")
12731273
def test_builtin_subscriptable(self):
1274-
"""
1275-
Starting with python3.9 builtin type such as list are subscriptable
1276-
"""
1277-
for typename in ("tuple", "list", "dict", "set", "frozenset"):
1274+
"""Starting with python3.9 builtin types such as list are subscriptable.
1275+
Any builtin class such as "enumerate" or "staticmethod" also works."""
1276+
for typename in ("tuple", "list", "dict", "set", "frozenset", "enumerate"):
12781277
src = f"""
12791278
{typename:s}[int]
12801279
"""

tests/unittest_inference.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3574,7 +3574,6 @@ def test_invalid_slicing_primaries(self) -> None:
35743574
"(1, 2, 3)[a:]",
35753575
"(1, 2, 3)[object:object]",
35763576
"(1, 2, 3)[1:object]",
3577-
"enumerate[2]",
35783577
]
35793578
for code in examples:
35803579
node = extract_node(code)

0 commit comments

Comments
 (0)