【发布时间】:2021-02-28 04:33:57
【问题描述】:
也许有人可以在这里帮助我。 我一直在为这个pycharm集成的静态类型检查器苦苦挣扎。
一些规格:
- Python 3.7.7
- Windows 10 专业版 x64
我尝试了以下 PyCharm 版本。
- 专业2020.1.3
- 社区 2020.2.3
我试图说明问题。您可以将其复制并粘贴到您的 PyCharm 中以验证问题。
class SpecificCLS:
pass
class CLS:
def __init__(self):
self.integer: int = 0
self.specific_cls: SpecificCLS = SpecificCLS()
def set_im_int(self, value: int):
self.integer = value
def get_im_int(self) -> int:
return self.integer
def get_specific_cls(self) -> SpecificCLS:
return self.specific_cls
def set_specific_cls(self, value: SpecificCLS):
self.specific_cls = value
cls = CLS()
# Example for assigning a class into an integer
cls.integer = SpecificCLS() # PyCharm does NOT show any error/warning that a class is assigned into an variable that is declared as an "int"
cls.set_im_int(SpecificCLS()) # PyCharm recognise an error (underlined red): Expected type 'int', got 'SpecificCLS' instead
# Example for assigning an integer into a class
cls.specific_cls = 1 # PyCharm does NOT show any error/warning that a class is assigned into an variable that is declared as an "int"
cls.set_specific_cls(1) # PyCharm recognise an error (underlined red): Expected type 'SpecificCLS', got 'int' instead
如果您查看这一行,您会注意到没有显示错误。
cls.integer = SpecificCLS() # PyCharm does NOT show any error/warning that a class is assigned into an variable that is declared as an "int"
如果我们使用 setter 方法来赋值,pycharm 会正确识别错误的赋值。
cls.set_im_int(SpecificCLS()) # PyCharm recognise an error (underlined red): Expected type 'int', got 'SpecificCLS' instead
那么谁能告诉我为什么 setter 的类型检查工作得很好,但其他分配却不行?
到目前为止,我一直使用 getter/setter 来验证我是否会为彼此分配正确的类型。因此我用'__'标记了私有字段,这样没有人可以直接更改状态。 由于我已经从 python 3.5.x 更新到 3.7.x,我认为我可以删除这个样板代码。我想要那种静态类型检查功能,但我不想强迫我一直使用 getter/setter。
感谢您的帮助。
最好的问候
2020 年 11 月 18 日更新:12:00 很抱歉这个误导性的例子。我试图编辑剪辑并希望问题现在很清楚。我还尝试更详细地解释我在 pycharm 的执行/性能中看到的问题。我还添加了更新的屏幕截图。
【问题讨论】:
标签: python pycharm type-hinting