Skip to content

Commit 80ce031

Browse files
github-actions[bot]correctmostPierre-Sassoulas
authored
[Backport maintenance/3.3.x] Fix IndexError when typing._alias() has missing arguments (#2624)
* Fix IndexError when typing._alias() has missing arguments Closes #2513 (cherry picked from commit ca0230d) Co-authored-by: correctmost <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 8c74a5f commit 80ce031

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Diff for: ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ What's New in astroid 3.3.6?
1313
============================
1414
Release date: TBA
1515

16+
* Fix crash when typing._alias() call is missing arguments.
17+
18+
Closes #2513
19+
1620

1721

1822
What's New in astroid 3.3.5?

Diff for: astroid/brain/brain_typing.py

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def _looks_like_typing_alias(node: Call) -> bool:
257257
isinstance(node.func, Name)
258258
# TODO: remove _DeprecatedGenericAlias when Py3.14 min
259259
and node.func.name in {"_alias", "_DeprecatedGenericAlias"}
260+
and len(node.args) == 2
260261
and (
261262
# _alias function works also for builtins object such as list and dict
262263
isinstance(node.args[0], (Attribute, Name))

Diff for: tests/brain/test_typing.py

+48-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66

7-
from astroid import builder
7+
from astroid import bases, builder, nodes
88
from astroid.exceptions import InferenceError
99

1010

@@ -23,3 +23,50 @@ def test_infer_typevar() -> None:
2323
)
2424
with pytest.raises(InferenceError):
2525
call_node.inferred()
26+
27+
28+
class TestTypingAlias:
29+
def test_infer_typing_alias(self) -> None:
30+
"""
31+
Test that _alias() calls can be inferred.
32+
"""
33+
node = builder.extract_node(
34+
"""
35+
from typing import _alias
36+
x = _alias(int, float)
37+
"""
38+
)
39+
assert isinstance(node, nodes.Assign)
40+
assert isinstance(node.value, nodes.Call)
41+
inferred = next(node.value.infer())
42+
assert isinstance(inferred, nodes.ClassDef)
43+
assert len(inferred.bases) == 1
44+
assert inferred.bases[0].name == "int"
45+
46+
@pytest.mark.parametrize(
47+
"alias_args",
48+
[
49+
"", # two missing arguments
50+
"int", # one missing argument
51+
"int, float, tuple", # one additional argument
52+
],
53+
)
54+
def test_infer_typing_alias_incorrect_number_of_arguments(
55+
self, alias_args: str
56+
) -> None:
57+
"""
58+
Regression test for: https://github.com/pylint-dev/astroid/issues/2513
59+
60+
Test that _alias() calls with the incorrect number of arguments can be inferred.
61+
"""
62+
node = builder.extract_node(
63+
f"""
64+
from typing import _alias
65+
x = _alias({alias_args})
66+
"""
67+
)
68+
assert isinstance(node, nodes.Assign)
69+
assert isinstance(node.value, nodes.Call)
70+
inferred = next(node.value.infer())
71+
assert isinstance(inferred, bases.Instance)
72+
assert inferred.name == "_SpecialGenericAlias"

0 commit comments

Comments
 (0)