Skip to content

Commit 257f4e7

Browse files
authored
Revert "Type _infer of Attribute, AssignAttr, Global and Subscript (#1657)" (#1666)
This reverts commit 90362ad.
1 parent 8e7174a commit 257f4e7

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

astroid/inference.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ def infer_import(
277277
**kwargs: Any,
278278
) -> Generator[nodes.Module, None, None]:
279279
"""infer an Import node: return the imported module/object"""
280-
context = context or InferenceContext()
280+
if not context:
281+
raise InferenceError(node=self, context=context)
281282
name = context.lookupname
282283
if name is None:
283284
raise InferenceError(node=self, context=context)
@@ -303,7 +304,8 @@ def infer_import_from(
303304
**kwargs: Any,
304305
) -> Generator[InferenceResult, None, None]:
305306
"""infer a ImportFrom node: return the imported module/object"""
306-
context = context or InferenceContext()
307+
if not context:
308+
raise InferenceError(node=self, context=context)
307309
name = context.lookupname
308310
if name is None:
309311
raise InferenceError(node=self, context=context)
@@ -332,19 +334,18 @@ def infer_import_from(
332334
nodes.ImportFrom._infer = infer_import_from # type: ignore[assignment]
333335

334336

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]:
337+
def infer_attribute(self, context=None):
341338
"""infer an Attribute node by using getattr on the associated object"""
342339
for owner in self.expr.infer(context):
343340
if owner is util.Uninferable:
344341
yield owner
345342
continue
346343

347-
context = copy_context(context)
344+
if not context:
345+
context = InferenceContext()
346+
else:
347+
context = copy_context(context)
348+
348349
old_boundnode = context.boundnode
349350
try:
350351
context.boundnode = owner
@@ -357,20 +358,20 @@ def infer_attribute(
357358
pass
358359
finally:
359360
context.boundnode = old_boundnode
360-
return InferenceErrorInfo(node=self, context=context)
361+
return dict(node=self, context=context)
361362

362363

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

367370

368371
@decorators.raise_if_nothing_inferred
369372
@decorators.path_wrapper
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:
373+
def infer_global(self, context=None):
374+
if context.lookupname is None:
374375
raise InferenceError(node=self, context=context)
375376
try:
376377
return bases._infer_stmts(self.root().getattr(context.lookupname), context)
@@ -386,10 +387,7 @@ def infer_global(
386387
_SUBSCRIPT_SENTINEL = object()
387388

388389

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]:
390+
def infer_subscript(self, context=None):
393391
"""Inference for subscripts
394392
395393
We're understanding if the index is a Const
@@ -441,12 +439,14 @@ def infer_subscript(
441439
found_one = True
442440

443441
if found_one:
444-
return InferenceErrorInfo(node=self, context=context)
442+
return dict(node=self, context=context)
445443
return None
446444

447445

448-
nodes.Subscript._infer = decorators.path_wrapper(infer_subscript) # type: ignore[assignment]
449-
nodes.Subscript.infer_lhs = infer_subscript
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)
450450

451451

452452
@decorators.raise_if_nothing_inferred
@@ -981,7 +981,8 @@ def _infer_augassign(
981981
self: nodes.AugAssign, context: InferenceContext | None = None
982982
) -> Generator[InferenceResult | util.BadBinaryOperationMessage, None, None]:
983983
"""Inference logic for augmented binary operations."""
984-
context = context or InferenceContext()
984+
if context is None:
985+
context = InferenceContext()
985986

986987
rhs_context = context.clone()
987988

astroid/nodes/node_classes.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@
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 (
34-
InferenceErrorInfo,
35-
InferenceResult,
36-
SuccessfulInferenceResult,
37-
)
33+
from astroid.typing import InferenceResult, SuccessfulInferenceResult
3834

3935
if sys.version_info >= (3, 8):
4036
from typing import Literal
@@ -70,7 +66,7 @@ def _is_const(value):
7066
]
7167
InferLHS = Callable[
7268
[_NodesT, Optional[InferenceContext]],
73-
typing.Generator[InferenceResult, None, Optional[InferenceErrorInfo]],
69+
typing.Generator[InferenceResult, None, None],
7470
]
7571
InferBinaryOperation = Callable[
7672
[_NodesT, Optional[InferenceContext]],
@@ -970,8 +966,6 @@ class AssignAttr(_base_nodes.ParentAssignNode):
970966
_astroid_fields = ("expr",)
971967
_other_fields = ("attrname",)
972968

973-
infer_lhs: ClassVar[InferLHS[AssignAttr]]
974-
975969
@decorators.deprecate_default_argument_values(attrname="str")
976970
def __init__(
977971
self,
@@ -3843,8 +3837,6 @@ class Subscript(NodeNG):
38433837
_astroid_fields = ("value", "slice")
38443838
_other_fields = ("ctx",)
38453839

3846-
infer_lhs: ClassVar[InferLHS[Subscript]]
3847-
38483840
def __init__(
38493841
self,
38503842
ctx: Context | None = None,

0 commit comments

Comments
 (0)