(任何)python 模块的类型提示是什么? [英] What is the type hint for a (any) python module?

查看:52
本文介绍了(任何)python 模块的类型提示是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为模块(模块"类)添加 (Python3) 类型提示.typing 包没有提供,types.ModuleType() 是一个构造函数,它返回一个特定名称的模块对象.

I would like to add the (Python3) type hint for a module (class 'module'). The typing package doesn't provide one, and types.ModuleType() is a constructor that returns a module object for a specific name.

示例:

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

至少在 PyCharm 中会导致在 types.pyi 中找不到引用 ModuleType".

at least in PyCharm results in "Cannot find reference ModuleType in types.pyi".

请注意,模块类型的 Python 类型 不能回答我的问题,因为它没有解释 ModuleType 既是构造函数又是类型,如下所述.

Note that Python typing for module type doesn't answer my question, as it does not explain that ModuleType is both a constructor as well as a type, as answered below.

推荐答案

types.ModuleType() 是一个构造函数.

没关系.types.ModuleType 仍然是对类型的引用,就像 strint 一样.不需要 generic Module[typehint] 注释,所以 types.ModuleType 正是您需要在这里使用的.

That doesn't matter. types.ModuleType is still a reference to a type, just like str and int are. There is no need for a generic Module[typehint] annotation, so types.ModuleType is exacly what you need to use here.

例如,官方 Python typeshed 项目 提供了一个 输入 sys.modules 的提示注释:>

For example, the official Python typeshed project provides a type hint annotation for sys.modules as:

from types import FrameType, ModuleType, TracebackType

# ...

modules: Dict[str, ModuleType]

不要被这里的名字所迷惑;types.ModuleType 是对模块类型的引用.它不是一个单独的工厂函数或其他东西.CamelCase 名称遵循该模块的约定,您使用该引用是因为该类型对象不能作为内置对象使用.types 模块分配type(sys) 到名称.

Don't be confused by the name here; types.ModuleType is a reference to the module type. It is not a separate factory function or something. The CamelCase name follows the convention of that module, and you use that reference because the type object is not otherwise available as a built-in. The types module assigns the value of type(sys) to the name.

如果 PyCharm 在查找 types.ModuleType 存根时遇到问题,那么这要么是 PyCharm 本身的问题(一个错误),要么是当前捆绑的存根已过时,或者您使用了不完整的排版存根集.请参阅有关如何使用自定义存根 提供全新的套装.

If PyCharm is having issues with finding the types.ModuleType stubs, then that's either a problem with PyCharm itself (a bug), or the stubs currently bundled are outdated, or you used an incomplete typeshed set of stubs. See the PyCharm documentation on how to use custom stubs to provide a fresh set.

如果这不起作用,则可能是 PyCharm 中处理导出类型提示概念的错误.Typeshed 当前定义了Mod> 在单独的模块中输入提示,然后是 使用 from module import name as name 语法导入到 types.pyi 存根文件.PEP 484 指出导入的类型提示不是存根 除非您使用 as 语法:

If that doesn't work, it may be a bug in PyCharm dealing with the concept of exporting type hints. Typeshed currently defines the ModuleType type hints in a separate module, which are then imported into the types.pyi stubfile using the from module import name as name syntax. PEP 484 states that imported type hints are not part of the stub unless you use the as syntax:

导入存根的模块和变量不被视为从存根导出,除非导入使用 import ... as ... 形式或等效的 from ... import ... 作为 ... 形式.

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 尚未正确处理此类情况.

It may be that PyCharm doesn't yet correctly handle such cases.

这篇关于(任何)python 模块的类型提示是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆