Python类型提示语法如何/为何起作用?

Mar*_*oma 18 python type-hinting pep python-3.x

我刚看到PEP 484中的以下示例:

def greeting(name: str) -> str:
    return 'Hello ' + name

print(greeting('Martin'))
print(greeting(1))
Run Code Online (Sandbox Code Playgroud)

正如所料,这在Python 2中不起作用:

  File "test.py", line 1
    def greeting(name: str) -> str:
                     ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

但是,它适用于Python 3:

Hello Martin
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    print(greeting(1))
  File "test.py", line 2, in greeting
    return 'Hello ' + name
TypeError: Can't convert 'int' object to str implicitly
Run Code Online (Sandbox Code Playgroud)

这是出乎意料的.它还没有真正检查类型,正如您可以看到以下示例(它运行,但不会抛出异常):

def greeting(name: str) -> int:
    return 'Hello ' + name

print(greeting('Martin'))
Run Code Online (Sandbox Code Playgroud)

似乎在:必须是函数的名称之后,但函数似乎被忽略了:

def aha(something):
    print("aha")
    return something+"!"

def greeting(name: aha, foo) -> int:
    return 'Hello ' + name + foo

print(greeting('Martin', 'ad'))
Run Code Online (Sandbox Code Playgroud)

之后的名称似乎也是如此->.

这种类型的提示语法是否使用其他东西(比如Java Modeling语言使用注释)?这个语法是什么时候引入Python的?有没有办法用这个语法进行静态类型检查?它总是打破Python 2的兼容性吗?

Mar*_*ers 30

这里没有类型暗示.你所做的只是提供注释 ; 这些是在PEP 3107中引入的(仅在Python 3中,在Python 2中不支持这一点); 它们允许您注释参数并使用任意信息返回值以供以后检查:

>>> greeting.__annotations__
{'name': <class 'str'>, 'return': <class 'str'>}
Run Code Online (Sandbox Code Playgroud)

否则他们根本不会被咨询.相反,您获得的错误消息是尝试在函数体中连接字符串和整数值:

>>> 'Hello ' + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
Run Code Online (Sandbox Code Playgroud)

这是一个自定义类型错误,旨在提供有关str+ int连接失败原因的其他信息; 它被str.__add__任何类型的方法抛出str:

>>> ''.__add__(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> ''.__add__(True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'bool' object to str implicitly
Run Code Online (Sandbox Code Playgroud)

然后,PEP 484建议使用这些注释来使用其他工具进行实际的静态类型检查,但随着PEP的引入,

虽然这些注释在运行时通过通常的__annotations__属性可用,但在运行时不会进行类型检查.相反,该提议假定存在一个单独的离线类型检查器,用户可以自动运行其源代码.从本质上讲,这种类型的检查器就像一个非常强大的linter.

强调原作.

PEP的灵感来自使用PEP 3107注释的现有工具; 特别是mypy项目(通过采用PEP 484循环回来),还有PyCharm IDEpytypedecl项目中类型提示支持.请参阅Guido van Rossum的原始电子邮件,以启动此项工作以及后续电子邮件.

mypy显然支持Python 2,通过预处理注释,在为您编译源代码之前删除它们,但是否则通常不能使用Python代码在Python 2中工作的语法.

PEP 484还描述了存根文件的使用,它位于常规Python文件旁边; 这些使用.pyi扩展并只包含签名(带有类型提示),使主.py文件注释免费,因此可用于Python 2(前提是您编写了Polyglot Python代码).

  • @moose:PyCharm也支持注释语法,请参阅https://www.jetbrains.com/pycharm/help/type-hinting-in-pycharm.html (2认同)