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

Abierto
#5,701 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
53/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python, rust

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Rust
Estrellas
7.2k
Forks
578
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de facebook/pyrefly

Todos los issues de facebook/pyrefly

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.