in chz/factories.py [0:0]
def from_string(self, factory: str) -> Callable[..., Any]:
"""
If factory=module:fn, import module and return fn.
If factory=fn, look in the default module for a function named fn.
"""
if ":" not in factory:
if self.default_module is None:
raise MetaFromString(
f"No module specified in {factory!r} and no default module specified"
)
if isinstance(self.default_module, str):
module = importlib.import_module(self.default_module)
else:
module = self.default_module
var = factory
else:
module_name, var = factory.split(":", 1)
if module_name != "lambda" and not module_name.startswith("lambda "):
module = _module_from_name(module_name)
else:
import ast
if isinstance(self.default_module, str):
eval_ctx = importlib.import_module(self.default_module)
elif self.default_module is not None:
eval_ctx = self.default_module
else:
eval_ctx = None
try:
# TODO: add docs for this branch
if isinstance(ast.parse(factory).body[0].value, ast.Lambda): # type: ignore[attr-defined]
return eval_in_context(factory, eval_ctx)
except Exception as e:
raise MetaFromString(
f"Could not interpret {factory!r} as a function: {e}"
) from None
raise AssertionError
return _module_getattr(module, var)