17
17
from astroid .inference_tip import inference_tip
18
18
from astroid .interpreter import objectmodel
19
19
from astroid .manager import AstroidManager
20
- from astroid .nodes .node_classes import AssignName , Attribute , Call , Name
21
- from astroid .nodes .scoped_nodes import FunctionDef
22
20
from astroid .typing import InferenceResult , SuccessfulInferenceResult
23
21
from astroid .util import UninferableBase , safe_infer
24
22
@@ -92,7 +90,7 @@ def _functools_partial_inference(
92
90
raise UseInferenceDefault from exc
93
91
if isinstance (inferred_wrapped_function , UninferableBase ):
94
92
raise UseInferenceDefault ("Cannot infer the wrapped function" )
95
- if not isinstance (inferred_wrapped_function , FunctionDef ):
93
+ if not isinstance (inferred_wrapped_function , nodes . FunctionDef ):
96
94
raise UseInferenceDefault ("The wrapped function is not a function" )
97
95
98
96
# Determine if the passed keywords into the callsite are supported
@@ -106,7 +104,9 @@ def _functools_partial_inference(
106
104
inferred_wrapped_function .args .kwonlyargs or (),
107
105
)
108
106
parameter_names = {
109
- param .name for param in function_parameters if isinstance (param , AssignName )
107
+ param .name
108
+ for param in function_parameters
109
+ if isinstance (param , nodes .AssignName )
110
110
}
111
111
if set (call .keyword_arguments ) - parameter_names :
112
112
raise UseInferenceDefault ("wrapped function received unknown parameters" )
@@ -135,23 +135,25 @@ def _looks_like_lru_cache(node) -> bool:
135
135
if not node .decorators :
136
136
return False
137
137
for decorator in node .decorators .nodes :
138
- if not isinstance (decorator , (Attribute , Call )):
138
+ if not isinstance (decorator , (nodes . Attribute , nodes . Call )):
139
139
continue
140
140
if _looks_like_functools_member (decorator , "lru_cache" ):
141
141
return True
142
142
return False
143
143
144
144
145
- def _looks_like_functools_member (node : Attribute | Call , member : str ) -> bool :
145
+ def _looks_like_functools_member (
146
+ node : nodes .Attribute | nodes .Call , member : str
147
+ ) -> bool :
146
148
"""Check if the given Call node is the wanted member of functools."""
147
- if isinstance (node , Attribute ):
149
+ if isinstance (node , nodes . Attribute ):
148
150
return node .attrname == member
149
- if isinstance (node .func , Name ):
151
+ if isinstance (node .func , nodes . Name ):
150
152
return node .func .name == member
151
- if isinstance (node .func , Attribute ):
153
+ if isinstance (node .func , nodes . Attribute ):
152
154
return (
153
155
node .func .attrname == member
154
- and isinstance (node .func .expr , Name )
156
+ and isinstance (node .func .expr , nodes . Name )
155
157
and node .func .expr .name == "functools"
156
158
)
157
159
return False
@@ -161,10 +163,12 @@ def _looks_like_functools_member(node: Attribute | Call, member: str) -> bool:
161
163
162
164
163
165
def register (manager : AstroidManager ) -> None :
164
- manager .register_transform (FunctionDef , _transform_lru_cache , _looks_like_lru_cache )
166
+ manager .register_transform (
167
+ nodes .FunctionDef , _transform_lru_cache , _looks_like_lru_cache
168
+ )
165
169
166
170
manager .register_transform (
167
- Call ,
171
+ nodes . Call ,
168
172
inference_tip (_functools_partial_inference ),
169
173
_looks_like_partial ,
170
174
)
0 commit comments