python / python/mypy

TypeForm does not overlap with classes that represent special forms at runtime.

Abierto
#21,048 2 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-reachability topic-type-form topic-type-narrowing
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug Report

A value annotated as a TypeForm is not considered to overlap with various clases used to represent special forms at runtime.

I have tested typing.NewType, types.GenericAlias, and types.UnionType. [^1]

Given a variable defined as x: TypeForm[object] when using isinstance(x, typing.NewType) (or any of the above types), the truthy branch that follows is considered unreachable.

This does not happen with isinstance(x, type) though.

To Reproduce

from typing_extensions import TypeForm
import typing
import types

Y = typing.NewType("Y", int)

x1: TypeForm[object] = Y  # no issue, `Y` is a valid type expression

if isinstance(x1, typing.NewType):
    print("new type") # error: Statement is unreachable  [unreachable]
    
x2: TypeForm[object] = list[int]  # no issue, `list[int]` is a valid type expression

if isinstance(x2, types.GenericAlias):
    print("generic alias") # error: Statement is unreachable  [unreachable]
    
x3: TypeForm[object] = int | str  # no issue, `int | str` is a valid type expression

if isinstance(x3, types.UnionType):
    print("generic alias") # error: Statement is unreachable  [unreachable]
    
x4: TypeForm[object] = int  # no issue, `int` is a valid type expression

if isinstance(x4, type):
    print("generic alias") # no issue, code is reachable

https://mypy-play.net/?mypy=master&python=3.14&flags=warn-unreachable&gist=ea667f1dabb249a088448f23252c92c1

Expected Behavior

The code within the branches to be considered reachable.

Actual Behavior

main.py:10: error: Statement is unreachable  [unreachable]
main.py:15: error: Statement is unreachable  [unreachable]
main.py:20: error: Statement is unreachable  [unreachable]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.19.1 and main branch
  • Mypy command-line flags: --enable-incomplete-feature=TypeForm --warn-unreachable
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 1.14

[^1]: I think think is all the publicly documented classes?

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

El informe no menciona ningún archivo fuente ni ninguna prueba existente. Empieza ejecutando el reproductor proporcionado con --enable-incomplete-feature=TypeForm y --warn-unreachable; después, sigue el tratamiento de solapamientos de TypeForm para typing.NewType, types.GenericAlias y types.UnionType. Se considera completado cuando esas ramas se consideran alcanzables y una prueba de regresión cubre los casos.

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
3/5
Tiempo estimado
1-2 días
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.