Generate function from Python from source code string.
(
name: str,
source: str,
*,
return_type: Any = MISSING,
globals: Dict[str, Any] = None,
locals: Dict[str, Any] = None,
)
| 104 | |
| 105 | |
| 106 | def build_function( |
| 107 | name: str, |
| 108 | source: str, |
| 109 | *, |
| 110 | return_type: Any = MISSING, |
| 111 | globals: Dict[str, Any] = None, |
| 112 | locals: Dict[str, Any] = None, |
| 113 | ) -> Callable: |
| 114 | """Generate function from Python from source code string.""" |
| 115 | assert locals is not None |
| 116 | if return_type is not MISSING: |
| 117 | locals["_return_type"] = return_type |
| 118 | exec(source, globals, locals) # nosec: B102 |
| 119 | obj = locals[name] |
| 120 | obj.__sourcecode__ = source |
| 121 | return cast(Callable, obj) |
| 122 | |
| 123 | |
| 124 | def build_function_source( |