python / python/mypy

Support exhaustiveness checking of single enumerable properties

Abierto
#14,261 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

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

Descripción

Feature

Support exhaustiveness checking of single boolean and enum properties and attributes of objects.

Pitch

The proposal is to improve exhaustiveness checking by supporting properties/attributes on a matched object. While it would be amazing to have infinitely nested support for this, I think we can recognize that there is value in supporting just single-level enumerable attributes.

In this example mypy isn't currently recognizing that all possible variations of Foo are handled, and gives an error on the assert_never().

from typing import assert_never

class Sentinel: ...

class Foo:
    prop: bool

val: Foo | Sentinel

match val:
    case Sentinel():
        ...
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case no_match:
        assert_never(no_match)  # Currently a false positive here, no_match should be <nothing>.

https://mypy-play.net/?mypy=latest&python=3.11&gist=1edcb8f3ea56334662ed2036404563a1

Likewise, if the feature is accepted, we should expect an unreachable error for the third case in this example.

from typing import assert_never

class Foo:
    prop: bool

val: Foo

match val:
    case Foo(prop=True):
        ...
    case Foo(prop=False):
        ...
    case Foo():
        ...  # Currently a false negative here, should be unreachable.

https://mypy-play.net/?mypy=latest&python=3.11&gist=74b23b6868c0c52056be1ade612b3052

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 con los dos ejemplos de match de Python y sus reproducciones enlazadas en mypy-play. Define la exhaustividad y el comportamiento de los casos inalcanzables para propiedades booleanas y propiedades de tipo enum de un solo nivel; después, verifica que se corrijan los resultados false-positive y false-negative descritos en el issue.

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
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.