Skip to content

Commit 90362ad

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

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

astroid/inference.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ def infer_import(
277277
**kwargs: Any,
278278
) -> Generator[nodes.Module, None, None]:
279279
"""infer an Import node: return the imported module/object"""
280-
if not context:
281-
raise InferenceError(node=self, context=context)
280+
context = context or InferenceContext()
282281
name = context.lookupname
283282
if name is None:
284283
raise InferenceError(node=self, context=context)
@@ -304,8 +303,7 @@ def infer_import_from(
304303
**kwargs: Any,
305304
) -> Generator[InferenceResult, None, None]:
306305
"""infer a ImportFrom node: return the imported module/object"""
307-
if not context:
308-
raise InferenceError(node=self, context=context)
306+
context = context or InferenceContext()
309307
name = context.lookupname
310308
if name is None:
311309
raise InferenceError(node=self, context=context)
@@ -334,18 +332,19 @@ def infer_import_from(
334332
nodes.ImportFrom._infer = infer_import_from # type: ignore[assignment]
335333

336334

337-
def infer_attribute(self, context=None):
335+
@decorators.raise_if_nothing_inferred
336+
def infer_attribute(
337+
self: nodes.Attribute | nodes.AssignAttr,
338+
context: InferenceContext | None = None,
339+
**kwargs: Any,
340+
) -> Generator[InferenceResult, None, InferenceErrorInfo]:
338341
"""infer an Attribute node by using getattr on the associated object"""
339342
for owner in self.expr.infer(context):
340343
if owner is util.Uninferable:
341344
yield owner
342345
continue
343346

344-
if not context:
345-
context = InferenceContext()
346-
else:
347-
context = copy_context(context)
348-
347+
context = copy_context(context)
349348
old_boundnode = context.boundnode
350349
try:
351350
context.boundnode = owner
@@ -358,20 +357,20 @@ def infer_attribute(self, context=None):
358357
pass
359358
finally:
360359
context.boundnode = old_boundnode
361-
return dict(node=self, context=context)
360+
return InferenceErrorInfo(node=self, context=context)
362361

363362

364-
nodes.Attribute._infer = decorators.raise_if_nothing_inferred(
365-
decorators.path_wrapper(infer_attribute)
366-
)
363+
nodes.Attribute._infer = decorators.path_wrapper(infer_attribute) # type: ignore[assignment]
367364
# won't work with a path wrapper
368-
nodes.AssignAttr.infer_lhs = decorators.raise_if_nothing_inferred(infer_attribute)
365+
nodes.AssignAttr.infer_lhs = infer_attribute
369366

370367

371368
@decorators.raise_if_nothing_inferred
372369
@decorators.path_wrapper
373-
def infer_global(self, context=None):
374-
if context.lookupname is None:
370+
def infer_global(
371+
self: nodes.Global, context: InferenceContext | None = None, **kwargs: Any
372+
) -> Generator[InferenceResult, None, None]:
373+
if context is None or context.lookupname is None:
375374
raise InferenceError(node=self, context=context)
376375
try:
377376
return bases._infer_stmts(self.root().getattr(context.lookupname), context)
@@ -387,7 +386,10 @@ def infer_global(self, context=None):
387386
_SUBSCRIPT_SENTINEL = object()
388387

389388

390-
def infer_subscript(self, context=None):
389+
@decorators.raise_if_nothing_inferred
390+
def infer_subscript(
391+
self: nodes.Subscript, context: InferenceContext | None = None, **kwargs: Any
392+
) -> Generator[InferenceResult, None, InferenceErrorInfo | None]:
391393
"""Inference for subscripts
392394
393395
We're understanding if the index is a Const
@@ -439,14 +441,12 @@ def infer_subscript(self, context=None):
439441
found_one = True
440442

441443
if found_one:
442-
return dict(node=self, context=context)
444+
return InferenceErrorInfo(node=self, context=context)
443445
return None
444446

445447

446-
nodes.Subscript._infer = decorators.raise_if_nothing_inferred( # type: ignore[assignment]
447-
decorators.path_wrapper(infer_subscript)
448-
)
449-
nodes.Subscript.infer_lhs = decorators.raise_if_nothing_inferred(infer_subscript)
448+
nodes.Subscript._infer = decorators.path_wrapper(infer_subscript) # type: ignore[assignment]
449+
nodes.Subscript.infer_lhs = infer_subscript
450450

451451

452452
@decorators.raise_if_nothing_inferred
@@ -963,8 +963,7 @@ def _infer_compare(
963963

964964
def _infer_augassign(self, context=None):
965965
"""Inference logic for augmented binary operations."""
966-
if context is None:
967-
context = InferenceContext()
966+
context = context or InferenceContext()
968967

969968
rhs_context = context.clone()
970969

astroid/nodes/node_classes.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
from astroid.nodes import _base_nodes
3131
from astroid.nodes.const import OP_PRECEDENCE
3232
from astroid.nodes.node_ng import NodeNG
33-
from astroid.typing import InferenceResult, SuccessfulInferenceResult
33+
from astroid.typing import (
34+
InferenceErrorInfo,
35+
InferenceResult,
36+
SuccessfulInferenceResult,
37+
)
3438

3539
if sys.version_info >= (3, 8):
3640
from typing import Literal
@@ -65,7 +69,7 @@ def _is_const(value):
6569
]
6670
InferLHS = Callable[
6771
[_NodesT, Optional[InferenceContext]],
68-
typing.Generator[InferenceResult, None, None],
72+
typing.Generator[InferenceResult, None, Optional[InferenceErrorInfo]],
6973
]
7074

7175

@@ -961,6 +965,8 @@ class AssignAttr(_base_nodes.ParentAssignNode):
961965
_astroid_fields = ("expr",)
962966
_other_fields = ("attrname",)
963967

968+
infer_lhs: ClassVar[InferLHS[AssignAttr]]
969+
964970
@decorators.deprecate_default_argument_values(attrname="str")
965971
def __init__(
966972
self,
@@ -3832,6 +3838,8 @@ class Subscript(NodeNG):
38323838
_astroid_fields = ("value", "slice")
38333839
_other_fields = ("ctx",)
38343840

3841+
infer_lhs: ClassVar[InferLHS[Subscript]]
3842+
38353843
def __init__(
38363844
self,
38373845
ctx: Context | None = None,

0 commit comments

Comments
 (0)