忽略 Python 类型提示

标签 python python-3.x pycharm python-typing

我是 python 新手,正在尝试类型提示,但它们似乎只在某些情况下起作用。它们似乎在属性返回类型上按预期工作,但是当我尝试将整数分配给字符串值(即 self._my_string = 4)时,我没有收到任何问题报告。

class TypeHintTest(object):
    _my_string: str

    def __init__(self):
        self._my_string = 4  # no error

    @property
    def as_int(self) -> int:  
        return self._my_string  # Error : expected int got str

结果对象包含一个 int 值(如预期)。

我使用的是pyCharm 2018.3.2社区版,解释器是3.6

enter image description here

下面的问题似乎类似,但是将构造函数更改为 def __init__(self) -> None 的解决方案并没有改变任何东西。 Python: All type hints errors in subclass constructure seems ignored

最佳答案

类型提示,顾名思义,就是提示。如果您为变量分配不同的类型,Python 不会引发错误。
然而,Pycharm应该说它需要另一种变量类型。

关于忽略 Python 类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020807/

相关文章:

python - 在 Python 异常处理程序中捕获异常的更简洁方法

python - beautifulsoup 检索日期

python - : how-to launch for a standard terminal (to solve an issue with curses)

python - PyCharm 2018 中测试失败的两次回溯

python - Tensorflow 无法在 Windows 和 pycharm 上运行

python - 编写一个包含多个值的字典,将数据存储在 CSV 文件的列中

python - Pandas .str.replace 不替换表中的所有实例

python-3.x - 定义以数字开头的函数名(在 Python 3 中)?

python - 如何用一行代码解释filter和lambda的功能?

python-3.x - 如何使用 Python3.6.4 为 CentOS 6.10 安装 Curses?