給定以下 Python 檔案:
class SomeValue:
def __init__(self, value: int):
self.value = value
some_values = sorted([SomeValue(3), SomeValue(4)], lambda v: v.value)
在此運行mypy時,我得到
demo.py:5: error: No overload variant of "sorted" matches argument types "List[SomeValue]", "Callable[[Any], Any]"
demo.py:5: note: Possible overload variants:
demo.py:5: note: def [SupportsLessThanT <: SupportsLessThan] sorted(Iterable[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> List[SupportsLessThanT]
demo.py:5: note: def [_T] sorted(Iterable[_T], *, key: Callable[[_T], SupportsLessThan], reverse: bool = ...) -> List[_T]
Found 1 error in 1 file (checked 1 source file)
我真的不明白是什么讓人mypy不開心。
uj5u.com熱心網友回復:
嘗試:
some_values = sorted([SomeValue(3), SomeValue(4)], key=lambda v: v.value)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/444765.html
上一篇:將字串排序為雙精度
