python / python/mypy

Warn when PEP695 type param clashes with a TypeVar name

Offen
#18,499 7 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduzieren Sie das Problem im verlinkten mypy playground mit dem Beispielprogramm und vergleichen Sie anschließend die Behandlung arithmetischer Operationen in den Methoden der generischen Klasse mit den Funktionen, die bereits typgeprüft werden. Verfolgen Sie die Behandlung von Klassen-Typparametern nach PEP 695 und Operationen mit eingeschränkten TypeVar; abgeschlossen ist die Aufgabe, wenn das Beispielprogramm ohne die sechs Operatorfehler typgeprüft wird.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.