python / python/mypy

Plugin interface for type narrowing on `==` comparisons

Abierto
#10,708 1 comentario 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature topic-plugins
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature

I'm proposing a new plugin interface for type narrowing on == comparisons.

Pitch

Take the following code:

from enum import IntEnum

class MyIntEnum(IntEnum):
    A = 1
    B = 2

m: MyIntEnum

if m == 1:
    pass
else:
    reveal_type(m)

This should reveal MyIntEnum.B, but mypy can't know that because it has no idea about how the __eq__ method on MyIntEnum works.
A plugin interface together with a builtin plugin could fix that.

mypy would walk through the MyIntEnum MRO until it encounters a class that defines __eq__ and then look for a plugin able to handle this class.

The plugin matching the class will be called and can return two types. One narrowing for if the condition matches and one for if it doesn't.

This would also be useful for checking match statements (#10191). Currently I have to special case enums there, as match always compares emums with ==, while mypy only narrows types if enums are compared with is.

It would probably also make it possible to get rid of the special casing of a == type(A) and replace it with a plugin.

I would like to first hear some opinions about this before I start implementing it.

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

Empieza revisando las interfaces de plugin existentes de mypy y el comportamiento actual del estrechamiento de tipos de enum para == frente a is. Después, examina el caso de una sentencia match al que se hace referencia en #10191 y define cómo devolvería un plugin tipos de estrechamiento para comparaciones coincidentes y no coincidentes.

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

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
20/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.