Figure out story about diamond inheritance
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 25/100
Piste de recherche
Reproduce the diamond-inheritance case described in the issue and inspect class_derivation_paths in mypy/maptype.py, including the callers that take only one path. Then review verify_base_classes in semanal.py and the linked triggering test case to determine which policy is feasible and what diagnostic or behavior would define completion.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
In mypy/maptype.py, we have a function class_derivation_paths that finds all the paths up the hierarchy from a derived class to a given ancestor class. The code implicitly assumes that this function will only ever return a single path, for example by silently taking the first element of a list derived from its result and dropping the rest:
return map_instance_to_supertypes(instance, superclass)[0]
And indeed there are a couple of assertions in comments in the code that this function should only ever find a single path:
# FIX: Currently we should only have one supertype per interface, so no
# need to return an array
[...]
# FIX: Currently we might only ever have a single path, so this could be
# simplified
But this doesn't seem to be true! In fact, if we set up the classic diamond inheritance pattern:
A
/ \
B C
\ /
D
then we do in fact get two paths, one through B and one through C. Demonstrated by adding an assert and a triggering test case here: https://github.com/gnprice/mypy/commit/a85876521
which produces this exception:
AssertionError: [[<TypeInfo __main__.B>, <TypeInfo __main__.A>], [<TypeInfo __main__.C>, <TypeInfo __main__.A>]]
What should our approach to this situation be?
In roughly increasing order of semantic and implementation complexity, some options include
- forbid this situation -- require that there's only a single inheritance path from a given derived class to a given ancestor, and say that code that uses diamond inheritance can't be typed
- allow diamond inheritance only if the ancestor is not generic
- allow diamond inheritance only if all the inheritance paths can be verified, just from the class definitions, to always result in the same instance of the ancestor for any instance of the derived
- allow diamond inheritance only if the actual instances of the derived class that we encounter each result in a single instance of the ancestor across all inheritance paths (this is uncomfortably reminiscent of C++ templates)
- something even more complicated that allows inheriting from several different instances of the same ancestor
I'm inclined to start with option 1, as it's super simple to understand and to implement, and see if that ever poses a problem. Maybe nobody actually uses diamond inheritance in code they want to try to type; it's a pretty tricky pattern in the first place. If we want to do this, we should detect the situation up front (like in verify_base_classes in semanal.py) and give an appropriate error.
Option 2 is also super simple to implement, if people use diamond inheritance but they don't need generics with it. Somewhat less satisfying as a rule for the user.
I'd be kind of concerned if we find ourselves starting to think we need one of the more complex options.
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Merge moyen
- 1 j 18 h
- PR mergées (30 j)
- 54
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/mypy
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
bug topic-configuration topic-error-reporting
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Toutes les issues de python/mypy
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
zostera/django-bootstrap4#894 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
use-agent-os/agent-os#3276 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
NousResearch/hermes-agent#117848 ·