`for` loop over a subclass ignores its overridden `__iter__` and uses the base class's type argument

Aperta
#5,570 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python, rust

Direzione di ricerca

The reproducer shows that iter(b) infers Iterator[int], while for loops and comprehensions infer object; start by running the Python example in the Pyrefly sandbox and tracing how iteration types are inferred for each construct. Find the relevant Rust implementation and regression tests for for loops and comprehensions. Done means both infer int for this subclass and existing iteration tests still pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

quansight typechecking
Describe the Bug
from collections.abc import Iterable, Iterator
from typing import reveal_type


class B(Iterable[object]):
    def __iter__(self) -> Iterator[int]:
        yield 1


def f(b: B) -> None:
    reveal_type(iter(b))  # Iterator[int]
    for y in b:
        reveal_type(y)  # expected int, got object
        y + 1  # false positive
    reveal_type([y for y in b])  # expected list[int], got list[object]

Spotted in Requests

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZXBDoIwDIZfpQkHMEYXrhx5Au_IAWQiF0YYGvf2tt0WN4hcPLK1Xbd-_8-uZf5wka13OJuILXblS15-ZebzKwtw7aFzUsRdJ0XGx59R4XDrgD5DrgO5Lc26ydoCyjVyQReExpy1hwNAEpe1ocSVIajaSAXffMOZ8j1h17LDSPThXqF18T2C1uAIOYXeG1QMTEoP9PzbhioTHFrH1dm5qDl7Bn-6B9uil8DFLoCbIs4Ome2feEcc3_k6JujqEh7LMulCCJ-u5l7IUXTqpkWUIbix5tUM_MOIy4FiqWgs-w_zqUU-3SM-JeA54tSMBoPYk3Cdi-PGLt0f

(Only applicable for extension issues) IDE Information

No response

Lingua principale
Rust
Stelle
7.2k
Fork
578
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di facebook/pyrefly

Tutte le issue di facebook/pyrefly

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.