Incorrect narrowing after `TypeIs[type[...]]` check

Aperta
#5,701 1 commento 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
53/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python, rust

Direzione di ricerca

Start by reproducing the narrowing behavior in the linked Pyrefly sandbox, then trace how custom TypeIs predicates narrow both branches in the Rust type checker. Compare the TypeIs[type[bytes]] example with the built-in issubclass example and find the relevant narrowing tests. Done means the else branch retains the appropriate type instead of becoming Never, with regression coverage for the example.

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

Descrizione

narrowing typechecking
Describe the Bug

narrowing with a custom -> TypeIs[type[Foo]] incorrectly narrows the else branch to Never:

from typing import TypeIs, reveal_type

def is_bytes(obj: object, /) -> TypeIs[type[bytes]]:
    return isinstance(obj, type) and issubclass(obj, bytes)

def check(cls: type, /) -> None:
    if is_bytes(cls):
        reveal_type(cls)  # type[bytes]
    else:
        reveal_type(cls)  # Never

def check2(cls: type, /) -> None:
    if issubclass(cls, bytes):
        reveal_type(cls)  # type[bytes]
    else:
        reveal_type(cls)  # type[Any]
Sandbox Link

https://pyrefly.org/sandbox/?project=v2.tVW7DoJAEPyVjTQUxktMbPgDG2NjpcQAIhB5GA4MxPjvzu0B4QjRWFjq7dzOsTOzP0SmGvQWdp_kjRp2Is9-i442JNkLdkmCx69hR1V-5CLX7T-tFthIiEAuVVfoG2RwImufTatPGM4SUE2DOAxudpDC5ToYdb-R3FjlHTPUmeIeHsFHRBbf0lGc1_sMhoPfYLT-Tml4FSr7V_2BG2Ow6FzIoa6SVOrRWnTAj6Rqh6ThdOXoKcrMw9apMz8s7caBw3RsIRrMoV1Pix1XOfRsXp0iJ36waK__oE5a2AQwUlRj36Dr6pRbWDUhxVV1l44QPbwoIxHm4lIEUhgIoQiS9_AS3mLmdVSwfyWunTfihll-MsMb

(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.