A decorator that creates a wrapper around `fn` that annotates the call with annotations given by the supplied keyword arguments. These annotations can be retrieved from a traceback using `walk_annotated_tb()`. Example: ``` fn = lambda x: x annotated_fn = annotat
(**aux)
| 265 | # function, there is no way of getting the function object itself that correctly handles anonymous |
| 266 | # functions. Otherwise, we could just set an attribute on the function. |
| 267 | def annotate_stack(**aux) -> Callable: |
| 268 | """A decorator that creates a wrapper around `fn` that annotates the call with annotations given |
| 269 | by the supplied keyword arguments. |
| 270 | |
| 271 | These annotations can be retrieved from a traceback using `walk_annotated_tb()`. |
| 272 | |
| 273 | Example: |
| 274 | ``` |
| 275 | fn = lambda x: x |
| 276 | annotated_fn = annotate_stack(my_annotation="Hello, world!")(fn) |
| 277 | ``` |
| 278 | |
| 279 | Args: |
| 280 | aux: The auxiliary data with which to annotate calls to the wrapped function. |
| 281 | |
| 282 | Returns: |
| 283 | A wrapped function that calls `fn` after annotating the call with `aux`. |
| 284 | """ |
| 285 | |
| 286 | if not is_stack_summary_enabled(): |
| 287 | return lambda fn: fn |
| 288 | |
| 289 | def decorator(fn: Callable) -> Callable: |
| 290 | @functools.wraps(fn) |
| 291 | def stack_annotation_wrapper(*args, **kwargs): |
| 292 | # Retrieved via `frame.f_locals["aux_"]` by `_walk_stack` below. |
| 293 | aux_ = aux # noqa: F841 |
| 294 | return fn(*args, **kwargs) |
| 295 | |
| 296 | return stack_annotation_wrapper |
| 297 | |
| 298 | return decorator |
| 299 | |
| 300 | |
| 301 | def _is_annotation_frame(frame: types.FrameType) -> bool: |