From db6509bf61f85710c986280826a3a5d1acc0c750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Wed, 22 Jun 2022 11:56:14 +0200 Subject: [PATCH] Add typing to Name._infer and AssignName.infer_lhs (#1651) Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> --- astroid/inference.py | 18 +++++++++++++----- astroid/nodes/node_classes.py | 10 ++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 6ae5c923ed..feb58a2444 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -33,7 +33,11 @@ ) from astroid.interpreter import dunder_lookup from astroid.manager import AstroidManager -from astroid.typing import InferenceErrorInfo, SuccessfulInferenceResult +from astroid.typing import ( + InferenceErrorInfo, + InferenceResult, + SuccessfulInferenceResult, +) if TYPE_CHECKING: from astroid.objects import Property @@ -185,7 +189,7 @@ def _infer_map( nodes.Dict._infer = infer_map # type: ignore[assignment] -def _higher_function_scope(node): +def _higher_function_scope(node: nodes.NodeNG) -> nodes.FunctionDef | None: """Search for the first function which encloses the given scope. This can be used for looking up in that function's scope, in case looking up in a lower scope for a particular @@ -201,11 +205,15 @@ def _higher_function_scope(node): while current.parent and not isinstance(current.parent, nodes.FunctionDef): current = current.parent if current and current.parent: - return current.parent + return current.parent # type: ignore[return-value] return None -def infer_name(self, context=None): +def infer_name( + self: nodes.Name | nodes.AssignName, + context: InferenceContext | None = None, + **kwargs: Any, +) -> Generator[InferenceResult, None, None]: """infer a Name: use name lookup rules""" frame, stmts = self.lookup(self.name) if not stmts: @@ -225,7 +233,7 @@ def infer_name(self, context=None): # pylint: disable=no-value-for-parameter -nodes.Name._infer = decorators.raise_if_nothing_inferred( +nodes.Name._infer = decorators.raise_if_nothing_inferred( # type: ignore[assignment] decorators.path_wrapper(infer_name) ) nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index 13e6ab6423..4d27e1e59e 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -30,7 +30,7 @@ from astroid.nodes import _base_nodes from astroid.nodes.const import OP_PRECEDENCE from astroid.nodes.node_ng import NodeNG -from astroid.typing import SuccessfulInferenceResult +from astroid.typing import InferenceResult, SuccessfulInferenceResult if sys.version_info >= (3, 8): from typing import Literal @@ -63,6 +63,10 @@ def _is_const(value): ], Any, ] +InferLHS = Callable[ + [_NodesT, Optional[InferenceContext]], + typing.Generator[InferenceResult, None, None], +] @decorators.raise_if_nothing_inferred @@ -329,7 +333,7 @@ class LookupMixIn(NodeNG): """Mixin to look up a name in the right scope.""" @lru_cache() # noqa - def lookup(self, name: str) -> tuple[str, list[NodeNG]]: + def lookup(self, name: str) -> tuple[LocalsDictNodeNG, list[NodeNG]]: """Lookup where the given variable is assigned. The lookup starts from self's scope. If self is not a frame itself @@ -380,6 +384,8 @@ class AssignName(_base_nodes.NoChildrenNode, LookupMixIn, _base_nodes.ParentAssi _other_fields = ("name",) + infer_lhs: ClassVar[InferLHS[AssignName]] + @decorators.deprecate_default_argument_values(name="str") def __init__( self,