reagento / adaptix

An extremely flexible and configurable data model conversion library.
https://adaptix.readthedocs.io
Apache License 2.0
342 stars 24 forks source link

Add ``if TYPE_CHECKING:`` evaler #288

Open zhPavel opened 4 weeks ago

zhPavel commented 4 weeks ago

Due to cyclic import or some obscure linting rules people use if TYPE_CHECKING: constructs preventing runtime introspections. We can create workaround for this problem executing code under if node

zhPavel commented 4 weeks ago

The implementation can be based on this snippet:

import ast
from types import ModuleType
import inspect
from typing import Sequence

def _is_type_checking_condition(node: ast.expr) -> bool:
    return True  # add actual checking

def _collect_type_checking_only_fragments(module: ast.Module) -> Sequence[ast.stmt]:
    fragments = []
    for stmt in module.body:
        if isinstance(stmt, ast.If) and _is_type_checking_condition(stmt.test):
            fragments.extend(stmt.body)

    return fragments

def eval_type_checking(module: ModuleType) -> None:
    source = inspect.getsource(module)
    type_checking_only_fragments = _collect_type_checking_only_fragments(ast.parse(source))
    code = compile(ast.Module(type_checking_only_fragments), f'<eval_type_checking of {module}>', 'exec')
    namespace = {k: v for k, v in module.__dict__.items()}
    exec(code, namespace)
    for k, v in namespace.items():
        if not hasattr(module, k):
            setattr(module, k, v)