python / python/mypy

Warn when PEP695 type param clashes with a TypeVar name

Aperta
#18,499 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

When defining a generic class with a type variable bounded to number classes, arithmetic operations are not available in class methods.

To Reproduce

Playground link: https://mypy-play.net/?mypy=master&python=3.12&gist=97147ba435ef98e5566e5c6a01424f92

Sample program

from typing import TypeVar
from decimal import Decimal

TAmount = TypeVar("TAmount", int, Decimal)

class Box[TAmount]:
    def __init__(self, amount: TAmount) -> None:
        self.amount = amount
    
    def box_method_with_arithmetic(self, other: Box[TAmount]) -> None:
        add = self.amount + other.amount
        sub = self.amount - other.amount
        mul = self.amount * other.amount
        div = self.amount / other.amount
        floordiv = self.amount // other.amount
        pow = self.amount ** other.amount

def value_function_with_arithmetic(left: TAmount, right: TAmount) -> None:
    add = left + right
    sub = left - right
    mul = left * right
    div = left / right
    floordiv = left // right
    pow = left ** right

def box_function_with_arithmetic(left: Box[TAmount], right: Box[TAmount]) -> None:
    add = left.amount + right.amount
    sub = left.amount - right.amount
    mul = left.amount * right.amount
    div = left.amount / right.amount
    floordiv = left.amount // right.amount
    pow = left.amount ** right.amount

Expected Behavior

The program should typecheck successfully.

Actual Behavior

main.py:11: error: Unsupported left operand type for + ("TAmount")  [operator]
main.py:12: error: Unsupported left operand type for - ("TAmount")  [operator]
main.py:13: error: Unsupported left operand type for * ("TAmount")  [operator]
main.py:14: error: Unsupported left operand type for / ("TAmount")  [operator]
main.py:15: error: Unsupported left operand type for // ("TAmount")  [operator]
main.py:16: error: Unsupported left operand type for ** ("TAmount")  [operator]
Found 6 errors in 1 file (checked 1 source file)

This seems to only be a problem in methods on the generic class. The functions which operate on values and class instances typecheck cleanly.

Note that the TypeVar is defined as being exactly int or Decimal as per https://github.com/python/mypy/issues/12899#issuecomment-1140352126.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci il problema nel mypy playground collegato usando il programma di esempio, quindi confronta la gestione dell’aritmetica nei metodi della classe generica con le funzioni che passano già il controllo dei tipi. Traccia la gestione dei parametri di tipo della classe secondo PEP 695 e delle operazioni con TypeVar vincolato; il lavoro è completato quando il programma di esempio passa il controllo dei tipi senza i sei errori degli operatori.

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
40/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.