Skip to content

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

Open

Description

@glyph

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

Activity

  1. 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[/+] on May 28, 2022
  2. glyph commented on May 28, 2022

    @glyph
    Author

    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

  3. pranavrajpal commented on May 29, 2022

    @pranavrajpal
    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.

  4. erictraut commented on Aug 13, 2023

    @erictraut

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

  5. JelleZijlstra commented on Aug 13, 2023

    @JelleZijlstra
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions