INNER CODE UNIT · Python
get_init_self_annotations
DogsTailFarmer/martin-binance · martin_binance/backup.py:295
def get_init_self_annotations(strategy_instance) -> Dict[str, str]:
"""
AST Parser with textwrap protection.
Traverses the class hierarchy and extracts all 'self.attr: Type' definitions
directly from the source code of __init__ methods.
"""
annotations = {}
for cls in strategy_instance.__class__.__mro__:
if "__init__" in cls.__dict__:
try:
raw_source = inspect.getsource(cls.__init__)
source = textwrap.dedent(raw_source)
tree = ast.parse(source)
for node in ast.walk(tree):
if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Attribute) \
and isinstance(node.target.value, ast.Name) and node.target.value.id == "self":