【发布时间】:2020-11-25 03:52:57
【问题描述】:
我正在构建一个 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))
【问题讨论】:
-
你的意思是
types.ModuleType? -
模块没有“返回类型”——所以不清楚你的意思。
-
@martineau 大概是这个函数的返回类型,它将动态导入模块。
-
@khelwood 这正是我正在寻找的类型。这将起作用。让它成为一个答案,我会接受它。
-
我很惊讶当我搜索时它没有更容易出现。 :(
标签: python python-3.x python-typing