datetimes `__sub__` is not symmetrical in subclass.
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
import datetime
from typing import Any
class MyDatetime(datetime.datetime):
def __eq__(self, other: Any) -> bool:
if isinstance(other, datetime.datetime):
delta = self - other # error: No overload variant of "__sub__" of "datetime" matches argument type "datetime" [operator]
delta = other - self # OK
return True
else:
return NotImplemented
python: 3.9
mypy: 0.950
playground: https://mypy-play.net/?mypy=0.950&python=3.9&gist=ac2ffa60be085e552cb67043afa59e07
The applicability of the superclass __sub__ method is only recognized for datetime - MyDatetime, but not for MyDatetime - datetime.
Looking into typeshed, there is this annotation:
_D = TypeVar("_D", bound=date)
class datetime(date):
if sys.version_info >= (3, 8):
def __sub__(self: _D, __other: _D) -> timedelta: ...
else:
def __sub__(self, __other: datetime) -> timedelta: ...
Telling mypy that the python version is 3.7 gets rid of the error, so there seems to be some issue, where the _Ds are not treated the same way depending on the order of operations.
The same error is not reported for mypy 0.921 and earlier: https://mypy-play.net/?mypy=0.921&python=3.9&gist=ac2ffa60be085e552cb67043afa59e07
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 damit, den verlinkten mypy-play-Reproducer mit Python 3.9 auszuführen und das Verhalten zwischen mypy 0.921 und 0.950 zu vergleichen. Untersuche anschließend die typeshed-Annotationen für datetime.sub, wobei du dich auf die Typparameter-Variable _D und die Reihenfolge der Operanden konzentrierst; fertig ist die Aufgabe, wenn beide Subtraktionsrichtungen für das Beispiel mit der Unterklasse konsistent typgeprüft werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100