adaptix icon indicating copy to clipboard operation
adaptix copied to clipboard

Add ``if TYPE_CHECKING:`` evaler

Open zhPavel opened this issue 1 year ago • 1 comments

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 avatar Jun 08 '24 19:06 zhPavel

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)

zhPavel avatar Jun 08 '24 19:06 zhPavel

Implemented at #331

zhPavel avatar Aug 29 '24 19:08 zhPavel