datetimes `__sub__` is not symmetrical in subclass.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il riproduttore mypy-play collegato con Python 3.9 e confrontando il comportamento tra mypy 0.921 e 0.950. Poi esamina le annotazioni di typeshed per datetime.sub, concentrandoti sulla variabile di tipo _D e sull’ordine degli operandi; il lavoro è completato quando entrambe le direzioni della sottrazione superano in modo coerente il controllo dei tipi per l’esempio della sottoclasse.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100