Skip to content
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

[Backport maintenance/3.3.x] Fix IndexError when typing._alias() has missing arguments #2624

Open
wants to merge 3 commits into
base: maintenance/3.3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Release date: TBA

What's New in astroid 3.3.6?
============================

* Fix crash when typing._alias() call is missing arguments.

Closes #2513
Release date: TBA

Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
1 change: 1 addition & 0 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def _looks_like_typing_alias(node: Call) -> bool:
isinstance(node.func, Name)
# TODO: remove _DeprecatedGenericAlias when Py3.14 min
and node.func.name in {"_alias", "_DeprecatedGenericAlias"}
and len(node.args) == 2
and (
# _alias function works also for builtins object such as list and dict
isinstance(node.args[0], (Attribute, Name))
Expand Down
49 changes: 48 additions & 1 deletion tests/brain/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from astroid import builder
from astroid import bases, builder, nodes
from astroid.exceptions import InferenceError


Expand All @@ -23,3 +23,50 @@ def test_infer_typevar() -> None:
)
with pytest.raises(InferenceError):
call_node.inferred()


class TestTypingAlias:
def test_infer_typing_alias(self) -> None:
"""
Test that _alias() calls can be inferred.
"""
node = builder.extract_node(
"""
from typing import _alias
x = _alias(int, float)
"""
)
assert isinstance(node, nodes.Assign)
assert isinstance(node.value, nodes.Call)
inferred = next(node.value.infer())
assert isinstance(inferred, nodes.ClassDef)
assert len(inferred.bases) == 1
assert inferred.bases[0].name == "int"

@pytest.mark.parametrize(
"alias_args",
[
"", # two missing arguments
"int", # one missing argument
"int, float, tuple", # one additional argument
],
)
def test_infer_typing_alias_incorrect_number_of_arguments(
self, alias_args: str
) -> None:
"""
Regression test for: https://github.com/pylint-dev/astroid/issues/2513

Test that _alias() calls with the incorrect number of arguments can be inferred.
"""
node = builder.extract_node(
f"""
from typing import _alias
x = _alias({alias_args})
"""
)
assert isinstance(node, nodes.Assign)
assert isinstance(node.value, nodes.Call)
inferred = next(node.value.infer())
assert isinstance(inferred, bases.Instance)
assert inferred.name == "_SpecialGenericAlias"
Loading