Skip to content

Commit

Permalink
Fix a strange construct that seem unnecessary
Browse files Browse the repository at this point in the history
And probably contribute to the circular import problem.
  • Loading branch information
Pierre-Sassoulas committed May 1, 2021
1 parent 0528581 commit b09100b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
14 changes: 12 additions & 2 deletions astroid/bases.py
Expand Up @@ -316,9 +316,19 @@ def bool_value(self, context=None):
return True
return result

# This is set in inference.py.
def getitem(self, index, context=None):
pass
# TODO: Rewrap index to Const for this case
new_context = contextmod.bind_context_to_node(context, self)
if not context:
context = new_context
# Create a new CallContext for providing index as an argument.
new_context.callcontext = contextmod.CallContext(args=[index])
method = next(self.igetattr("__getitem__", context=context), None)
if not isinstance(method, BoundMethod):
raise exceptions.InferenceError(
"Could not find __getitem__ for {node!r}.", node=self, context=context
)
return next(method.infer_call_result(self, new_context))


class UnboundMethod(Proxy):
Expand Down
6 changes: 3 additions & 3 deletions astroid/builder.py
Expand Up @@ -18,10 +18,10 @@
The builder is not thread safe and can't be used to parse different sources
at the same time.
"""

import os
import textwrap
from tokenize import detect_encoding
from typing import List, Union

from astroid import (
bases,
Expand All @@ -34,6 +34,7 @@
util,
)
from astroid._ast import get_parser_module
from astroid.node_classes import NodeNG

objects = util.lazy_import("objects")

Expand Down Expand Up @@ -357,7 +358,7 @@ def _find_statement_by_line(node, line):
return None


def extract_node(code, module_name=""):
def extract_node(code: str, module_name: str = "") -> Union[NodeNG, List[NodeNG]]:
"""Parses some Python code as a module and extracts a designated AST node.
Statements:
Expand Down Expand Up @@ -409,7 +410,6 @@ def extract_node(code, module_name=""):
a module. Will be passed through textwrap.dedent first.
:param str module_name: The name of the module.
:returns: The designated node from the parse tree, or a list of nodes.
:rtype: astroid.bases.NodeNG, or a list of nodes.
"""

def _extract(node):
Expand Down
22 changes: 0 additions & 22 deletions astroid/inference.py
Expand Up @@ -872,28 +872,6 @@ def infer_index(self, context=None):

nodes.Index._infer = infer_index

# TODO: move directly into bases.Instance when the dependency hell
# will be solved.
def instance_getitem(self, index, context=None):
# Rewrap index to Const for this case
new_context = contextmod.bind_context_to_node(context, self)
if not context:
context = new_context

# Create a new callcontext for providing index as an argument.
new_context.callcontext = contextmod.CallContext(args=[index])

method = next(self.igetattr("__getitem__", context=context), None)
if not isinstance(method, bases.BoundMethod):
raise exceptions.InferenceError(
"Could not find __getitem__ for {node!r}.", node=self, context=context
)

return next(method.infer_call_result(self, new_context))


bases.Instance.getitem = instance_getitem


def _populate_context_lookup(call, context):
# Allows context to be saved for later
Expand Down

0 comments on commit b09100b

Please sign in to comment.