How to implement overload of `__setitem__` for `MutableSequence`?
オープン
まだ誰も着手していません。
feature
priority-1-normal
topic-overloads
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
I'm trying to implement my own MutableSequence but struggling with the proper way to implement the @overload
Here's my code example:
from typing import Iterable
from typing import List
from typing import MutableSequence
from typing import Union
from typing import overload
class RecordBehaviourList(MutableSequence[str]):
def __init__(self, lst: List[str]) -> None:
self._lst = lst
@overload
def __getitem__(self, index: int) -> str:
...
@overload # noqa: F811 # TODO: flake8 3.8
def __getitem__(self, index: slice) -> MutableSequence[str]:
...
def __getitem__( # noqa: F811 # TODO: flake8 3.8
self,
index: Union[int, slice],
) -> Union[str, MutableSequence[str]]:
return self._lst[index]
@overload
def __setitem__(self, index: int, value: str) -> None:
...
@overload # noqa: F811 # TODO: flake8 3.8
def __setitem__(self, index: slice, value: Iterable[str]) -> None:
...
def __setitem__( # noqa: F811 # TODO: flake8 3.8
self,
index: Union[int, slice],
value: Union[str, Iterable[str]],
) -> None:
self._lst[index] = value
def __delitem__(self, index: Union[int, slice]) -> None:
del self._lst[index]
def insert(self, index: int, value: str) -> None:
self._lst.insert(index, value)
I've taken the overload definitions from this example (?) but perhaps I'm doing this incorrectly?
For now I can # type: ignore but seems non-ideal
Here's my version information and current output
$ mypy --version
mypy 0.740
$ mypy t3.py
t3.py:39: error: Invalid index type "Union[int, slice]" for "List[str]"; expected type "int"
t3.py:39: error: Incompatible types in assignment (expression has type "Union[str, Iterable[str]]", target has type "str")
Found 2 errors in 1 file (checked 1 source file)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、mypy 0.740 を使って t3.py の例を再現し、typeshed からリンクされている overload 定義と比較します。List[str] に対する setitem の実装が拒否される理由を調査します。正しくサポートされる型付けを確立するか、mypy の診断上の問題を確認できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100