Overridden `__iter__` with more specific item type is only respected in some contexts
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Given a subclass C of Iterable[int] that more specifically implements def __iter__(self) -> Iterator[bool] (which is fine, since bool is a subclass of int), I would expect mypy to understand that iterating C() yields bools. And indeed it does understand this in some contexts, such as iter(C()) and for item in C(). But in other contexts, such as list(C()) and unpacking, it relies only on the base class Iterable[int] and discards the more specific information from __iter__.
To Reproduce
from typing import Iterable, Iterator
class C(Iterable[int]):
def __iter__(self) -> Iterator[bool]:
yield True
# these give bool, as expected
reveal_type(C().__iter__()) # Iterator[bool]
reveal_type(iter(C())) # Iterator[bool]
reveal_type([comp_item for comp_item in C()]) # list[bool]
for loop_item in C(): reveal_type(loop_item) # bool
# these unexpectedly give int
reveal_type(list(C())) # list[int]
[unpack_item] = C(); reveal_type(unpack_item) # int
[*unpack_items] = C(); reveal_type(unpack_items) # list[int]
# these unexpectedly give Any
reveal_type([*C()]) # list[Any] (#15747)
reveal_type(max(*C())) # Any
Expected Behavior
All items should be inferred as bool.
main.py:8: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:9: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:10: note: Revealed type is "builtins.list[builtins.bool]"
main.py:11: note: Revealed type is "builtins.bool"
main.py:14: note: Revealed type is "builtins.list[builtins.bool]"
main.py:15: note: Revealed type is "builtins.bool"
main.py:16: note: Revealed type is "builtins.list[builtins.bool]"
main.py:19: note: Revealed type is "builtins.list[builtins.bool]"
main.py:20: note: Revealed type is "builtins.bool"
Actual Behavior
The items are inconsistently inferred as bool, int, or Any, depending on the context.
main.py:8: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:9: note: Revealed type is "typing.Iterator[builtins.bool]"
main.py:10: note: Revealed type is "builtins.list[builtins.bool]"
main.py:11: note: Revealed type is "builtins.bool"
main.py:14: note: Revealed type is "builtins.list[builtins.int]"
main.py:15: note: Revealed type is "builtins.int"
main.py:16: note: Revealed type is "builtins.list[builtins.int]"
main.py:19: note: Revealed type is "builtins.list[Any]"
main.py:20: note: Revealed type is "Any"
One of the Any cases was previously reported:
- #14470
- #15747
Your Environment
- Mypy version used: 1.7.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.11
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, die Reproduktion des Issues mit mypy auszuführen und die reveal_type-Ergebnisse für iter(C()), list(C()), das Entpacken und Argumente mit Sternchen zu vergleichen. Verfolge die Einstiegspunkte der Typprüfung für die Listenkonstruktion, das Entpacken und Aufrufe mit Sternchen; abgeschlossen ist die Aufgabe, wenn alle aufgeführten Elementtypen konsistent als bool inferiert werden, einschließlich der zuvor gemeldeten Any-Fälle.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100