首页 > 解决方案 > 如何“键入”随机包裹退货

问题描述

我正在构建一个 API 以允许在我正在处理的应用程序中使用插件。我importlib.import_module用来导入插件。显然我不知道要提前导入哪些模块。有没有办法在我用来进行导入的方法上将返回类型识别为通用模块?

def import_plugin(plugin_name: str) -> <Some generic module type>:
    # conditional tests here...
    return importlib.import_module("plugins.{}".format(plugin_name))

标签: pythonpython-3.xpython-typing

解决方案


types.ModuleType模块的类型在模块中给出types

import types
type(types) is types.ModuleType
# => True

推荐阅读