TL;DR:从今天(2019 年)开始,在 Python 3.7+ 中,您必须使用“未来”语句 from __future__ import annotations 启用此功能。
(from __future__ import annotations 启用的行为可能 在 Python 的未来版本中成为默认值,was going 将在 Python 3.10 中成为默认值。但是,3.10 中的更改 was reverted在最后一刻,现在可能根本不会发生。)
在 Python 3.6 或更低版本中,您应该使用字符串。
我猜你遇到了这个异常:
NameError: name 'Position' is not defined
这是因为必须先定义Position,然后才能在注释中使用它,除非您使用启用了PEP 563 更改的Python。
Python 3.7+:from __future__ import annotations
Python 3.7 引入了PEP 563: postponed evaluation of annotations。使用 future 语句 from __future__ import annotations 的模块将自动将注解存储为字符串:
from __future__ import annotations
class Position:
def __add__(self, other: Position) -> Position:
...
这已计划成为 Python 3.10 中的默认设置,但现在已推迟此更改。由于 Python 仍然是一种动态类型语言,因此在运行时不会进行类型检查,因此键入注释应该不会影响性能,对吧?错误的!在 Python 3.7 之前,打字模块曾经是 one of the slowest python modules in core,所以对于涉及导入 typing 模块的代码,升级到 3.7 时会看到 up to 7 times increase in performance。
Python
According to PEP 484,您应该使用字符串而不是类本身:
class Position:
...
def __add__(self, other: 'Position') -> 'Position':
...
如果您使用 Django 框架,这可能很熟悉,因为 Django 模型也使用字符串进行前向引用(外键定义,其中外部模型为 self 或尚未声明)。这应该适用于 Pycharm 和其他工具。
来源
PEP 484 和 PEP 563 的相关部分,省去你的旅行:
当类型提示包含尚未定义的名称时,该定义可以表示为字符串文字,稍后再解析。
这种情况经常发生的情况是定义容器类,其中被定义的类出现在某些方法的签名中。例如,下面的代码(一个简单的二叉树实现的开始)不起作用:
class Tree:
def __init__(self, left: Tree, right: Tree):
self.left = left
self.right = right
为了解决这个问题,我们写:
class Tree:
def __init__(self, left: 'Tree', right: 'Tree'):
self.left = left
self.right = right
字符串字面量应该包含一个有效的 Python 表达式(即 compile(lit, '', 'eval') 应该是一个有效的代码对象),并且一旦模块完全加载,它应该不会出错。计算它的本地和全局命名空间应该是相同的命名空间,相同函数的默认参数将在其中计算。
和 PEP 563:
在 Python 3.10 中,函数和变量注释将不再在定义时进行评估。相反,字符串形式将保留在相应的 __annotations__ 字典中。静态类型检查器不会发现行为差异,而在运行时使用注释的工具将不得不执行延迟评估。
...
可以使用以下特殊导入从 Python 3.7 开始启用上述功能:
from __future__ import annotations
你可能想做的事情
A.定义一个虚拟Position
在类定义之前,放置一个虚拟定义:
class Position(object):
pass
class Position(object):
...
这将摆脱NameError,甚至看起来还可以:
>>> Position.__add__.__annotations__
{'other': __main__.Position, 'return': __main__.Position}
是吗?
>>> for k, v in Position.__add__.__annotations__.items():
... print(k, 'is Position:', v is Position)
return is Position: False
other is Position: False
B. Monkey-patch 以添加注释:
您可能想尝试一些 Python 元编程魔法并编写一个装饰器
对类定义进行猴子补丁以添加注释:
class Position:
...
def __add__(self, other):
return self.__class__(self.x + other.x, self.y + other.y)
装饰者应该负责相当于这个:
Position.__add__.__annotations__['return'] = Position
Position.__add__.__annotations__['other'] = Position
至少看起来是对的:
>>> for k, v in Position.__add__.__annotations__.items():
... print(k, 'is Position:', v is Position)
return is Position: True
other is Position: True
可能太麻烦了。