From 3b6ce3d9844041f97244162cc7eb3f07095cdcb0 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 15 Jul 2023 21:18:40 -0400 Subject: [PATCH] Add `TypeAlias.assigned_stmts()` This partially reverts commit a82384a61df2ea026f527d511e29d0ed9c4fb794. --- astroid/nodes/node_classes.py | 13 +++++++++++++ astroid/protocols.py | 2 +- tests/test_type_params.py | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index fc09829c94..e7149219b2 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -19,6 +19,7 @@ TYPE_CHECKING, Any, Callable, + ClassVar, Literal, Optional, Union, @@ -4058,6 +4059,18 @@ def _infer( ) -> Iterator[TypeAlias]: yield self + assigned_stmts: ClassVar[ + Callable[ + [ + TypeAlias, + AssignName, + InferenceContext | None, + None, + ], + Generator[NodeNG, None, None], + ] + ] = protocols.assign_assigned_stmts + class TypeVar(_base_nodes.AssignTypeNode): """Class representing a :class:`ast.TypeVar` node. diff --git a/astroid/protocols.py b/astroid/protocols.py index 7d8791b01c..b5bd0d5cc0 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -441,7 +441,7 @@ def arguments_assigned_stmts( @decorators.raise_if_nothing_inferred def assign_assigned_stmts( - self: nodes.AugAssign | nodes.Assign | nodes.AnnAssign, + self: nodes.AugAssign | nodes.Assign | nodes.AnnAssign | nodes.TypeAlias, node: node_classes.AssignedStmtsPossibleNode = None, context: InferenceContext | None = None, assign_path: list[int] | None = None, diff --git a/tests/test_type_params.py b/tests/test_type_params.py index 6ce5e6877a..afc38b14bc 100644 --- a/tests/test_type_params.py +++ b/tests/test_type_params.py @@ -37,6 +37,9 @@ def test_type_alias() -> None: assert node.statement() is node + assigned = next(node.assigned_stmts()) + assert assigned is node.value + def test_type_param_spec() -> None: node = extract_node("type Alias[**P] = Callable[P, int]")