NEWS

Recovers clean, parseable source text from Python function objects, including nested and interactive definitions.

On this page 3 sections

Tools that analyze or rewrite Python code often receive a live function object but need the readable source text that created it. Finding that text is not always straightforward: a function typed in an interactive shell may not have a normal file, and a function nested inside a class or another function carries indentation that prevents it from being parsed on its own.

inspect.getsource() is the standard solution; dill and sourceinspect cover more interactive environments. Their APIs and output differ, leaving every AST or CST consumer to choose fallbacks and normalize indentation.

getsources is a small Python library that provides one consistent way to recover that text and make it ready for parsing. It tries Python’s standard inspect module first, uses dill when needed, and can remove indentation inherited from surrounding code. The release is available on GitHub.

Source retrieval and normalization

getsource(function) tries inspect.getsource() first and falls back to dill.source.getsource() after OSError, including for definitions stored in a classic REPL history.

getclearsource(function) removes indentation inherited from an enclosing class or function while preserving indentation inside the function body.

from getsources import getclearsource

class Parser:
    @staticmethod
    def parse(value):
        return int(value)

print(getclearsource(Parser.parse))
#> def parse(value):
#>     return int(value)

The returned text can now be parsed without reconstructing the enclosing class.

The library handles functions written in Python, including methods and lambdas—not modules, classes, built-in functions, or every object that can be called like a function. A lambda may still be returned with its surrounding statement, such as handler = lambda value: value + 1.

Choosing a source-recovery tool

For ordinary file-backed code, textwrap.dedent(inspect.getsource(function)) may be all a project needs. inspect.getsourcelines() also returns line numbers and supports more kinds of inspectable objects, while direct use of dill exposes more recovery controls.

sourceinspect covers more interactive shells. executing and stack-data are better when inspection begins with a frame or traceback; cloudpickle moves behavior between processes instead of recovering readable source.

Interactive recovery cannot always be guaranteed. Dynamic exec or eval code may have no trustworthy source, matching a lambda to shell history can be ambiguous, and a file on disk may have changed since its function was loaded. getsources recovers available source text; it does not reconstruct source from Python’s compiled representation.

Intended use

The package gives AST- and CST-based libraries one source API for functions from modules, nested scopes, or a classic REPL.