Skip to content

Commit f25ef82

Browse files
Fix issue parsing _CallableType & _TupleType with no args (#2844)
Signed-off-by: Emmanuel Ferdman <[email protected]>
1 parent a14cc3c commit f25ef82

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

astroid/brain/brain_typing.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,16 +345,20 @@ def _looks_like_special_alias(node: nodes.Call) -> bool:
345345
PY37: Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
346346
PY39: Callable = _CallableType(collections.abc.Callable, 2)
347347
"""
348-
return isinstance(node.func, nodes.Name) and (
349-
(
350-
node.func.name == "_TupleType"
351-
and isinstance(node.args[0], nodes.Name)
352-
and node.args[0].name == "tuple"
353-
)
354-
or (
355-
node.func.name == "_CallableType"
356-
and isinstance(node.args[0], nodes.Attribute)
357-
and node.args[0].as_string() == "collections.abc.Callable"
348+
return (
349+
isinstance(node.func, nodes.Name)
350+
and node.args
351+
and (
352+
(
353+
node.func.name == "_TupleType"
354+
and isinstance(node.args[0], nodes.Name)
355+
and node.args[0].name == "tuple"
356+
)
357+
or (
358+
node.func.name == "_CallableType"
359+
and isinstance(node.args[0], nodes.Attribute)
360+
and node.args[0].as_string() == "collections.abc.Callable"
361+
)
358362
)
359363
)
360364

tests/brain/test_typing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,23 @@ def test_infer_typing_alias_incorrect_number_of_arguments(
7070
inferred = next(node.value.infer())
7171
assert isinstance(inferred, bases.Instance)
7272
assert inferred.name == "_SpecialGenericAlias"
73+
74+
75+
class TestSpecialAlias:
76+
@pytest.mark.parametrize(
77+
"code",
78+
[
79+
"_CallableType()",
80+
"_TupleType()",
81+
],
82+
)
83+
def test_special_alias_no_crash_on_empty_args(self, code: str) -> None:
84+
"""
85+
Regression test for: https://github.com/pylint-dev/astroid/issues/2772
86+
87+
Test that _CallableType() and _TupleType() calls with no arguments
88+
do not cause an IndexError.
89+
"""
90+
# Should not raise IndexError
91+
module = builder.parse(code)
92+
assert isinstance(module, nodes.Module)

0 commit comments

Comments
 (0)