Warn when PEP695 type param clashes with a TypeVar name
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue in the linked mypy playground using the sample program, then compare arithmetic handling in the generic class methods with the functions that already typecheck. Trace PEP 695 class type-parameter handling and constrained TypeVar operations; done means the sample program typechecks without the six operator errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 40/100