Mic*_*wen 277 python typing pycharm python-3.x python-3.5
我在python 3中有以下代码:
class Position:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __add__(self, other: Position) -> Position:
return Position(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
但是我的编辑器(PyCharm)说无法解析引用位置(在_add__方法中).我该如何指定我希望返回类型是类型__add__?
编辑:我认为这实际上是一个PyCharm问题.它实际上使用其警告中的信息和代码完成

但如果我错了,请纠正我,并需要使用其他语法.
Pau*_*ine 408
TL; DR:如果您使用的是Python 4.0,它就可以了.截至今天(2019年)的3.7+,您必须使用future语句(from __future__ import annotations)来启用此功能- 对于Python 3.6或更低版本,使用字符串.
我猜你有这个例外:
NameError: name 'Position' is not defined
Run Code Online (Sandbox Code Playgroud)
这是因为Position必须先定义才能在注释中使用它,除非您使用的是Python 4.
from __future__ import annotationsPython 3.7引入了PEP 563:推迟评注注释.使用future语句的模块from __future__ import annotations将自动将注释存储为字符串:
from __future__ import annotations
class Position:
def __add__(self, other: Position) -> Position:
...
Run Code Online (Sandbox Code Playgroud)
这计划成为Python 4.0中的默认设置.由于Python仍然是动态类型语言,因此在运行时不进行类型检查,因此键入注释应该不会对性能产生影响,对吧?错误!在python 3.7之前,输入模块曾经是核心中最慢的python模块之一,所以如果你升级到3.7时你import typing会看到性能提升7倍.
根据PEP 484,您应该使用字符串而不是类本身:
class Position:
...
def __add__(self, other: 'Position') -> 'Position':
...
Run Code Online (Sandbox Code Playgroud)
如果你使用Django框架,这可能是熟悉的,因为Django模型也使用字符串作为前向引用(外键模型已经self或未被声明的外键定义).这应该适用于Pycharm和其他工具.
PEP 484和PEP 563的相关部分,为您带来免费旅行:
前向参考
当类型提示包含尚未定义的名称时,该定义可以表示为字符串文字,稍后要解决.
这种情况通常发生的情况是容器类的定义,其中定义的类出现在某些方法的签名中.例如,以下代码(简单二叉树实现的开始)不起作用:
class Tree:
def __init__(self, left: Tree, right: Tree):
self.left = left
self.right = right
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我们写道:
class Tree:
def __init__(self, left: 'Tree', right: 'Tree'):
self.left = left
self.right = right
Run Code Online (Sandbox Code Playgroud)
字符串文字应该包含一个有效的Python表达式(即,编译(点亮,'','eval')应该是一个有效的代码对象)并且一旦模块完全加载就应该评估没有错误.在其中计算它的本地和全局命名空间应该是相同的命名空间,在该命名空间中将评估同一函数的默认参数.
和PEP 563:
在Python 4.0中,函数和变量注释将不再在定义时进行评估.相反,字符串形式将保留在相应的
__annotations__字典中.静态类型检查器将看不到行为上的差异,而在运行时使用注释的工具将不得不执行推迟的评估....
可以使用以下特殊导入从Python 3.7开始启用上述功能:
from __future__ import annotations
Run Code Online (Sandbox Code Playgroud)
Position在类定义之前,放置一个虚拟定义:
class Position(object):
pass
class Position(object):
...
Run Code Online (Sandbox Code Playgroud)
这将摆脱,NameError甚至看起来可以:
>>> Position.__add__.__annotations__
{'other': __main__.Position, 'return': __main__.Position}
Run Code Online (Sandbox Code Playgroud)
但是吗?
>>> for k, v in Position.__add__.__annotations__.items():
... print(k, 'is Position:', v is Position)
return is Position: False
other is Position: False
Run Code Online (Sandbox Code Playgroud)
您可能想尝试一些Python元编程魔术并编写装饰器来修补类定义以添加注释:
class Position:
...
def __add__(self, other):
return self.__class__(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
装饰者应该负责相当于:
Position.__add__.__annotations__['return'] = Position
Position.__add__.__annotations__['other'] = Position
Run Code Online (Sandbox Code Playgroud)
至少看起来是对的:
>>> for k, v in Position.__add__.__annotations__.items():
... print(k, 'is Position:', v is Position)
return is Position: True
other is Position: True
Run Code Online (Sandbox Code Playgroud)
可能太麻烦了.
如果您使用的是3.6或更低版本,请使用包含类名的字符串文字,在3.7中使用from __future__ import annotations它就可以了.
che*_*ner 201
在Python 3.11中实现的PEP 673添加了Self类型。
from typing import Self
class Position:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __add__(self, other: Self) -> Self:
return type(self)(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
返回Self通常是一个好主意,但是您必须返回与 相同类型的对象self,这意味着调用type(self)而不是Position。
对于旧版本的 Python(当前为 3.7 及更高版本),请使用该typing-extensions包。其目的之一是
允许在较旧的 Python 版本上使用新类型系统功能。例如,
typing.TypeGuard它是 Python 3.10 中的新功能,但typing_extensions也允许使用早期 Python 版本的用户使用它。
然后你只需导入 from而typing_extensions不是typing例如from typing_extensions import Self。
Yvo*_*PIS 16
当可以接受基于字符串的类型提示时,__qualname__也可以使用该项目。它保存类的名称,并且在类定义的主体中可用。
class MyClass:
@classmethod
def make_new(cls) -> __qualname__:
return cls()
Run Code Online (Sandbox Code Playgroud)
通过这样做,重命名类并不意味着修改类型提示。但我个人不希望聪明的代码编辑器能很好地处理这种形式。
jsb*_*eno 12
在解析类主体本身时,"位置"这个名称是不可用的.我不知道你是如何使用类型声明的,但Python的PEP 484 - 这是大多数模式应该使用的,如果使用这些打字提示说你可以简单地将名称作为字符串放在这一点:
def __add__(self, other: 'Position') -> 'Position':
return Position(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
检查https://www.python.org/dev/peps/pep-0484/#forward-references - 符合的工具将知道从那里打开类名并使用它.(总是很重要的是记住,Python语言本身并没有对这些注释做任何事情 - 它们通常用于静态代码分析,或者可以在运行时具有用于类型检查的库/框架 - 但是你必须明确地设置它)
将类型指定为字符串很好,但总是给我一点点,我们基本上绕过了解析器.所以你最好不要拼写这些文字字符串中的任何一个:
def __add__(self, other: 'Position') -> 'Position':
return Position(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
稍微有点变化就是使用绑定的typevar,至少在声明typevar时你必须只写一次字符串:
from typing import TypeVar
T = TypeVar('T', bound='Position')
class Position:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __add__(self, other: T) -> T:
return Position(self.x + other.x, self.y + other.y)
Run Code Online (Sandbox Code Playgroud)
如果您只关心修复NameError: name 'Position' is not defined,则可以将类名指定为字符串:
def __add__(self, other: 'Position') -> 'Position':
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用 Python 3.7 或更高版本,请将以下行添加到代码顶部(就在其他导入之前)
from __future__ import annotations
Run Code Online (Sandbox Code Playgroud)
但是,如果您还希望它适用于子类,并返回特定的子类,则需要使用一个Generic类,通过定义一个TypeVar.
稍微不常见的是,TypeVar绑定到 的类型self。基本上,这个打字提示告诉类型检查器的返回类型__add__()和copy()是同一类型self。
from __future__ import annotations
from typing import TypeVar
T = TypeVar('T', bound=Position)
class Position:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
def __add__(self: T, other: Position) -> T:
return type(self)(self.x + other.x, self.y + other.y)
def copy(self: T) -> T:
return type(self)(self.x, self.y)
Run Code Online (Sandbox Code Playgroud)
一世 ??保罗的回答
但是,关于与 self 相关的类型提示继承有一点需要说明,即如果您通过使用类名的文字复制粘贴作为字符串来输入提示,那么您的类型提示将不会以正确或一致的方式。
对此的解决方案是通过将类型提示放在函数本身的返回上来提供返回类型提示。
? 例如,执行以下操作:
class DynamicParent:
def func(self):
# roundabout way of returning self in order to have inherited type hints of the return
# /sf/answers/4545728491/
_self:self.__class__ = self
return _self
Run Code Online (Sandbox Code Playgroud)
? 而不是这样做:
class StaticParent:
def func(self) -> 'StaticParent':
return self
Run Code Online (Sandbox Code Playgroud)
以下是您想通过回旋处进行类型提示的原因?上面显示的方式
class StaticChild(StaticParent):
pass
class DynamicChild(DynamicParent):
pass
static_child = StaticChild()
dynamic_child = DynamicChild()
Run Code Online (Sandbox Code Playgroud)
? dynamic_child屏幕截图显示在引用 self 时类型提示工作正常:
? static_child截图显示类型提示错误地指向父类,即类型提示没有随着继承正确改变;这是static因为它总是指向父母,即使它应该指向孩子
| 归档时间: |
|
| 查看次数: |
36000 次 |
| 最近记录: |