python / python/mypy

`TypeIs` with `isinstance` fails to narrow

Abierto
#21,508 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

If I use TypeIs along with isinstance, the type doesn't get narrowed

To Reproduce

from typing import TypeIs, Any, TypeAlias, reveal_type

SliceInt: TypeAlias = slice[int | None, int | None, int | None]
SliceStr: TypeAlias = slice[str | None, str | None, str | None]


def is_slice_int(obj: Any) -> TypeIs[SliceInt]:  # type: ignore[empty-body]
    ...



def main(obj: SliceInt | SliceStr) -> None:
    if is_slice_int(obj):
        pass
    elif isinstance(obj, slice):
        reveal_type(obj)

https://mypy-play.net/?mypy=latest&python=3.14&gist=170b0c83ef93742ff998485be01e9440

Expected Behavior

t.py:19: note: Revealed type is "slice[str | None, str | None, str | None]"

Actual Behavior

t.py:19: note: Revealed type is "slice[int | None, int | None, int | None] | slice[str | None, str | None, str | None]"

Note that, for comparison, pyright and pyrefly get this right:

(scratch) mgorelli@marcoslaptop:~/scratch$ pyright t.py
pyre/home/mgorelli/scratch/t.py
  /home/mgorelli/scratch/t.py:19:21 - information: Type of "obj" is "slice[str | None, str | None, str | None]"
0 errors, 0 warnings, 1 information
(scratch) mgorelli@marcoslaptop:~/scratch$ pyright t.py
/home/mgorelli/scratch/t.py
  /home/mgorelli/scratch/t.py:19:21 - information: Type of "obj" is "slice[str | None, str | None, str | None]"
0 errors, 0 warnings, 1 information
(scratch) mgorelli@marcoslaptop:~/scratch$ pyrefly check t.py
 INFO revealed type: slice[str | None, str | None, str | None] [reveal-type]
  --> t.py:19:20
   |
19 |         reveal_type(obj)
   |                    -----
   |
 INFO 0 errors

Your Environment

  • Mypy version used: 2.1.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13.13

Guía de contribución

Abrir la guía de contribución

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.

Línea de trabajo

Comienza con el reproductor de mypy-play enlazado y compara el tipo revelado para la rama TypeIs/isinstance con la salida esperada. Rastrea los puntos de entrada del estrechamiento de tipos para TypeIs e isinstance, luego verifica que el ejemplo revele únicamente SliceStr y añade cobertura para esta regresión si se encuentra la ubicación de prueba relevante.

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

Evaluación

Stack tecnológico
python
Área
devtools
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
52/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.