inspect_utils¶
Utilities for inspecting Python objects, classes, functions, and frames.
This module provides helper functions for runtime introspection, including qualified name resolution, caller frame inspection, argument extraction from locals, class slot enumeration, and MRO traversal.
Main APIs
classof, moduleof, cname, func_name, qual_name, qual_mro, is_subclass, get_caller_function, current_module, fetch_args, fetch_call, get_fn_kwargs, get_defaulted_params, parent_locals, parent_globals, parent_coords, class_slots
Note that the parent_*() helpers look past the immediate caller: the
frame at level + 2 - i.e. the caller of the function calling the
helper - is inspected by default.
Example
from py_misc_utils import inspect_utils as iu
class Greeter:
def hello(self):
pass
# Qualified names resolve through instances to their class.
assert iu.qual_name(Greeter()).endswith('.Greeter')
assert iu.qual_name(dict) == 'builtins.dict'
classof(obj)
¶
Returns the class of an object, or the object itself if it is a class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
An object or class. |
required |
Returns:
| Type | Description |
|---|---|
|
|
|
|
unavailable. |
Source code in py_misc_utils/inspect_utils.py
41 42 43 44 45 46 47 48 49 50 51 | |
moduleof(obj)
¶
Returns the module name where an object's class is defined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
An object or class. |
required |
Returns:
| Type | Description |
|---|---|
|
The |
|
|
available. |
Source code in py_misc_utils/inspect_utils.py
54 55 56 57 58 59 60 61 62 63 64 65 66 | |
cname(obj)
¶
Returns the class name of an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
An object or class. |
required |
Returns:
| Type | Description |
|---|---|
|
The |
|
|
available. |
Source code in py_misc_utils/inspect_utils.py
69 70 71 72 73 74 75 76 77 78 79 80 81 | |
func_name(func)
¶
Returns the name of a function or class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
A function, method, class, or other callable object. |
required |
Returns:
| Type | Description |
|---|---|
|
The |
|
|
|
Source code in py_misc_utils/inspect_utils.py
84 85 86 87 88 89 90 91 92 93 94 95 96 | |
qual_name(obj, builtin_strip=False)
¶
Returns the qualified name for a class, function, method, or module.
For non-class/function/method/module objects, uses the object's class - so the qualified name of an instance is the qualified name of its class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The object to get the qualified name for. |
required | |
builtin_strip
|
If True, strips builtin module prefixes. |
False
|
Returns:
| Type | Description |
|---|---|
|
A dotted qualified name string. |
Source code in py_misc_utils/inspect_utils.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
qual_mro(obj, builtin_strip=False)
¶
Yields qualified names for all classes in an object's MRO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
A class or an instance of a class. |
required | |
builtin_strip
|
If True, strips builtin module prefixes. |
False
|
Yields:
| Type | Description |
|---|---|
|
Qualified name strings for each class in |
|
|
most specific class up to |
Source code in py_misc_utils/inspect_utils.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
is_subclass(cls, cls_group)
¶
Checks whether cls is a subclass of cls_group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
The class to check. |
required | |
cls_group
|
A class or tuple of classes to check against. |
required |
Returns:
| Type | Description |
|---|---|
|
True if |
|
|
otherwise. |
Source code in py_misc_utils/inspect_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 | |
get_caller_function(back=0, frame=None)
¶
Returns the function object of the caller at a given stack depth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
back
|
Number of frames to go back from the current frame (default 0). |
0
|
|
frame
|
An optional frame object to use instead of walking the stack. |
None
|
Returns:
| Type | Description |
|---|---|
|
The function object of the caller, resolved via qualified name lookup. |
Source code in py_misc_utils/inspect_utils.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
current_module()
¶
Returns the module of the caller's frame.
Returns:
| Type | Description |
|---|---|
|
The module object of the frame one level up the call stack. |
Source code in py_misc_utils/inspect_utils.py
225 226 227 228 229 230 231 | |
fetch_args(func, locs, input_args=())
¶
Extracts positional and keyword arguments for a function from locals.
Optional parameters pick their value from the matching local variable, falling back to the parameter default when the local is None or missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function whose signature is used to extract arguments. |
required | |
locs
|
A dict-like mapping of local variable names to values. |
required | |
input_args
|
A tuple of input arguments to use as positional defaults. |
()
|
Returns:
| Type | Description |
|---|---|
|
A tuple |
|
|
arguments and |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a required positional argument is missing from locals and no input_args remain. |
Source code in py_misc_utils/inspect_utils.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
fetch_call(func, locs, input_args=())
¶
Calls a function with arguments extracted from locals.
Uses fetch_args() to build the call arguments from the given locals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function to call. |
required | |
locs
|
A dict-like mapping of local variable names to values. |
required | |
input_args
|
A tuple of input arguments to use as positional defaults. |
()
|
Returns:
| Type | Description |
|---|---|
|
The result of calling |
Source code in py_misc_utils/inspect_utils.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
get_fn_kwargs(args, func, prefix=None, roffset=None)
¶
Extracts keyword arguments for a function from an args dict.
Uses inspect.getfullargspec to determine parameter names and defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
A dict mapping argument names to values. |
required | |
func
|
The function whose signature is used. |
required | |
prefix
|
An optional prefix string for argument names (e.g. 'config.'). |
None
|
|
roffset
|
An offset index for required arguments. |
None
|
Returns:
| Type | Description |
|---|---|
|
A dict of parameter names to values, using defaults where available. |
Source code in py_misc_utils/inspect_utils.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
get_defaulted_params(func)
¶
Returns parameters of a function that have default values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function to inspect. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of |
Source code in py_misc_utils/inspect_utils.py
362 363 364 365 366 367 368 369 370 371 372 373 374 | |
parent_locals(level=0)
¶
Returns the locals dict of a parent frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Additional levels to go up (default 0 means the immediate caller's parent). |
0
|
Returns:
| Type | Description |
|---|---|
|
The |
Source code in py_misc_utils/inspect_utils.py
377 378 379 380 381 382 383 384 385 386 387 388 389 | |
parent_globals(level=0)
¶
Returns the globals dict of a parent frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Additional levels to go up (default 0 means the immediate caller's parent). |
0
|
Returns:
| Type | Description |
|---|---|
|
The |
Source code in py_misc_utils/inspect_utils.py
392 393 394 395 396 397 398 399 400 401 402 403 404 | |
parent_coords(level=0)
¶
Returns the filename and line number of a parent frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Additional levels to go up (default 0 means the immediate caller's parent). |
0
|
Returns:
| Type | Description |
|---|---|
|
A tuple |
Source code in py_misc_utils/inspect_utils.py
407 408 409 410 411 412 413 414 415 416 417 418 419 | |
class_slots(cls)
¶
Returns all slot names defined across a class's MRO.
Note that slot names reappear once per ancestor defining them: a slotless subclass does not hide the slots of its parents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
A class object. |
required |
Returns:
| Type | Description |
|---|---|
|
A tuple of slot name strings from all classes in |
|
|
empty for classes without slots anywhere in their hierarchy. |
Source code in py_misc_utils/inspect_utils.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |