From 76968f3a9078278e742011f25b6bef33b2895f30 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:35:35 -0400 Subject: [PATCH 1/3] Fix IndexError when typing._alias() has missing arguments Closes #2513 (cherry picked from commit ca0230d876914290712b4478acc863ae974f3b2d) --- ChangeLog | 4 +++ astroid/brain/brain_typing.py | 1 + tests/brain/test_typing.py | 49 ++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 4d3de29097..d7f48a8d5d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,10 @@ What's New in astroid 3.4.0? Release date: TBA +* Fix crash when typing._alias() call is missing arguments. + + Closes #2513 + What's New in astroid 3.3.6? ============================ diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index 38b01778b1..5e3a0c90d6 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -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)) diff --git a/tests/brain/test_typing.py b/tests/brain/test_typing.py index 1e8e3eaf88..11b77b7f9e 100644 --- a/tests/brain/test_typing.py +++ b/tests/brain/test_typing.py @@ -4,7 +4,7 @@ import pytest -from astroid import builder +from astroid import bases, builder, nodes from astroid.exceptions import InferenceError @@ -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" From 78507c49d8cc7729dbfa3dc095330bea2806651d Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 23 Oct 2024 20:42:03 +0200 Subject: [PATCH 2/3] Update ChangeLog --- ChangeLog | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index d7f48a8d5d..3e1546d0df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,13 +8,13 @@ What's New in astroid 3.4.0? Release date: TBA -* Fix crash when typing._alias() call is missing arguments. - - Closes #2513 - What's New in astroid 3.3.6? ============================ + +* Fix crash when typing._alias() call is missing arguments. + + Closes #2513 Release date: TBA From 7bc02b7e04d85033903073a69e548b383bdc702a Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Wed, 23 Oct 2024 20:42:34 +0200 Subject: [PATCH 3/3] Update ChangeLog --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 3e1546d0df..ee3824cf09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,11 +11,11 @@ Release date: TBA What's New in astroid 3.3.6? ============================ +Release date: TBA * Fix crash when typing._alias() call is missing arguments. Closes #2513 -Release date: TBA