Shouldn't the return type of `__reversed__` be an `Iterable[T]` instead of `Iterator[T]`?
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
Línea de trabajo
Comienza con la definición de Reversible en stdlib/typing.pyi en las líneas enlazadas y, después, compara los dos ejemplos de mypy Playground del issue. Determina una firma que acepte la implementación de reversed mostrada y que mantenga la comprobación de tipos de reversed(my_iter); se considera terminado cuando se hayan resuelto ambos errores indicados sin debilitar el comportamiento de tipado no relacionado.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Currently the return type of Reversible.__reversed__ is typed as Iterator[T], which suggests that the returned instance must have both an __next__ and an __iter__ method.
However, the following code seems to work fine at runtime:
from dataclasses import dataclass
from typing import Iterator, Iterable
@dataclass
class MyIter:
_values: list[int]
def __iter__(self) -> Iterator[int]:
return iter(self._values)
def __reversed__(self) -> Iterator[int]:
return MyIter(self._values[::-1])
my_iter = MyIter([1, 2, 3])
for x in my_iter:
print(x)
for x in reversed(my_iter):
print(x)
Note that __reversed__ here returns a MyIter instance, which has an __iter__ method, but no __next__, i.e., it is an Iterable but not an Iterator. The Python interpreter seems to deal with that fine at runtime, i.e., it doesn't actually seem to need the __next__ method. This is slightly surprising, because the docs specify:
It should return a new iterator object that iterates over all the objects in the container in reverse order.
I.e., it doesn't use the word "iterable object".
Unfortunately, the current signature of __reversed__ means that this example does not type check: Obviously the type checker has to complain about the return MyIter(...) line, because MyIter is indeed only an Iterable (example on mypy playground):
main.py:13: error: Incompatible return value type (got "MyIter", expected "Iterator[int]") [return-value]
main.py:13: note: "MyIter" is missing following "Iterator" protocol member:
main.py:13: note: __next__
Found 1 error in 1 file (checked 1 source file)
Now I'm wondering if the signature should actually be def __reversed__(self) -> Iterable[int] to lessen the requirement and match the runtime behavior?
Note that simply changing the return type to Iterable on user side means that the return statement now type checks, but then all usages (reversed(my_iter)) stop to type check because the type checker will no longer consider MyIter as a valid Reversible (modified example on mypy playground):
main.py:18: error: No overload variant of "reversed" matches argument type "MyIter" [call-overload]
main.py:18: note: Possible overload variants:
main.py:18: note: def [_T] __new__(cls, Reversible[_T], /) -> reversed[_T]
main.py:18: note: def [_T] __new__(cls, SupportsLenAndGetItem[_T], /) -> reversed[_T]
Found 1 error in 1 file (checked 1 source file)
- Lenguaje dominante
- Python
- Estrellas
- 5.1k
- Forks
- 2.1k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 82
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.
Más de python/typeshed
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
project: infrastructure
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
-
stubs: improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Todos los issues de python/typeshed
Issues similares
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
zostera/django-bootstrap4#894 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
use-agent-os/agent-os#3276 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
NousResearch/hermes-agent#117848 ·