Skip to content

Move inference methods to nodes module(s) #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Release date: TBA
Closes #1780
Refs #2140

* Remove the ``inference`` module. Node inference methods are now in the module
defining the node, rather than being associated to the node afterward.

Closes #679

* Move ``LookupMixIn`` to ``astroid.nodes._base_nodes`` and make it private.

* Reduce file system access in ``ast_from_file()``.

* Reduce time to ``import astroid`` by delaying ``astroid_bootstrapping()`` until
Expand Down
2 changes: 1 addition & 1 deletion astroid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

# isort: on

from astroid import inference, raw_building
from astroid import raw_building
from astroid.__pkginfo__ import __version__, version
from astroid.astroid_manager import MANAGER
from astroid.bases import BaseInstance, BoundMethod, Instance, UnboundMethod
Expand Down
16 changes: 12 additions & 4 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import collections
import collections.abc
from collections.abc import Iterable, Iterator
from typing import TYPE_CHECKING, Any, ClassVar, Literal
from typing import TYPE_CHECKING, Any, Literal

from astroid import nodes
from astroid import decorators, nodes
from astroid.const import PY310_PLUS
from astroid.context import (
CallContext,
Expand All @@ -28,7 +28,6 @@
)
from astroid.interpreter import objectmodel
from astroid.typing import (
InferBinaryOp,
InferenceErrorInfo,
InferenceResult,
SuccessfulInferenceResult,
Expand Down Expand Up @@ -346,7 +345,16 @@ class Instance(BaseInstance):
def __init__(self, proxied: nodes.ClassDef | None) -> None:
super().__init__(proxied)

infer_binary_op: ClassVar[InferBinaryOp[Instance]]
@decorators.yes_if_nothing_inferred
def infer_binary_op(
self,
opnode: nodes.AugAssign | nodes.BinOp,
operator: str,
other: InferenceResult,
context: InferenceContext,
method: SuccessfulInferenceResult,
) -> Generator[InferenceResult, None, None]:
return method.infer_call_result(self, context)

def __repr__(self) -> str:
return "<Instance of {}.{} at 0x{}>".format(
Expand Down
7 changes: 5 additions & 2 deletions astroid/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import sys
from abc import ABC, abstractmethod
from collections.abc import Iterator
from typing import Union
from typing import TYPE_CHECKING, Union

from astroid import bases, nodes, util
from astroid import nodes, util
from astroid.typing import InferenceResult

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self

if TYPE_CHECKING:
from astroid import bases

_NameNodes = Union[nodes.AssignAttr, nodes.Attribute, nodes.AssignName, nodes.Name]


Expand Down
8 changes: 6 additions & 2 deletions astroid/filter_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from astroid import nodes
from astroid.nodes import node_classes
from astroid.typing import SuccessfulInferenceResult

if TYPE_CHECKING:
from astroid.nodes import _base_nodes


def _get_filtered_node_statements(
base_node: nodes.NodeNG, stmt_nodes: list[nodes.NodeNG]
Expand Down Expand Up @@ -44,7 +48,7 @@ def _get_if_statement_ancestor(node: nodes.NodeNG) -> nodes.If | None:


def _filter_stmts(
base_node: node_classes.LookupMixIn,
base_node: _base_nodes.LookupMixIn,
stmts: list[SuccessfulInferenceResult],
frame: nodes.LocalsDictNodeNG,
offset: int,
Expand Down
22 changes: 22 additions & 0 deletions astroid/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,25 @@ def object_len(node, context: InferenceContext | None = None):
raise AstroidTypeError(
f"'{result_of_len}' object cannot be interpreted as an integer"
)


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
name fails.

:param node: A scope node.
:returns:
``None``, if no parent function scope was found,
otherwise an instance of :class:`astroid.nodes.scoped_nodes.Function`,
which encloses the given node.
"""
current = node
while current.parent and not isinstance(current.parent, nodes.FunctionDef):
current = current.parent
if current and current.parent:
return current.parent
return None
Loading