Open Viicos opened 1 year ago
@Viicos I think adding this to monkeytype/type_checking_imports_transformer.py could solve your problem
@staticmethod def _add_type_checking_import(source_module: Module, if_type_checking: bool = False) -> Module: context = CodemodContext() transformer = MyAddImportsVisitor(context, if_type_checking) transformed_source_module = transformer.transform_module(source_module) return transformed_source_module
When using libcst to add types, we often want to add imports in a conditional
if TYPE_CHECKING
block, to avoid circular imports or improve performance.It would be great to have an extra
if_type_checking: bool
parameter toAddImportsVisitor
, that would add the import in the block (and importTYPE_CHECKING
fromtyping
if it is not already the case).If you think this is a good idea, I may work on this.