(任何)python模块的类型提示是什么?

(任何)python模块的类型提示是什么?,第1张

概述我想为模块添加( Python3)类型提示(类’module’).输入包不提供一个,而types.ModuleType()是一个返回特定名称的模块对象的构造函数. 例: import typesdef foo(module: types.ModuleType): pass 至少在PyCharm中导致“在types.pyi中找不到参考ModuleType”. and types.Module 我想为模块添加( Python3)类型提示(类’module’).输入包不提供一个,而types.ModuleType()是一个返回特定名称的模块对象的构造函数.

例:

import typesdef foo(module: types.ModuleType):   pass

至少在PyCharm中导致“在types.pyi中找不到参考ModuleType”.

解决方法

and types.ModuleType() is a constructor.

那没关系. types.ModuleType仍然是对类型的引用,就像str和int一样.不需要通用的Module [typehint]注释,因此types.ModuleType非常适合您在这里使用的内容.

例如,官方Python typeshed project提供了type hint annotation for sys.modules

from types import FrameType,ModuleType,TracebackType# ...modules: Dict[str,ModuleType]

不要被这里的名字搞糊涂; types.ModuleType是对模块类型的引用.它不是一个单独的工厂功能或其他东西. CamelCase名称遵循该模块的约定,并且您使用该引用,因为类型对象不能作为内置函数使用.类型模块assigns the value of type(sys) to the name.

如果PyCharm在查找types.ModuleType存根时遇到问题,那么这就是PyCharm本身的问题(一个BUG),或者当前捆绑的存根是过时的,或者你使用了一个不完整的类型的存根集.请参阅how to use custom stubs上的PyCharm文档以提供新的一套.

如果这不起作用,可能是PyCharm中处理导出类型提示概念的错误.当前defines the ModuleType type hints in a separate module,当前是imported into the types.pyi stubfile,使用from模块导入名称作为名称语法. PEP 484声明导入的类型提示不是存根的一部分,除非您使用as语法:

Modules and variables imported into the stub are not consIDered exported from the stub unless the import uses the import ... as ... form or the equivalent from ... import ... as ... form.

可能是PyCharm还没有正确处理这种情况.

总结

以上是内存溢出为你收集整理的(任何)python模块的类型提示是什么?全部内容,希望文章能够帮你解决(任何)python模块的类型提示是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1192199.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存