How to implement overload of `__setitem__` for `MutableSequence`?
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
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)
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo el ejemplo de t3.py con mypy 0.740 y compara las definiciones de overload enlazadas desde typeshed. Investiga por qué se rechaza la implementación de setitem para List[str]; se considera terminado cuando se haya establecido la tipificación compatible correcta o se haya confirmado un problema de diagnóstico de mypy.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100