【问题标题】:Instantiate python type-hinted list to emptyInstantiate python type-hinted list to empty
【发布时间】:2022-12-01 18:59:44
【问题描述】:

I have some python that does this:

from typing import List, NewType

MultiList = NewType("MultiList", List[List[int]])

def myfunc():
  multi: MultiList = []
  # More stuff here

The code works fine, it's just my IDE (PyCharm) doesn't like the instantiation of multi to an empty list, I get this error:

"Expected type 'MultiList', got 'list[list[int]]' instead"

I mean, a MultiListisa list[list[int]], so I really don't know why it's complaining. Unless it's because the list is empty, but that doesn't make a lot of sense to me either.

It's not the end of the world, the code works just fine, I'd just like to know why it's "wrong", per se.

【问题讨论】:

    标签: python python-typing


    【解决方案1】:

    If you just mean "list of lists of ints", then don't use NewType:

    MultiList = List[List[int]]
    

    【讨论】:

      猜你喜欢
      • 2022-10-18
      • 1970-01-01
      • 2015-02-11
      • 2022-12-05
      • 2020-05-11
      • 2022-12-15
      • 2014-12-20
      • 2012-02-15
      • 2020-07-05
      相关资源
      最近更新 更多