@@ -158,23 +158,43 @@ def resolve_targets(
158158 classes : Mapping [str , frozenset [str ]] | None = None ,
159159 owner : str | None = None ,
160160 typed : Mapping [Value , str ] | None = None ,
161+ bases : Mapping [str , SymbolId ] | None = None ,
161162) -> tuple [dict [Value , Target ], dict [Value , SymbolId ]]:
162163 """Map every callee value of ``function`` to its target, and every symbol value.
163164 ``nested`` names the functions defined inside this one (``outer.inner``);
164165 ``classes`` maps the module's classes to their methods, ``owner`` is the class of a
165166 method and ``typed`` the parameters annotated with a module class. ``App(x)`` is a
166- call to ``App.__init__``, ``app.run()`` and ``self.run()`` calls to ``App.run``."""
167+ call to ``App.__init__``, ``app.run()`` and ``self.run()`` calls to ``App.run``.
168+ ``bases`` maps a module class to the symbol of its base: an attribute the class does
169+ not define is inherited, so ``self.get_argument`` in a ``RequestHandler`` subclass
170+ denotes ``tornado.web.RequestHandler.get_argument``."""
167171
168172 module = scopes .module_scope
169- symbols = derive_symbols (function , parameters )
170- targets : dict [Value , Target ] = {
171- value : ExternalSymbol (symbol ) for value , symbol in symbols .items ()
172- }
173173 classes = classes or {}
174174 instance_of : dict [Value , str ] = dict (typed or {})
175175 if owner is not None and function .parameters :
176176 instance_of [function .parameters [0 ]] = owner
177177 defs = {i .result : i for block in function .blocks for i in block .instructions if i .result }
178+ for block in function .blocks :
179+ for instruction in block .instructions :
180+ if isinstance (instruction , Call ):
181+ callee = defs .get (instruction .callee )
182+ if isinstance (callee , Global ) and callee .name in classes :
183+ instance_of [instruction .result ] = callee .name
184+ inherited : dict [Value , SymbolId ] = dict (parameters or {})
185+ for block in function .blocks :
186+ for instruction in block .instructions :
187+ if isinstance (instruction , GetAttr ) and instruction .object in instance_of :
188+ if instruction .object in inherited :
189+ continue # an annotated parameter denotes its own class
190+ class_name = instance_of [instruction .object ]
191+ base = (bases or {}).get (class_name )
192+ if base is not None and instruction .attribute not in classes .get (class_name , ()):
193+ inherited [instruction .result ] = base .attribute (instruction .attribute )
194+ symbols = derive_symbols (function , inherited )
195+ targets : dict [Value , Target ] = {
196+ value : ExternalSymbol (symbol ) for value , symbol in symbols .items ()
197+ }
178198 for block in function .blocks :
179199 for instruction in block .instructions :
180200 if isinstance (instruction , MakeFunction ):
@@ -189,17 +209,29 @@ def resolve_targets(
189209 targets [instruction .result ] = KnownFunction (instruction .name )
190210 elif binding .kind is BindingKind .CLASS and "__init__" in classes .get (instruction .name , ()):
191211 targets [instruction .result ] = KnownFunction (f"{ instruction .name } .__init__" )
192- elif isinstance (instruction , Call ):
193- callee = defs .get (instruction .callee )
194- if isinstance (callee , Global ) and callee .name in classes :
195- instance_of [instruction .result ] = callee .name
196212 elif isinstance (instruction , GetAttr ) and instruction .object in instance_of :
197213 method = f"{ instance_of [instruction .object ]} .{ instruction .attribute } "
198214 if method in nested :
199215 targets [instruction .result ] = KnownFunction (method )
216+ elif isinstance (instruction , GetAttr ):
217+ # ``Student.create(...)``: a static or class method called on the class.
218+ origin = defs .get (instruction .object )
219+ if isinstance (origin , Global ) and origin .name in classes :
220+ method = f"{ origin .name } .{ instruction .attribute } "
221+ if method in nested :
222+ targets [instruction .result ] = KnownFunction (method )
200223 return targets , symbols
201224
202225
226+ def _is_staticmethod (function : nodes .Function ) -> bool :
227+ for decorator in function .decorators :
228+ if isinstance (decorator , nodes .Name ) and decorator .identifier == "staticmethod" :
229+ return True
230+ if isinstance (decorator , nodes .Attribute ) and decorator .name == "staticmethod" :
231+ return True
232+ return False
233+
234+
203235def _annotated (
204236 function : nodes .Function , ssa : FunctionIR , scopes : ScopeTable , table : SymbolTable
205237) -> dict [Value , SymbolId ]:
@@ -257,6 +289,14 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
257289 for statement in ctx .module .body
258290 if isinstance (statement , nodes .Class )
259291 }
292+ bases : dict [str , SymbolId ] = {}
293+ for statement in ctx .module .body :
294+ if isinstance (statement , nodes .Class ):
295+ for base_expression in statement .bases :
296+ symbol = table .resolve_expression (scopes .module_scope .id , base_expression )
297+ if symbol is not None :
298+ bases [statement .name ] = symbol
299+ break
260300 sites : dict [str , tuple [CallSite , ...]] = {}
261301 symbols : dict [str , Mapping [Value , SymbolId ]] = {}
262302 unsupported : set [str ] = set ()
@@ -268,6 +308,8 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
268308 sites [name ] = ()
269309 continue
270310 owner = name .rsplit ("." , 1 )[0 ] if "." in name and name .rsplit ("." , 1 )[0 ] in classes else None
311+ if owner is not None and _is_staticmethod (function ):
312+ owner = None # a static method has no receiver
271313 targets , symbols [name ] = resolve_targets (
272314 ssa ,
273315 scopes ,
@@ -277,6 +319,7 @@ def compute(cls, ctx: AnalysisContext) -> CallGraph:
277319 classes ,
278320 owner ,
279321 _typed_with_module_classes (function , ssa , classes ),
322+ bases ,
280323 )
281324 found : list [CallSite ] = []
282325 for block in ssa .blocks :
0 commit comments