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

it's not possible to define an 'add' that works with more than one kind of number when using number types directly #12899

Open
glyph opened this issue May 28, 2022 · 4 comments
Labels
bug mypy got something wrong

Comments

@glyph
Copy link

glyph commented May 28, 2022

Bug Report

I can't figure out how to write generic numeric code.

This is already covered to some extent by #3186, but that's about a higher-level numeric tower issue, rather than the very practical issue of code that can just work with int or float but has to keep the number type straight.

To Reproduce

from typing import TypeVar

Num = TypeVar("Num", bound=int | float)


def add(a: Num, b: Num) -> Num:
    return a + b

I actually want | Decimal | Fraction in there, but that makes the problem worse.

The same issue seems to occur with Generics.

Expected Behavior

No errors.

Actual Behavior

add.py:7: error: Incompatible return value type (got "float", expected "Num")
add.py:7: error: Unsupported operand types for + (likely involving Union)
Found 2 errors in 1 file (checked 1 source file)

I understand how this would not work if I were using division, or if there were some other variance on Num but is there really some way to add an int to an int and get a float?

Your Environment

  • Mypy version used: mypy 0.960 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.10.4 from python.org
  • Operating system and version:
ProductName:	macOS
ProductVersion:	12.4
BuildVersion:	21F79
@glyph glyph added the bug mypy got something wrong label May 28, 2022
@glyph glyph changed the title it's not possible to define an 'add' that works with more than one kind of number it's not possible to define an 'add' that works with more than one kind of number when using number types directly May 28, 2022
@glyph
Copy link
Author

glyph commented May 28, 2022

I guess the workaround looks like this?

from typing import TypeVar, Protocol

Self = TypeVar("Self")


class Addable(Protocol):
    def __add__(self: Self, other: Self) -> Self:
        ...


Num = TypeVar("Num", bound=Addable)


def add(a: Num, b: Num) -> Num:
    return a + b

This seems to work? It still has the problem of "mixing int and float is allowed, and results in float results" but this is like #9015 in that it's an intentional if unfortunate compromise with reality

@pranavrajpal
Copy link
Contributor

Another workaround (that still allows mixing int and float) seems to be using a constrained type variable:

from typing import TypeVar

Num = TypeVar("Num", int, float)

def add(a: Num, b: Num) -> Num:
    return a + b

reveal_type(add(1, 2)) # N: Revealed type is "builtins.int"
reveal_type(add(1, 2.0)) # N: Revealed type is "builtins.float"
reveal_type(add(1.0, 2)) # N: Revealed type is "builtins.float"
reveal_type(add(1.0, 2.0)) # N: Revealed type is "builtins.float"

I think mypy is somewhat correct in erroring on your original example because subclassing float is allowed. In this example:

from typing import TypeVar

# mypy is probably treating int | float the same as float here
Num = TypeVar("Num", bound=float)

class C(float):
    pass

def add(a: Num, b: Num) -> Num:
    return a + b # E: Incompatible return value type (got "float", expected "Num")

reveal_type(add(C(), C())) # N: Revealed type is "__main__.C"

mypy infers Num to be C in the first call, even though add will return a float because C doesn't override __add__. Using a constrained type variable avoids this by solving Num as float in that case.

The workaround with an Addable protocol seems like a mypy bug, since it doesn't seem to error when using a subclass of float like above.

@erictraut
Copy link

I think mypy is doing the right thing here. I don't consider this a bug.

@JelleZijlstra
Copy link
Member

The OP's example with latest mypy (https://mypy-play.net/?mypy=latest&python=3.10&gist=433772fa66fc4f012b485d036496cc89) still produces these errors:

main.py:7: error: Incompatible return value type (got "int | float", expected "Num")  [return-value]
main.py:7: error: Unsupported operand types for + (likely involving Union)  [operator]

I think the first error is correct but the second is a bug. Pyright only produces a single error, equivalent to the return-value error.

If you change the return type to float, theoperator error is still emitted, but Pyright accepts the code, I think correctly.

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

4 participants