Get rid of the "flipping behaviour" in narrowign by equality/identity
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
We have this code that makes sure that if the intersection of declared and narrowed is not representable, then we prefer the narrowed. I added this logic a while ago, since this is what a user usually expects in case of e.g. an assignment or even an isinstance() check.
However, the recent effort to make equality/identity narrowing symmetric also caused this to result in a "flipping" behavior, when sides exchange types after a comparison, see e.g. testNarrowGenericCallableEquality. Although such cases are rare, I think the resulting behavior is really weird/confusing.
So I propose to add a flag to each restriction in binder (similar to from_assignment), say prefer_declared that would be set to True for right-most operand in equality/identity. This flag would override the fallback mentioned above. So that after something like assert x == y == a, the type of a will not change if the intersection is not representable.
I understand this breaks the symmetry, but this is purely practical thing, this is why people write x == 2 and not 2 == x.
cc @hauntsaninja
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der Fallback-Logik in mypy/checkexpr.py um die Zeilen 6494-6499 und verfolge dann, wie Einschränkungen im binder dargestellt und angewendet werden. Führe den Fall testNarrowGenericCallableEquality aus, um das Umschlagverhalten zu reproduzieren, und untersuche die benachbarten Tests zum Narrowing von Gleichheit und Identität. Die Aufgabe ist abgeschlossen, wenn der Operand ganz rechts seinen deklarierten Typ behält, wenn der Schnitt nicht darstellbar ist, ohne das gewöhnliche Narrowing-Verhalten zu ändern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Aktiv
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 48/100