40
40
from astroid .interpreter .dunder_lookup import lookup
41
41
from astroid .interpreter .objectmodel import ClassModel , FunctionModel , ModuleModel
42
42
from astroid .manager import AstroidManager
43
- from astroid .nodes import (
44
- Arguments ,
45
- Const ,
46
- NodeNG ,
47
- _base_nodes ,
48
- const_factory ,
49
- node_classes ,
50
- )
43
+ from astroid .nodes import _base_nodes , node_classes
51
44
from astroid .nodes .scoped_nodes .mixin import ComprehensionScope , LocalsDictNodeNG
52
45
from astroid .nodes .scoped_nodes .utils import builtin_lookup
53
46
from astroid .nodes .utils import Position
60
53
61
54
if TYPE_CHECKING :
62
55
from astroid import nodes , objects
56
+ from astroid .nodes import Arguments , Const , NodeNG
63
57
from astroid .nodes ._base_nodes import LookupMixIn
64
58
65
59
@@ -177,6 +171,15 @@ def function_to_method(n, klass):
177
171
return n
178
172
179
173
174
+ def _infer_last (
175
+ arg : SuccessfulInferenceResult , context : InferenceContext
176
+ ) -> InferenceResult :
177
+ res = util .Uninferable
178
+ for b in arg .infer (context = context .clone ()):
179
+ res = b
180
+ return res
181
+
182
+
180
183
class Module (LocalsDictNodeNG ):
181
184
"""Class representing an :class:`ast.Module` node.
182
185
@@ -353,7 +356,7 @@ def getattr(
353
356
if name in self .special_attributes and not ignore_locals and not name_in_locals :
354
357
result = [self .special_attributes .lookup (name )]
355
358
if name == "__name__" :
356
- main_const = const_factory ("__main__" )
359
+ main_const = node_classes . const_factory ("__main__" )
357
360
main_const .parent = AstroidManager ().builtins_module
358
361
result .append (main_const )
359
362
elif not ignore_locals and name_in_locals :
@@ -607,6 +610,14 @@ def _infer(
607
610
yield self
608
611
609
612
613
+ class __SyntheticRoot (Module ):
614
+ def __init__ (self ):
615
+ super ().__init__ ("__astroid_synthetic" , pure_python = False )
616
+
617
+
618
+ SYNTHETIC_ROOT = __SyntheticRoot ()
619
+
620
+
610
621
class GeneratorExp (ComprehensionScope ):
611
622
"""Class representing an :class:`ast.GeneratorExp` node.
612
623
@@ -1538,10 +1549,7 @@ def infer_yield_result(self, context: InferenceContext | None = None):
1538
1549
"""
1539
1550
for yield_ in self .nodes_of_class (node_classes .Yield ):
1540
1551
if yield_ .value is None :
1541
- const = node_classes .Const (None )
1542
- const .parent = yield_
1543
- const .lineno = yield_ .lineno
1544
- yield const
1552
+ yield node_classes .Const (None , parent = yield_ , lineno = yield_ .lineno )
1545
1553
elif yield_ .scope () == self :
1546
1554
yield from yield_ .value .infer (context = context )
1547
1555
@@ -1551,6 +1559,8 @@ def infer_call_result(
1551
1559
context : InferenceContext | None = None ,
1552
1560
) -> Iterator [InferenceResult ]:
1553
1561
"""Infer what the function returns when called."""
1562
+ if context is None :
1563
+ context = InferenceContext ()
1554
1564
if self .is_generator ():
1555
1565
if isinstance (self , AsyncFunctionDef ):
1556
1566
generator_cls : type [bases .Generator ] = bases .AsyncGenerator
@@ -1572,7 +1582,7 @@ def infer_call_result(
1572
1582
and len (self .args .args ) == 1
1573
1583
and self .args .vararg is not None
1574
1584
):
1575
- if isinstance (caller .args , Arguments ):
1585
+ if isinstance (caller .args , node_classes . Arguments ):
1576
1586
assert caller .args .args is not None
1577
1587
metaclass = next (caller .args .args [0 ].infer (context ), None )
1578
1588
elif isinstance (caller .args , list ):
@@ -1582,27 +1592,14 @@ def infer_call_result(
1582
1592
f"caller.args was neither Arguments nor list; got { type (caller .args )} "
1583
1593
)
1584
1594
if isinstance (metaclass , ClassDef ):
1585
- try :
1586
- class_bases = [
1587
- # Find the first non-None inferred base value
1588
- next (
1589
- b
1590
- for b in arg .infer (
1591
- context = context .clone () if context else context
1592
- )
1593
- if not (isinstance (b , Const ) and b .value is None )
1594
- )
1595
- for arg in caller .args [1 :]
1596
- ]
1597
- except StopIteration as e :
1598
- raise InferenceError (node = caller .args [1 :], context = context ) from e
1595
+ class_bases = [_infer_last (x , context ) for x in caller .args [1 :]]
1599
1596
new_class = ClassDef (
1600
1597
name = "temporary_class" ,
1601
1598
lineno = 0 ,
1602
1599
col_offset = 0 ,
1603
1600
end_lineno = 0 ,
1604
1601
end_col_offset = 0 ,
1605
- parent = AstroidManager (). synthetic_root ,
1602
+ parent = SYNTHETIC_ROOT ,
1606
1603
)
1607
1604
new_class .hide = True
1608
1605
new_class .postinit (
@@ -2827,13 +2824,8 @@ def _inferred_bases(self, context: InferenceContext | None = None):
2827
2824
2828
2825
for stmt in self .bases :
2829
2826
try :
2830
- # Find the first non-None inferred base value
2831
- baseobj = next (
2832
- b
2833
- for b in stmt .infer (context = context .clone ())
2834
- if not (isinstance (b , Const ) and b .value is None )
2835
- )
2836
- except (InferenceError , StopIteration ):
2827
+ baseobj = _infer_last (stmt , context )
2828
+ except InferenceError :
2837
2829
continue
2838
2830
if isinstance (baseobj , bases .Instance ):
2839
2831
baseobj = baseobj ._proxied
0 commit comments