Mypy 无法推断从列表变量创建的枚举
Mypy fails to infer enums being created from a list variable
可以通过获取可能的成员列表来创建枚举,我就是这样做的:
# example_issue.py
import enum
yummy_foods = ["ham", "cheese"]
foods = enum.Enum("Foods", yummy_foods)
cheese = foods.cheese
这看起来不错,运行良好,但是 mypy returns
example_issue.py:4: error: Enum() expects a string, tuple, list or dict literal as the second argument
example_issue.py:5: error: "Type[foods]" has no attribute "cheese"
Found 2 errors in 1 file (checked 1 source file)
mypy 在这里做什么,为什么它不能遵循 foods
可以在 yummy_foods
中取任何值?
使用变量 yummy_foods
对于 mypy 的静态类型检查来说过于动态,参见 this GitHub issue。
如果您更改代码以生成 Enum
作为:
foods = enum.Enum("Foods", ["ham", "cheese"])
mypy 将能够找出枚举中存在哪些属性。
可以通过获取可能的成员列表来创建枚举,我就是这样做的:
# example_issue.py
import enum
yummy_foods = ["ham", "cheese"]
foods = enum.Enum("Foods", yummy_foods)
cheese = foods.cheese
这看起来不错,运行良好,但是 mypy returns
example_issue.py:4: error: Enum() expects a string, tuple, list or dict literal as the second argument
example_issue.py:5: error: "Type[foods]" has no attribute "cheese"
Found 2 errors in 1 file (checked 1 source file)
mypy 在这里做什么,为什么它不能遵循 foods
可以在 yummy_foods
中取任何值?
使用变量 yummy_foods
对于 mypy 的静态类型检查来说过于动态,参见 this GitHub issue。
如果您更改代码以生成 Enum
作为:
foods = enum.Enum("Foods", ["ham", "cheese"])
mypy 将能够找出枚举中存在哪些属性。