Skip to content
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
24 changes: 14 additions & 10 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,20 @@ def _looks_like_special_alias(node: nodes.Call) -> bool:
PY37: Callable = _VariadicGenericAlias(collections.abc.Callable, (), special=True)
PY39: Callable = _CallableType(collections.abc.Callable, 2)
"""
return isinstance(node.func, nodes.Name) and (
(
node.func.name == "_TupleType"
and isinstance(node.args[0], nodes.Name)
and node.args[0].name == "tuple"
)
or (
node.func.name == "_CallableType"
and isinstance(node.args[0], nodes.Attribute)
and node.args[0].as_string() == "collections.abc.Callable"
return (
isinstance(node.func, nodes.Name)
and node.args
and (
(
node.func.name == "_TupleType"
and isinstance(node.args[0], nodes.Name)
and node.args[0].name == "tuple"
)
or (
node.func.name == "_CallableType"
and isinstance(node.args[0], nodes.Attribute)
and node.args[0].as_string() == "collections.abc.Callable"
)
)
)

Expand Down
20 changes: 20 additions & 0 deletions tests/brain/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,23 @@ def test_infer_typing_alias_incorrect_number_of_arguments(
inferred = next(node.value.infer())
assert isinstance(inferred, bases.Instance)
assert inferred.name == "_SpecialGenericAlias"


class TestSpecialAlias:
@pytest.mark.parametrize(
"code",
[
"_CallableType()",
"_TupleType()",
],
)
def test_special_alias_no_crash_on_empty_args(self, code: str) -> None:
"""
Regression test for: https://github.com/pylint-dev/astroid/issues/2772

Test that _CallableType() and _TupleType() calls with no arguments
do not cause an IndexError.
"""
# Should not raise IndexError
module = builder.parse(code)
assert isinstance(module, nodes.Module)