Skip to content

Commit 49bf081

Browse files
authored
Type _infer of Call, Import and ImportFrom (#1653)
1 parent 39305fd commit 49bf081

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

astroid/context.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,15 @@ def __init__(self, path=None, nodes_inferred=None):
5555
5656
Currently this key is ``(node, context.lookupname)``
5757
"""
58-
self.lookupname = None
59-
"""
60-
:type: optional[str]
61-
62-
The original name of the node
58+
self.lookupname: str | None = None
59+
"""The original name of the node.
6360
6461
e.g.
6562
foo = 1
6663
The inference of 'foo' is nodes.Const(1) but the lookup name is 'foo'
6764
"""
68-
self.callcontext = None
69-
"""
70-
:type: optional[CallContext]
71-
72-
The call arguments and keywords for the given context
73-
"""
65+
self.callcontext: CallContext | None = None
66+
"""The call arguments and keywords for the given context."""
7467
self.boundnode = None
7568
"""
7669
:type: optional[NodeNG]

astroid/inference.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ def infer_name(
241241

242242
@decorators.raise_if_nothing_inferred
243243
@decorators.path_wrapper
244-
def infer_call(self, context=None):
244+
def infer_call(
245+
self: nodes.Call, context: InferenceContext | None = None, **kwargs: Any
246+
) -> Generator[InferenceResult, None, InferenceErrorInfo]:
245247
"""infer a Call node by trying to guess what the function returns"""
246248
callcontext = copy_context(context)
247249
callcontext.boundnode = None
@@ -260,16 +262,23 @@ def infer_call(self, context=None):
260262
yield from callee.infer_call_result(caller=self, context=callcontext)
261263
except InferenceError:
262264
continue
263-
return dict(node=self, context=context)
265+
return InferenceErrorInfo(node=self, context=context)
264266

265267

266268
nodes.Call._infer = infer_call # type: ignore[assignment]
267269

268270

269271
@decorators.raise_if_nothing_inferred
270272
@decorators.path_wrapper
271-
def infer_import(self, context=None, asname=True):
273+
def infer_import(
274+
self: nodes.Import,
275+
context: InferenceContext | None = None,
276+
asname: bool = True,
277+
**kwargs: Any,
278+
) -> Generator[nodes.Module, None, None]:
272279
"""infer an Import node: return the imported module/object"""
280+
if not context:
281+
raise InferenceError(node=self, context=context)
273282
name = context.lookupname
274283
if name is None:
275284
raise InferenceError(node=self, context=context)
@@ -283,13 +292,20 @@ def infer_import(self, context=None, asname=True):
283292
raise InferenceError(node=self, context=context) from exc
284293

285294

286-
nodes.Import._infer = infer_import
295+
nodes.Import._infer = infer_import # type: ignore[assignment]
287296

288297

289298
@decorators.raise_if_nothing_inferred
290299
@decorators.path_wrapper
291-
def infer_import_from(self, context=None, asname=True):
300+
def infer_import_from(
301+
self: nodes.ImportFrom,
302+
context: InferenceContext | None = None,
303+
asname: bool = True,
304+
**kwargs: Any,
305+
) -> Generator[InferenceResult, None, None]:
292306
"""infer a ImportFrom node: return the imported module/object"""
307+
if not context:
308+
raise InferenceError(node=self, context=context)
293309
name = context.lookupname
294310
if name is None:
295311
raise InferenceError(node=self, context=context)

0 commit comments

Comments
 (0)