键入警告:pylance“str”与“list[Literal]”不兼容

问题描述 投票:0回答:0

在这种情况下,为什么 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 类型! 我应该忽略还是应该在某个地方提出问题?

python python-3.x typing python-dataclasses pylance
© www.soinside.com 2019 - 2024. All rights reserved.