Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic with type variables fails to typecheck ("Unsupported operand types") #18203

Open
inducer opened this issue Nov 27, 2024 · 0 comments
Labels
bug mypy got something wrong

Comments

@inducer
Copy link

inducer commented Nov 27, 2024

To Reproduce

from typing import Protocol, TypeVar
from typing_extensions import Self

class A(Protocol):
    def __add__(self, other: int | Self) -> Self: ...
    def __radd__(self, other: int | Self) -> Self: ...


AT = TypeVar("AT", bound=int | A)
# OK:
# AT = TypeVar("AT", int, A)


def f(a: AT) -> None:
    a + a

https://mypy-play.net/?mypy=latest&python=3.12&gist=13a490c6b5b075de398e0c84736fd194

Expected Behavior

It's a little weird that there is a lone type variable, but all cases of the operator application should be covered, so IMO this should typecheck OK. pyright agrees:

WARNING: there is a new pyright version available (v1.1.388 -> v1.1.389).
Please install the new version or set PYRIGHT_PYTHON_FORCE_VERSION to `latest`

/home/andreas/tmp/mypy-arith.py
  /home/andreas/tmp/mypy-arith.py:14:10 - warning: TypeVar "AT" appears only once in generic function signature
    Use "int | A" instead (reportInvalidTypeVarUse)
  /home/andreas/tmp/mypy-arith.py:15:5 - warning: Expression value is unused (reportUnusedExpression)
0 errors, 2 warnings, 0 informations

Actual Behavior

mypy-arith.py:15: error: Unsupported operand types for + (likely involving Union)  [operator]

Your Environment

  • Mypy version used: 1.13
  • Mypy command-line flags: None
  • Mypy configuration options from None
  • Python version used: 3.13
@inducer inducer added the bug mypy got something wrong label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant