Skip to content

Commit 91533b7

Browse files
DanielNoordcdce8p
andauthored
Type _infer of Attribute, AssignAttr, Global and Subscript (#1657) (#1668)
Co-authored-by: Marc Mueller <[email protected]>
1 parent ef104c3 commit 91533b7

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

astroid/inference.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ def infer_name(
233233

234234

235235
# pylint: disable=no-value-for-parameter
236+
# The order of the decorators here is important
237+
# See https://github.com/PyCQA/astroid/commit/0a8a75db30da060a24922e05048bc270230f5
236238
nodes.Name._infer = decorators.raise_if_nothing_inferred( # type: ignore[assignment]
237239
decorators.path_wrapper(infer_name)
238240
)
@@ -277,8 +279,7 @@ def infer_import(
277279
**kwargs: Any,
278280
) -> Generator[nodes.Module, None, None]:
279281
"""infer an Import node: return the imported module/object"""
280-
if not context:
281-
raise InferenceError(node=self, context=context)
282+
context = context or InferenceContext()
282283
name = context.lookupname
283284
if name is None:
284285
raise InferenceError(node=self, context=context)
@@ -304,8 +305,7 @@ def infer_import_from(
304305
**kwargs: Any,
305306
) -> Generator[InferenceResult, None, None]:
306307
"""infer a ImportFrom node: return the imported module/object"""
307-
if not context:
308-
raise InferenceError(node=self, context=context)
308+
context = context or InferenceContext()
309309
name = context.lookupname
310310
if name is None:
311311
raise InferenceError(node=self, context=context)
@@ -334,18 +334,18 @@ def infer_import_from(
334334
nodes.ImportFrom._infer = infer_import_from # type: ignore[assignment]
335335

336336

337-
def infer_attribute(self, context=None):
337+
def infer_attribute(
338+
self: nodes.Attribute | nodes.AssignAttr,
339+
context: InferenceContext | None = None,
340+
**kwargs: Any,
341+
) -> Generator[InferenceResult, None, InferenceErrorInfo]:
338342
"""infer an Attribute node by using getattr on the associated object"""
339343
for owner in self.expr.infer(context):
340344
if owner is util.Uninferable:
341345
yield owner
342346
continue
343347

344-
if not context:
345-
context = InferenceContext()
346-
else:
347-
context = copy_context(context)
348-
348+
context = copy_context(context)
349349
old_boundnode = context.boundnode
350350
try:
351351
context.boundnode = owner
@@ -358,10 +358,12 @@ def infer_attribute(self, context=None):
358358
pass
359359
finally:
360360
context.boundnode = old_boundnode
361-
return dict(node=self, context=context)
361+
return InferenceErrorInfo(node=self, context=context)
362362

363363

364-
nodes.Attribute._infer = decorators.raise_if_nothing_inferred(
364+
# The order of the decorators here is important
365+
# See https://github.com/PyCQA/astroid/commit/0a8a75db30da060a24922e05048bc270230f5
366+
nodes.Attribute._infer = decorators.raise_if_nothing_inferred( # type: ignore[assignment]
365367
decorators.path_wrapper(infer_attribute)
366368
)
367369
# won't work with a path wrapper
@@ -370,8 +372,10 @@ def infer_attribute(self, context=None):
370372

371373
@decorators.raise_if_nothing_inferred
372374
@decorators.path_wrapper
373-
def infer_global(self, context=None):
374-
if context.lookupname is None:
375+
def infer_global(
376+
self: nodes.Global, context: InferenceContext | None = None, **kwargs: Any
377+
) -> Generator[InferenceResult, None, None]:
378+
if context is None or context.lookupname is None:
375379
raise InferenceError(node=self, context=context)
376380
try:
377381
return bases._infer_stmts(self.root().getattr(context.lookupname), context)
@@ -387,7 +391,9 @@ def infer_global(self, context=None):
387391
_SUBSCRIPT_SENTINEL = object()
388392

389393

390-
def infer_subscript(self, context=None):
394+
def infer_subscript(
395+
self: nodes.Subscript, context: InferenceContext | None = None, **kwargs: Any
396+
) -> Generator[InferenceResult, None, InferenceErrorInfo | None]:
391397
"""Inference for subscripts
392398
393399
We're understanding if the index is a Const
@@ -439,10 +445,12 @@ def infer_subscript(self, context=None):
439445
found_one = True
440446

441447
if found_one:
442-
return dict(node=self, context=context)
448+
return InferenceErrorInfo(node=self, context=context)
443449
return None
444450

445451

452+
# The order of the decorators here is important
453+
# See https://github.com/PyCQA/astroid/commit/0a8a75db30da060a24922e05048bc270230f5
446454
nodes.Subscript._infer = decorators.raise_if_nothing_inferred( # type: ignore[assignment]
447455
decorators.path_wrapper(infer_subscript)
448456
)
@@ -981,8 +989,7 @@ def _infer_augassign(
981989
self: nodes.AugAssign, context: InferenceContext | None = None
982990
) -> Generator[InferenceResult | util.BadBinaryOperationMessage, None, None]:
983991
"""Inference logic for augmented binary operations."""
984-
if context is None:
985-
context = InferenceContext()
992+
context = context or InferenceContext()
986993

987994
rhs_context = context.clone()
988995

astroid/nodes/node_classes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from astroid.nodes.node_ng import NodeNG
3333
from astroid.typing import (
3434
ConstFactoryResult,
35+
InferenceErrorInfo,
3536
InferenceResult,
3637
SuccessfulInferenceResult,
3738
)
@@ -74,7 +75,7 @@ def _is_const(value):
7475
]
7576
InferLHS = Callable[
7677
[_NodesT, Optional[InferenceContext]],
77-
typing.Generator[InferenceResult, None, None],
78+
typing.Generator[InferenceResult, None, Optional[InferenceErrorInfo]],
7879
]
7980
InferUnaryOp = Callable[[_NodesT, str], ConstFactoryResult]
8081

@@ -971,6 +972,8 @@ class AssignAttr(_base_nodes.ParentAssignNode):
971972
_astroid_fields = ("expr",)
972973
_other_fields = ("attrname",)
973974

975+
infer_lhs: ClassVar[InferLHS[AssignAttr]]
976+
974977
@decorators.deprecate_default_argument_values(attrname="str")
975978
def __init__(
976979
self,
@@ -3850,6 +3853,8 @@ class Subscript(NodeNG):
38503853
_astroid_fields = ("value", "slice")
38513854
_other_fields = ("ctx",)
38523855

3856+
infer_lhs: ClassVar[InferLHS[Subscript]]
3857+
38533858
def __init__(
38543859
self,
38553860
ctx: Context | None = None,

0 commit comments

Comments
 (0)