python / python/mypy

meet.is_overlapping_types should be more relaxed for isinstance checks

Abierto
#5,529 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

needs discussion priority-1-normal refactoring topic-reachability 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

Currently, the implementation of meet.is_overlapping_types(...) defaults to returning False when it encounters two unexpected types, and doesn't attempt to handle multiple inheritance at all. The current implementation also considers two types like List[A] and List[B] to be non-overlapping, even though they may overlap when both lists are empty.

This behavior makes sense when is_overlapping_types is used to detect unsafe overload variants, but may be too restrictive/cause branches to be unexpectedly unreachable when used for isinstance checks and if x is None checks and the like.

For more context, see https://github.com/python/mypy/pull/5476#discussion_r211030040 and https://github.com/python/mypy/issues/3603.

Some possible solutions include:

  1. Add a flag that makes is_overlapping_types default to returning True, not False. The main disadvantage here is that this would then make is_overlapping_types consider two unrelated types like Type[A] and TypedDict[...] to be overlapping, which seems wrong.
  2. Add a flag named allow_multiple_inheritance which makes the "if left and right are Instances" case just always return True. The main disadvantage here is that this may not capture every edge case we want to relax.

It may also be the case that neither solutions work and a more careful rearrangement of is_overlapping_types is necessary. The existing implementation of is_overlapping_types is designed to rule out bad combinations/normalize all types to Instance whenever possible, which may or may not be the correct thing to do for reachability checks.

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 leyendo la implementación de meet.is_overlapping_types y la discusión en PR 5476 y issue 3603. Compara su comportamiento actual con tipos inesperados, herencia múltiple y List[A] frente a List[B], y determina después qué comportamiento de alcanzabilidad se pretende. La tarea estará completada cuando se haya decidido un enfoque que relaje las comprobaciones de isinstance y None sin ampliar incorrectamente las comprobaciones de seguridad de overloads.

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
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.