Skip to content

Binary arithmetic on nested Unions of numeric types fails to typecheck #8125

Closed
@appleby

Description

@appleby

Are you reporting a bug, or opening a feature request?

bug (I think)

Please insert below the code you are checking with mypy, or a mock-up repro if the source is private. We would appreciate if you try to simplify your case to a minimal repro.

from typing import Union

T1 = Union[int, float]
T2 = Union[T1, complex]

def foo(a: T2, b: T2) -> T2:
    return a + b

What is the actual behavior/output?

$ mypy ~/tmp/foo.py
tmp/foo.py:7: error: Unsupported operand types for + ("int" and "Union[int, float]")
tmp/foo.py:7: note: Both left and right operands are unions
Found 1 error in 1 file (checked 1 source file)

What is the behavior/output you expect?

To typecheck without errors.

What are the versions of mypy and Python you are using? Do you see the same issue after installing mypy from Git master?

python 3.6.9
mypy 0.750 and master produce the above error.
mypy 0.740 does not produce an error.

What are the mypy flags you are using? (For example --strict-optional)

The issue reproduces when running mypy both without any flags and when running with --strict.

Additional info

In case it's helpful, here are some possibly/loosely-related issues I found while searching the issue tracker:

#2128
#3196

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions