39
39
from astroid .interpreter .dunder_lookup import lookup
40
40
from astroid .interpreter .objectmodel import ClassModel , FunctionModel , ModuleModel
41
41
from astroid .manager import AstroidManager
42
- from astroid .nodes import Arguments , Const , node_classes
42
+ from astroid .nodes import Arguments , Const , NodeNG , node_classes
43
43
from astroid .nodes .scoped_nodes .mixin import ComprehensionScope , LocalsDictNodeNG
44
44
from astroid .nodes .scoped_nodes .utils import builtin_lookup
45
45
from astroid .nodes .utils import Position
@@ -1074,6 +1074,8 @@ class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG):
1074
1074
_other_other_fields = ("locals" ,)
1075
1075
name = "<lambda>"
1076
1076
is_lambda = True
1077
+ special_attributes = FunctionModel ()
1078
+ """The names of special attributes that this function has."""
1077
1079
1078
1080
def implicit_parameters (self ):
1079
1081
return 0
@@ -1133,6 +1135,8 @@ def __init__(
1133
1135
:type: list(NodeNG)
1134
1136
"""
1135
1137
1138
+ self .instance_attrs : Dict [str , List [NodeNG ]] = {}
1139
+
1136
1140
super ().__init__ (
1137
1141
lineno = lineno ,
1138
1142
col_offset = col_offset ,
@@ -1263,6 +1267,21 @@ def frame(self: T, *, future: Literal[None, True] = None) -> T:
1263
1267
"""
1264
1268
return self
1265
1269
1270
+ def getattr (
1271
+ self , name : str , context : Optional [InferenceContext ] = None
1272
+ ) -> List [NodeNG ]:
1273
+ if not name :
1274
+ raise AttributeInferenceError (target = self , attribute = name , context = context )
1275
+
1276
+ found_attrs = []
1277
+ if name in self .instance_attrs :
1278
+ found_attrs = self .instance_attrs [name ]
1279
+ if name in self .special_attributes :
1280
+ found_attrs .append (self .special_attributes .lookup (name ))
1281
+ if found_attrs :
1282
+ return found_attrs
1283
+ raise AttributeInferenceError (target = self , attribute = name )
1284
+
1266
1285
1267
1286
class FunctionDef (mixins .MultiLineBlockMixin , node_classes .Statement , Lambda ):
1268
1287
"""Class representing an :class:`ast.FunctionDef`.
@@ -1281,11 +1300,7 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda):
1281
1300
returns = None
1282
1301
decorators : Optional [node_classes .Decorators ] = None
1283
1302
"""The decorators that are applied to this method or function."""
1284
- special_attributes = FunctionModel ()
1285
- """The names of special attributes that this function has.
1286
1303
1287
- :type: objectmodel.FunctionModel
1288
- """
1289
1304
is_function = True
1290
1305
"""Whether this node indicates a function.
1291
1306
@@ -1583,22 +1598,6 @@ def block_range(self, lineno):
1583
1598
"""
1584
1599
return self .fromlineno , self .tolineno
1585
1600
1586
- def getattr (self , name , context = None ):
1587
- """this method doesn't look in the instance_attrs dictionary since it's
1588
- done by an Instance proxy at inference time.
1589
- """
1590
- if not name :
1591
- raise AttributeInferenceError (target = self , attribute = name , context = context )
1592
-
1593
- found_attrs = []
1594
- if name in self .instance_attrs :
1595
- found_attrs = self .instance_attrs [name ]
1596
- if name in self .special_attributes :
1597
- found_attrs .append (self .special_attributes .lookup (name ))
1598
- if found_attrs :
1599
- return found_attrs
1600
- raise AttributeInferenceError (target = self , attribute = name )
1601
-
1602
1601
def igetattr (self , name , context = None ):
1603
1602
"""Inferred getattr, which returns an iterator of inferred statements."""
1604
1603
try :
0 commit comments