python / python/mypy

meet.is_overlapping_types should be more relaxed for isinstance checks

Ouverte
#5,529 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

needs discussion priority-1-normal refactoring topic-reachability topic-type-narrowing
Langage dominant
Python
Étoiles
20.6k
Forks
3.3k
Métriques de merge des PR
Métriques de PR en attente

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par lire l’implémentation de meet.is_overlapping_types et la discussion dans PR 5476 et issue 3603. Comparez son comportement actuel pour les types inattendus, l’héritage multiple et List[A] par rapport à List[B], puis déterminez quel comportement d’accessibilité est attendu. La tâche est terminée lorsqu’une approche a été définie pour assouplir les vérifications isinstance et None sans élargir à tort les vérifications de sécurité des overloads.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.