KEMBAR78
Union[float, int] is unsupported type for binary math operators · Issue #5381 · python/mypy · GitHub
Skip to content

Union[float, int] is unsupported type for binary math operators #5381

@campkeith

Description

@campkeith

For this code:

from typing import Union

def add(a: Union[float, int], b: Union[float, int]) -> float:
    return a + b

The error I get is:

% mypy --version
mypy 0.620
% mypy foo.py
foo.py:4: error: Unsupported operand types for + ("Union[float, int]" and "Union[float, int]")

If I declare either parameter as just an int or a float (and leave the other parameter unchanged) then the error goes away.

According to the documentation, it seems that Union[float, int] should be equivalent to float. So the simple solution seems to be to declare both parameters as float, but I would expect mypy to figure this out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions