有一段与此等效的正确工作代码:
from typing import Optional
class Game:
pass
def select(game: Game) -> Optional[Game]:
vs: list[Game] = []
next_games: list[list[Game, int]] = [[v, 0] for v in vs]
if not next_games:
return None
next_games[0][1] = 0 # Тут
return next_games[0][0]
PyCharm 对着标记线发誓,报错
意外类型:(int, int)可能的类型:(int, Game)(slice, Iterable[Game])
告诉我这意味着什么以及如何解决它。
首先,我发现了一个错误:最初,该方法的第二行
select看起来像这样(并且没有错误):然后我添加了标记的行并将其更改
tuple为list. 类型到底是什么意思,list[Game, int]我还不明白,但我怀疑这样的符号相当于list[Game]. 写是正确的其次,我似乎已经想出了如何破译错误信息本身。为此,请考虑几个示例:
让我们用 替换标记的行
next_games[0] = None,然后错误将变成Unexpected type(s):(int, None)Possible types:(int, list[Game, int])(slice, Iterable[list[Game, int]])。让我们用 替换标记的行
next_games[0][vs[0]] = None,然后错误将变成Unexpected type(s):(Game, None)Possible types:(int, Game)(slice, Iterable[Game])。我是这样解释的:元组在最后一个方括号中包含对象的类型以及等号右边的内容。