【问题标题】:Why does PyCharm incorrectly guess type for a list of dict_items?为什么 PyCharm 会错误地猜测 dict_items 列表的类型?
【发布时间】:2020-05-10 20:43:09
【问题描述】:

对于这段代码:

from typing import Dict, List, Tuple, Any


def test(d: Dict[int, Any]):
    item_list: List[Tuple[int, Any]] = list(d.items())

出于某种原因,PyCharm 猜测 item_list 将是 List[int] 类型:

我是否遗漏了什么,或者这是一个很容易推断类型的案例,而 PyCharm 在这里错了?

【问题讨论】:

  • 确实很奇怪。似乎是一个错误。 = [pair for pair in d.items()] 可以正常工作。
  • @trincot 是的,我就是这么想的。感谢您的确认

标签: python pycharm typing


【解决方案1】:

您的代码是正确的。这是 PyCharm 中的一个错误。 您可以在此处的错误跟踪器上跟踪其状态: Incorrect type inference of dict.items()。考虑对其进行投票,以便尽快修复。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2016-01-04
    • 2013-03-22
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多