Skip to content

Commit

Permalink
Add typing to Name._infer and AssignName.infer_lhs (#1651)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Mueller <[email protected]>
  • Loading branch information
DanielNoord and cdce8p authored Jun 22, 2022
1 parent 7883d0b commit db6509b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 13 additions & 5 deletions astroid/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +63,10 @@ def _is_const(value):
],
Any,
]
InferLHS = Callable[
[_NodesT, Optional[InferenceContext]],
typing.Generator[InferenceResult, None, None],
]


@decorators.raise_if_nothing_inferred
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit db6509b

Please sign in to comment.