How to implement overload of `__setitem__` for `MutableSequence`?
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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)
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo l’esempio in t3.py con mypy 0.740 e confronta le definizioni di overload collegate da typeshed. Indaga sul motivo per cui l’implementazione di setitem viene rifiutata per List[str]; il lavoro è completato quando viene stabilita la tipizzazione supportata corretta oppure viene confermato un problema di diagnostica di mypy.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100