英文:
Argument of type "tuple[str, str]" cannot be assigned to parameter "__key" of type "tuple[Literal, Literal]"
问题
我是Python新手,用Python解决Advent of Code问题时遇到了问题。我在解决第二天的问题时遇到了一个问题。
我定义了一个将元组映射到常量值的字典。具体来说,我定义了score_table
如下:
score_table = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
但是后来,当我尝试以以下方式访问字典时,我收到了来自pyright的错误:
for line in f:
u, v = line.split()
ans = ans + score[v] + score_table[(u, v)]
错误说
类型为"tuple[str, str]"的参数不能赋给函数"__getitem__"中类型为"tuple[Literal['A'], Literal['X']] | tuple[Literal['A'], Literal['Y']] | tuple[Literal['A'], Literal['Z']] | tuple[Literal['B'], Literal['X']] | tuple[Literal['B'], Literal['Y']] | tuple[Literal['B'], Literal['Z']] | tuple[Literal['C'], Literal['X']] | tuple[Literal['C'], Literal['Y']] | tuple[Literal['C'], Literal['Z']]"的参数"__key"
类型"tuple[str, str]"不能赋给类型"tuple[Literal['A'], Literal['X']] | tuple[Literal['A'], Literal['Y']] | tuple[Literal['A'], Literal['Z']] | tuple[Literal['B'], Literal['X']] | tuple[Literal['B'], Literal['Y']] | tuple[Literal['B'], Literal['Z']] | tuple[Literal['C'], Literal['X']] | tuple[Literal['C'], Literal['Y']] | tuple[Literal['C'], Literal['Z']]"
"tuple[str, str]"与"tuple[Literal['A'], Literal['X']]的类型不兼容"
Tuple条目1的类型不正确
"str"不能赋给类型"Literal['A']"
"tuple[str, str]"与"tuple[Literal['A'], Literal['Y']]的类型不兼容"
Tuple条目1的类型不正确
"str"不能赋给类型"Literal['A']"
"tuple[str, str]"与"tuple[Literal['A'], Literal['Z']]的类型不兼容"(reportGeneralTypeIssues)
1个错误,0个警告,0个信息
0.594秒完成
然而,我能够运行代码,甚至得到了正确的答案。我做错了什么吗?
if __name__ == "__main__":
with open("day2.in", "r", encoding="utf-8") as f:
# A, X = rock, B, Y = paper, C, Z = scissors
score = {'X': 1, 'Y': 2, 'Z': 3}
score_table = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
ans = 0
for line in f:
u, v = line.split()
ans = ans + score[v] + score_table[(u, v)]
print(ans)
英文:
I'm a python newbie and I have been solving Advent of Code questions with python. However, I encountered a problem when solving day 2's question.
I defined a dict
that maps a tuple
to a constant value. Specifically, I defined score_table
to be:
score_table = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
But later on, when I try to access the dict the following way, I received an error from pyright:
for line in f:
u, v = line.split()
ans = ans + score[v] + score_table[(u, v)]
The error says
Argument of type "tuple[str, str]" cannot be assigned to parameter "__key" of type "tuple[Literal['A'], Literal['X']] | tuple[Literal['A'], Literal['Y']] | tuple[Literal['A'], Literal['Z']] | tuple[Literal['B'], Literal['X']] | tuple[Literal['B'], Literal['Y']] | tuple[Literal['B'], Literal['Z']] | tuple[Literal['C'], Literal['X']] | tuple[Literal['C'], Literal['Y']] | tuple[Literal['C'], Literal['Z']]" in function "__getitem__"
  Type "tuple[str, str]" cannot be assigned to type "tuple[Literal['A'], Literal['X']] | tuple[Literal['A'], Literal['Y']] | tuple[Literal['A'], Literal['Z']] | tuple[Literal['B'], Literal['X']] | tuple[Literal['B'], Literal['Y']] | tuple[Literal['B'], Literal['Z']] | tuple[Literal['C'], Literal['X']] | tuple[Literal['C'], Literal['Y']] | tuple[Literal['C'], Literal['Z']]"
    "tuple[str, str]" is incompatible with "tuple[Literal['A'], Literal['X']]"
      Tuple entry 1 is incorrect type
        "str" cannot be assigned to type "Literal['A']"
    "tuple[str, str]" is incompatible with "tuple[Literal['A'], Literal['Y']]"
      Tuple entry 1 is incorrect type
        "str" cannot be assigned to type "Literal['A']"
    "tuple[str, str]" is incompatible with "tuple[Literal['A'], Literal['Z']]" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations
Completed in 0.594sec
However, I was able to run the code, and even got the question correct. Is there something that I did wrong?
For reference, below is my entire program
if __name__ == "__main__":
with open("day2.in", "r", encoding="utf-8") as f:
# A, X = rock, B, Y = paper, C, Z = scissors
score = {'X': 1, 'Y': 2, 'Z': 3}
score_table = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
ans = 0
for line in f:
u, v = line.split()
ans = ans + score[v] + score_table[(u, v)]
print(ans)
Thank you in advance!
答案1
得分: 0
As juanpa.arrivillaga 指出,当我明确定义score_table
的类型如下时,pyright不会显示任何错误:
score_table : dict[tuple[str, str], int] = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
英文:
As juanpa.arrivillaga pointed out, pyright doesn't show any errors after I explicitly define the type of score_table
in the following way:
score_table : dict[tuple[str, str], int] = {('A', 'X'): 3, ('A', 'Y'): 6, ('A', 'Z'): 0,
('B', 'X'): 0, ('B', 'Y'): 3, ('B', 'Z'): 6,
('C', 'X'): 6, ('C', 'Y'): 0, ('C', 'Z'): 3}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论