在这种情况下,为什么 pylance 无法确认我正在分配文字
Color = Literal["blue", "green", "white"]
@dataclass
class TestColor:
my_color: Color | list[Color]
color = TestColor(my_color=["blue"])
color.my_color = color.my_color[0]
错误出现在
color.my_color[0]
上,引发 pylance 警告:
"str" is incompatible with "list[Color]"
"str" cannot be assigned to type "Literal['blue']"
"str" cannot be assigned to type "Literal['green']"
"str" cannot be assigned to type "Literal['white']"
但是
color.my_color[0]
的值显然是 Color 类型!
我应该忽略还是应该在某个地方提出问题?