gpt4 book ai didi

python-3.x - 如何让 mypy 提示将 Any 分配给 int

转载 作者:行者123 更新时间:2023-12-05 06:31:41 24 4
gpt4 key购买 nike

mypy --strict 尽职地提示以下代码:

from typing import Any, Dict

def main() -> None:
my_str: str = 'hello'
my_int: int = my_str

if __name__ == "__main__":
main()

通过输出:

error: Incompatible types in assignment (expression has type "str", variable has type "int")

但是下面的代码没有任何错误地被接受:

from typing import Any, Dict

def main() -> None:
my_str: Any = 'hello'
my_int: int = my_str

if __name__ == "__main__":
main()

mypy 是否有选项使其也拒绝第二个示例?

我希望它这样做,因为它也拒绝以下内容:

from typing import Any, Dict, Union

def main() -> None:
my_str: Union[int, str] = 'hello'
my_int: int = my_str

if __name__ == "__main__":
main()

与:

error: Incompatible types in assignment (expression has type "Union[int, str]", variable has type "int")

在我的理解中,Any 只是所有可能类型的 Union

最佳答案

And in my understanding an Any is just the Union of all possible types.

这是不正确的。 Any 是一个escape hatch,它是您希望类型检查器忽略的变量的注解。这当然不是工会。

来自mypy documentation on Any :

A value with the Any type is dynamically typed. Mypy doesn’t know anything about the possible runtime types of such value. Any operations are permitted on the value, and the operations are only checked at runtime. You can use Any as an “escape hatch” when you can’t use a more precise type for some reason.

(粗体强调我的)

它明确涵盖了您的案例:

Any is compatible with every other type, and vice versa. You can freely assign a value of type Any to a variable with a more precise type:

a: Any = None
s: str = ''
a = 2 # OK (assign "int" to "Any")
s = a # OK (assign "Any" to "str")

Declared (and inferred) types are ignored (or erased) at runtime. They are basically treated as comments, and thus the above code does not generate a runtime error, even though s gets an int value when the program is run, while the declared type of s is actually str!

因此,如果您希望类型检查器继续跟踪值的使用方式,那么正确的方法是不使用Any。使用 Union[],就像您在第三个示例中所做的那样,或者重新考虑您的数据结构以允许更好的类型提示。例如,与其使用具有联合值类型的字典,不如考虑使用具有显式字段和每个字段的特定类型的命名元组或数据类。

关于python-3.x - 如何让 mypy 提示将 Any 分配给 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com