Specifying the return type of `__abs__` to be a `TypeVar` causes `expected "SupportsAbs[<nothing>]` error
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
Specifying the return type of __abs__ to be a TypeVar causes expected "SupportsAbs[<nothing>] error
The following works fine:
from typing import TypeVar
T = TypeVar("T", bound="Number")
class Number:
def __abs__(self: T):
return self
def square(self: T) -> T:
return self
def myabs(x: T) -> T:
return abs(x)
x = myabs(Number())
x.square()
But this produces an error:
from typing import TypeVar
T = TypeVar("T", bound="Number")
class Number:
def __abs__(self: T) -> T:
return self
def square(self: T) -> T:
return self
def myabs(x: T) -> T:
return abs(x)
x = myabs(Number())
x.square()
- What is the actual behavior/output?
error: Argument 1 to "abs" has incompatible type "T"; expected "SupportsAbs[<nothing>]"
- What is the behavior/output you expect?
No error, like in the first example.
- What are the versions of mypy and Python you are using?
Python 3.6 and MyPy 0.761
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 riproducendo entrambi gli snippet Python con mypy 0.761 e confronta l’effetto delle due annotazioni del valore restituito di abs sulla chiamata abs(x). Segui il percorso del controllo dei tipi per SupportsAbs e i bound di TypeVar; il lavoro è completato quando la versione annotata non produce alcun errore incompatible-argument come la versione non annotata.
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
- 42/100