Get the source code of a class or callable object (e.g.: function, method). First attempts to get the source code using `inspect.getsource`. In a dynamic environment (e.g.: Jupyter, IPython), if this fails, falls back to retrieving the source code from the current interactive shell sessi
(obj)
| 346 | |
| 347 | |
| 348 | def get_source(obj) -> str: |
| 349 | """Get the source code of a class or callable object (e.g.: function, method). |
| 350 | First attempts to get the source code using `inspect.getsource`. |
| 351 | In a dynamic environment (e.g.: Jupyter, IPython), if this fails, |
| 352 | falls back to retrieving the source code from the current interactive shell session. |
| 353 | |
| 354 | Args: |
| 355 | obj: A class or callable object (e.g.: function, method) |
| 356 | |
| 357 | Returns: |
| 358 | str: The source code of the object, dedented and stripped |
| 359 | |
| 360 | Raises: |
| 361 | TypeError: If object is not a class or callable |
| 362 | OSError: If source code cannot be retrieved from any source |
| 363 | ValueError: If source cannot be found in IPython history |
| 364 | |
| 365 | Note: |
| 366 | TODO: handle Python standard REPL |
| 367 | """ |
| 368 | if not (isinstance(obj, type) or callable(obj)): |
| 369 | raise TypeError(f"Expected class or callable, got {type(obj)}") |
| 370 | |
| 371 | inspect_error = None |
| 372 | try: |
| 373 | # Handle dynamically created classes |
| 374 | source = getattr(obj, "__source__", None) or inspect.getsource(obj) |
| 375 | return dedent(source).strip() |
| 376 | except OSError as e: |
| 377 | # let's keep track of the exception to raise it if all further methods fail |
| 378 | inspect_error = e |
| 379 | try: |
| 380 | import IPython |
| 381 | |
| 382 | shell = IPython.get_ipython() |
| 383 | if not shell: |
| 384 | raise ImportError("No active IPython shell found") |
| 385 | all_cells = "\n".join(shell.user_ns.get("In", [])).strip() |
| 386 | if not all_cells: |
| 387 | raise ValueError("No code cells found in IPython session") |
| 388 | |
| 389 | tree = ast.parse(all_cells) |
| 390 | for node in ast.walk(tree): |
| 391 | if isinstance(node, (ast.ClassDef, ast.FunctionDef)) and node.name == obj.__name__: |
| 392 | return dedent("\n".join(all_cells.split("\n")[node.lineno - 1 : node.end_lineno])).strip() |
| 393 | raise ValueError(f"Could not find source code for {obj.__name__} in IPython history") |
| 394 | except ImportError: |
| 395 | # IPython is not available, let's just raise the original inspect error |
| 396 | raise inspect_error |
| 397 | except ValueError as e: |
| 398 | # IPython is available but we couldn't find the source code, let's raise the error |
| 399 | raise e from inspect_error |
| 400 | |
| 401 | |
| 402 | def encode_file_base64(file_path: str) -> str: |
no test coverage detected