abs(Union[int, Decimal]) is inferred as object
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
I'm afraid I'm not sure whether I'm reporting a bug or requesting a feature. I'm happy to leave that up for you all to triage. I'll start with what I'm seeing and what I'd like to see, and then get a little bit into the why.
Here's a minimal reproduction:
from decimal import Decimal
from typing import Union
def check(x: Decimal, y: Union[Decimal, int]) -> bool:
return x < -abs(y)
With Python 3.7.3 (from Debian buster) and mypy 0.770 (from PyPI), checking this code returns an error:
deccomp.py:6: error: Unsupported operand type for unary - ("object") [operator]
Found 1 error in 1 file (checked 1 source file)
The return type of abs is inferred as object, I think because that's the common supertype of int and Decimal. It would be nice if, one way or another, the return type of abs could be inferred as some higher numeric type.
The context here is I'm working on accounting software where I want to be careful to do decimal math throughout. In other words, I never want to deal with the decimal.FloatOperation signal. For functions that do basic arithmetic or comparisons across numbers, it's fine to accept arguments that are either int or Decimal, and it's convenient for callers if I can annotate that argument type rather than requiring them to convert their integer arguments to Decimal all the time.
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 con la riproduzione minima in deccomp.py ed eseguila con mypy per confermare che abs(y) venga inferito come object e causi l'errore di unary-minus. Esamina come viene tipizzato abs per Union[Decimal, int]; il lavoro è completato quando l'esempio supera il type-check senza l'errore dell'operando object, preservando al contempo un'aritmetica sicura per Decimal.
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
- 35/100