summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-06-24 09:03:53 +0200
committerGitHub <noreply@github.com>2022-06-24 09:03:53 +0200
commit49bf08129bdcb84ef8e9c9225a7d3464010a733c (patch)
treed3dcebbf37ffd5ce67892e165ad1ae4e140a06f4
parent39305fde76d904a1447013a8a9cd4cd3734ae785 (diff)
downloadastroid-git-49bf08129bdcb84ef8e9c9225a7d3464010a733c.tar.gz
Type ``_infer`` of ``Call``, ``Import`` and ``ImportFrom`` (#1653)
-rw-r--r--astroid/context.py15
-rw-r--r--astroid/inference.py26
2 files changed, 25 insertions, 16 deletions
diff --git a/astroid/context.py b/astroid/context.py
index 12ef3e06..36a900bf 100644
--- a/astroid/context.py
+++ b/astroid/context.py
@@ -55,22 +55,15 @@ class InferenceContext:
Currently this key is ``(node, context.lookupname)``
"""
- self.lookupname = None
- """
- :type: optional[str]
-
- The original name of the node
+ self.lookupname: str | None = None
+ """The original name of the node.
e.g.
foo = 1
The inference of 'foo' is nodes.Const(1) but the lookup name is 'foo'
"""
- self.callcontext = None
- """
- :type: optional[CallContext]
-
- The call arguments and keywords for the given context
- """
+ self.callcontext: CallContext | None = None
+ """The call arguments and keywords for the given context."""
self.boundnode = None
"""
:type: optional[NodeNG]
diff --git a/astroid/inference.py b/astroid/inference.py
index feb58a24..4d184b21 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -241,7 +241,9 @@ nodes.AssignName.infer_lhs = infer_name # won't work with a path wrapper
@decorators.raise_if_nothing_inferred
@decorators.path_wrapper
-def infer_call(self, context=None):
+def infer_call(
+ self: nodes.Call, context: InferenceContext | None = None, **kwargs: Any
+) -> Generator[InferenceResult, None, InferenceErrorInfo]:
"""infer a Call node by trying to guess what the function returns"""
callcontext = copy_context(context)
callcontext.boundnode = None
@@ -260,7 +262,7 @@ def infer_call(self, context=None):
yield from callee.infer_call_result(caller=self, context=callcontext)
except InferenceError:
continue
- return dict(node=self, context=context)
+ return InferenceErrorInfo(node=self, context=context)
nodes.Call._infer = infer_call # type: ignore[assignment]
@@ -268,8 +270,15 @@ nodes.Call._infer = infer_call # type: ignore[assignment]
@decorators.raise_if_nothing_inferred
@decorators.path_wrapper
-def infer_import(self, context=None, asname=True):
+def infer_import(
+ self: nodes.Import,
+ context: InferenceContext | None = None,
+ asname: bool = True,
+ **kwargs: Any,
+) -> Generator[nodes.Module, None, None]:
"""infer an Import node: return the imported module/object"""
+ if not context:
+ raise InferenceError(node=self, context=context)
name = context.lookupname
if name is None:
raise InferenceError(node=self, context=context)
@@ -283,13 +292,20 @@ def infer_import(self, context=None, asname=True):
raise InferenceError(node=self, context=context) from exc
-nodes.Import._infer = infer_import
+nodes.Import._infer = infer_import # type: ignore[assignment]
@decorators.raise_if_nothing_inferred
@decorators.path_wrapper
-def infer_import_from(self, context=None, asname=True):
+def infer_import_from(
+ self: nodes.ImportFrom,
+ context: InferenceContext | None = None,
+ asname: bool = True,
+ **kwargs: Any,
+) -> Generator[InferenceResult, None, None]:
"""infer a ImportFrom node: return the imported module/object"""
+ if not context:
+ raise InferenceError(node=self, context=context)
name = context.lookupname
if name is None:
raise InferenceError(node=self, context=context)