@@ -277,8 +277,7 @@ def infer_import(
277
277
** kwargs : Any ,
278
278
) -> Generator [nodes .Module , None , None ]:
279
279
"""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 ()
282
281
name = context .lookupname
283
282
if name is None :
284
283
raise InferenceError (node = self , context = context )
@@ -304,8 +303,7 @@ def infer_import_from(
304
303
** kwargs : Any ,
305
304
) -> Generator [InferenceResult , None , None ]:
306
305
"""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 ()
309
307
name = context .lookupname
310
308
if name is None :
311
309
raise InferenceError (node = self , context = context )
@@ -334,18 +332,19 @@ def infer_import_from(
334
332
nodes .ImportFrom ._infer = infer_import_from # type: ignore[assignment]
335
333
336
334
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 ]:
338
341
"""infer an Attribute node by using getattr on the associated object"""
339
342
for owner in self .expr .infer (context ):
340
343
if owner is util .Uninferable :
341
344
yield owner
342
345
continue
343
346
344
- if not context :
345
- context = InferenceContext ()
346
- else :
347
- context = copy_context (context )
348
-
347
+ context = copy_context (context )
349
348
old_boundnode = context .boundnode
350
349
try :
351
350
context .boundnode = owner
@@ -358,20 +357,20 @@ def infer_attribute(self, context=None):
358
357
pass
359
358
finally :
360
359
context .boundnode = old_boundnode
361
- return dict (node = self , context = context )
360
+ return InferenceErrorInfo (node = self , context = context )
362
361
363
362
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]
367
364
# 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
369
366
370
367
371
368
@decorators .raise_if_nothing_inferred
372
369
@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 :
375
374
raise InferenceError (node = self , context = context )
376
375
try :
377
376
return bases ._infer_stmts (self .root ().getattr (context .lookupname ), context )
@@ -387,7 +386,10 @@ def infer_global(self, context=None):
387
386
_SUBSCRIPT_SENTINEL = object ()
388
387
389
388
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 ]:
391
393
"""Inference for subscripts
392
394
393
395
We're understanding if the index is a Const
@@ -439,14 +441,12 @@ def infer_subscript(self, context=None):
439
441
found_one = True
440
442
441
443
if found_one :
442
- return dict (node = self , context = context )
444
+ return InferenceErrorInfo (node = self , context = context )
443
445
return None
444
446
445
447
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
450
450
451
451
452
452
@decorators .raise_if_nothing_inferred
@@ -963,8 +963,7 @@ def _infer_compare(
963
963
964
964
def _infer_augassign (self , context = None ):
965
965
"""Inference logic for augmented binary operations."""
966
- if context is None :
967
- context = InferenceContext ()
966
+ context = context or InferenceContext ()
968
967
969
968
rhs_context = context .clone ()
970
969
0 commit comments