如何在 python 3.6 中使用类型提示? [英] How to use type hints in python 3.6?

查看:39
本文介绍了如何在 python 3.6 中使用类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到python 3.5和python 3.6添加了很多关于静态类型检查的功能,所以我尝试了以下代码(在python 3.6,稳定版本中).

I noticed python 3.5 and python 3.6 added a lot of features about static type checking, so I tried with the following code(in python 3.6, stable version).

from typing import List

a: List[str] = []
a.append('a')
a.append(1)
print(a)

令我惊讶的是,python 没有给我错误或警告,尽管 1 被附加到一个应该只包含字符串的 list 中.Pycharm 检测到类型错误并给了我一个警告,但它并不明显并且没有显示在输出控制台中,我担心有时我可能会错过它.我想要以下效果:

What surprised me was that, python didn't give me an error or warning, although 1 was appended to a list which should only contain strings. Pycharm detected the type error and gave me a warning about it, but it was not obvious and it was not shown in the output console, I was afraid sometimes I might miss it. I would like the following effects:

  1. 如果很明显我使用了如上所示的错误类型,请抛出警告或错误.
  2. 如果编译器无法可靠地检查我使用的类型是对还是错,请忽略它.

这可能吗?也许 mypy 可以做到,但我更喜欢使用 python-3.6 样式的类型检查(如 a: List[str])而不是注释样式(像 mypy 中使用的 # type List[str]).我很好奇原生python 3.6中是否有一个开关来实现我上面说的两点.

Is that possible? Maybe mypy could do it, but I'd prefer to use python-3.6-style type checking(like a: List[str]) instead of the comment-style(like # type List[str]) used in mypy. And I'm curious if there's a switch in native python 3.6 to achieve the two points I said above.

推荐答案

这可能吗?也许 mypy 可以做到,但我更喜欢使用 Python-3.6 样式的类型检查(如 a: List[str])而不是注释样式(如 # type:List[str]) 在 mypy 中使用.我很好奇原生python 3.6中是否有一个开关来实现我上面说的两点.

Is that possible? Maybe mypy could do it, but I'd prefer to use Python-3.6-style type checking (like a: List[str]) instead of the comment-style (like # type: List[str]) used in mypy. And I'm curious if there's a switch in native python 3.6 to achieve the two points I said above.

Python 不可能为你做这件事;您可以使用 mypy 进行类型检查(PyCharms 内置检查器也应该这样做).除此之外,mypy不限制你只能输入注释# type List[str],你可以使用变量注解作为你在 Python 3.6 中这样做,所以 a: List[str] 工作得同样好.

There's no way Python will do this for you; you can use mypy to get type checking (and PyCharms built-in checker should do it too). In addition to that, mypy also doesn't restrict you to only type comments # type List[str], you can use variable annotations as you do in Python 3.6 so a: List[str] works equally well.

使用 mypy 原样,因为发布是新鲜的,您需要安装 typed_ast 并使用 - 执行 mypy-fast-parser--python-version 3.6 如 mypy 的文档中所述.这可能很快就会改变,但现在你需要它们来让它顺利运行

With mypy as is, because the release is fresh, you'll need to install typed_ast and execute mypy with --fast-parser and --python-version 3.6 as documented in mypy's docs. This will probably change soon but for now you'll need them to get it running smoothly

更新: --fast-parser--python-version 3.6 现在不需要了.

Update: --fast-parser and --python-version 3.6 aren't needed now.

在你这样做之后,mypy 检测到你的 a: List[str] 上的第二个操作的不兼容性就好了.假设您的文件名为 tp_check.py 并带有语句:

After you do that, mypy detects the incompatibility of the second operation on your a: List[str] just fine. Let's say your file is called tp_check.py with statements:

from typing import List

a: List[str] = []
a.append('a')
a.append(1)
print(a)

使用上述参数运行 mypy(您必须先 pip install -U typed_ast):

Running mypy with the aforementioned arguments (you must first pip install -U typed_ast):

python -m mypy --fast-parser --python-version 3.6 tp_check.py

捕获错误:

tp_check.py:5: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "str"

正如在使用 Python 进行类型提示的许多其他答案中所述mypyPyCharms 的类型检查器是执行验证的,不是 Python 本身.Python 目前不使用此信息,它仅将其存储为元数据并在执行过程中忽略它.

As noted in many other answers on type hinting with Python, mypy and PyCharms' type-checkers are the ones performing the validation, not Python itself. Python doesn't use this information currently, it only stores it as metadata and ignores it during execution.

这篇关于如何在 python 3.6 中使用类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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