How can I keep imports lightweight and still properly type annotate?
问题 Tensorflow is a super heavy import. I want to import it only when it's needed. However, I have a model loading function like this: from typing import Dict, Any from keras.models import Model # Heavy import! Takes 2 seconds or so! # Model loading is a heavy task. Only do it once and keep it in memory model = None # type: Optional[Model] def load_model(config: Dict[str, Any], shape) -> Model: """Load a model.""" if globals()['model'] is None: globals()['model'] = create_model(wili.n_classes,