使用打字时初始化变量的正确方法是什么

What is the correct way of initialising variables when using typing

我们公司开始使用 Pythons Typing,因为它在较新的 Python3 版本中可用。 我们正在使用 "mypy" linter 来帮助解决脚本中的输入错误。 我在脚本头初始化空变量时出现问题,例如:

VARIABLE_NAME: str = None

因为稍后会设置,我目前的知识是你应该用 None.

初始化这些变量

mypy 认为这是不正确的并显示以下错误:

Incompatible types in assignment (expression has type "None", variable has type "str")mypy(error)

现在我想知道。 我应该改为用空字符串初始化变量吗? 我不确定这里的最佳做法是什么。

我相信这会写成:

from typing import Optional

VARIABLE_NAME: Optional[str] = None