列表的不兼容类型SupportsIndex上出现mypy错误。\u imul__

2024-10-01 17:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用__imul__()的定义对list进行子类化

from __future__ import annotations

from typing import SupportsIndex


class CustomList(list):
    def __imul__(self, n: SupportsIndex) -> CustomList:
        return super().__imul__(n)

Python3.9.2的mypy 0.910会引发一个错误,即参数类型不兼容

customlist.py:8: error: Argument 1 to "__imul__" of "list" has incompatible type "SupportsIndex"; expected "int"

这对我来说毫无意义__imul__()绝对接受SupportsIndex参数。根据docs

  1. The value n is an integer, or an object implementing __index__().

这是mypy中的一个bug吗?有没有办法在不添加本地忽略的情况下解决此错误


Tags: fromimportantyping参数定义错误future

热门问题