Repository: python/mypy

Issue:
Binary arithmetic on nested Unions of numeric types fails to typecheck
### 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.

``` python
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:

https://github.com/python/mypy/issues/2128
https://github.com/python/mypy/issues/3196



Discussion:
As a workaround, if I manually "flatten" the `Union`, mypy is happy again. That is, the following typechecks even on mypy 0.750 / master

``` python
from typing import Union

T3 = Union[int, float, complex]

def bar(a: T3, b: T3) -> T3:
    return a + b  # ok
```

It looks like the issue is that in `check_op` in `mypy/checkexpr.py`, it collects all of the union elements into `left_variants` and `right_variants`, but should flatten out any unions inside those as well.

Interested in submitting a PR?
> It looks like the issue is that in `check_op` in `mypy/checkexpr.py`, it collects all of the union elements into `left_variants` and `right_variants`, but should flatten out any unions inside those as well.
> 
> Interested in submitting a PR?

Not at this time, no. I spent a few minutes looking into it, but being new to the mypy codebase, I suspect it'll take longer than I want to spend on it. For now, I'm happy to stick with mypy 0.740.

Here are some notes in case it's useful for whoever picks this up (maybe future me). Warning: I have no understanding of mypy internals and didn't attempt to properly debug this, just skimmed the source code.

At first I was confused because `UnionType` does [call `flatten_nested_unions` in it's constructor](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/types.py#L1684) (#3196, #3298). But then I noticed that `UnionType` is a subclass of `ProperType` and the `ProperType` docstring says `Every type except TypeAliasType must inherit from this type.`

Since my example above is using nested type aliases, I assume these get turned into [`TypeAliasType`s](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/types.py#L152)? The docstring for `TypeAliasType` warns `NOTE: this is not being used yet, and the implementation is still incomplete`, but a quick grep seems to indicate that it is used and the docstring might just be out of date.

Following on with @msullivan's tip about `check_op` in `mypy/checkexpr.py`, I notice that `get_proper_type` is called before expanding the actual `UnionType` for both the [`left_variants`](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/checkexpr.py#L2514) and the [`right_variants`](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/checkexpr.py#L2556). However,

1. [`get_proper_type`](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/types.py#L1902) only does a single expansion of the type alias by calling [`TypeAlias._expand_once`](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/types.py#L1902) which doesn't do a full expansion of nested aliases, and

2. The call to `flatten_nested_unions` in `UnionType.__init__` only [recursively flattens nested `ProperType`s](https://github.com/python/mypy/blob/a2ab97badc0db97b6d639af0157a12357c12c91d/mypy/types.py#L2241), not nested `TypeAliasType`s.

I'm am not sure what the right fix would be here, whether it's (a) flattening nested type alias Unions "by hand" in `ExpressionChecker.check_op`, or (b) updating `flatten_nested_unions` to also flatten nested `TypeAliasType`s, or (c) updating `get_proper_type` to `expand_all_if_possible` rather than `_expand_once`, or (d) something else.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -19,7 +19,7 @@
     PartialType, DeletedType, UninhabitedType, TypeType, TypeOfAny, LiteralType, LiteralValue,
     is_named_instance, FunctionLike,
     StarType, is_optional, remove_optional, is_generic_instance, get_proper_type, ProperType,
-    get_proper_types
+    get_proper_types, flatten_nested_unions
 )
 from mypy.nodes import (
     NameExpr, RefExpr, Var, FuncDef, OverloadedFuncDef, TypeInfo, CallExpr,
@@ -2521,7 +2521,9 @@ def check_op(self, method: str, base_type: Type,
             left_variants = [base_type]
             base_type = get_proper_type(base_type)
             if isinstance(base_type, UnionType):
-                left_variants = [item for item in base_type.relevant_items()]
+                left_variants = [item for item in
+                                 flatten_nested_unions(base_type.relevant_items(),
+                                                       handle_type_alias_type=True)]
             right_type = self.accept(arg)
 
             # Step 1: We first try leaving the right arguments alone and destructure
@@ -2564,8 +2566,8 @@ def check_op(self, method: str, base_type: Type,
             right_type = get_proper_type(right_type)
             if isinstance(right_type, UnionType):
                 right_variants = [(item, TempNode(item, context=context))
-                                  for item in right_type.relevant_items()]
-
+                                  for item in flatten_nested_unions(right_type.relevant_items(),
+                                                                    handle_type_alias_type=True)]
             msg = self.msg.clean_copy()
             msg.disable_count = 0
             all_results = []
diff --git a/mypy/types.py b/mypy/types.py
--- a/mypy/types.py
+++ b/mypy/types.py
@@ -2231,15 +2231,19 @@ def has_type_vars(typ: Type) -> bool:
     return typ.accept(HasTypeVars())
 
 
-def flatten_nested_unions(types: Iterable[Type]) -> List[Type]:
+def flatten_nested_unions(types: Iterable[Type],
+                          handle_type_alias_type: bool = False) -> List[Type]:
     """Flatten nested unions in a type list."""
     # This and similar functions on unions can cause infinite recursion
     # if passed a "pathological" alias like A = Union[int, A] or similar.
     # TODO: ban such aliases in semantic analyzer.
     flat_items = []  # type: List[Type]
+    if handle_type_alias_type:
+        types = get_proper_types(types)
     for tp in types:
         if isinstance(tp, ProperType) and isinstance(tp, UnionType):
-            flat_items.extend(flatten_nested_unions(tp.items))
+            flat_items.extend(flatten_nested_unions(tp.items,
+                              handle_type_alias_type=handle_type_alias_type))
         else:
             flat_items.append(tp)
     return flat_items


Test patch:
diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test
--- a/test-data/unit/check-unions.test
+++ b/test-data/unit/check-unions.test
@@ -1013,3 +1013,18 @@ y: Union[int, Dict[int, int]] = 1 if bool() else {}
 u: Union[int, List[int]] = [] if bool() else 1
 v: Union[int, Dict[int, int]] = {} if bool() else 1
 [builtins fixtures/isinstancelist.pyi]
+
+[case testFlattenTypeAliasWhenAliasedAsUnion]
+from typing import Union
+
+T1 = int
+T2 = Union[T1, float]
+T3 = Union[T2, complex]
+T4 = Union[T3, int]
+
+def foo(a: T2, b: T2) -> T2:
+    return a + b
+
+def bar(a: T4, b: T4) -> T4:  # test multi-level alias
+    return a + b
+[builtins fixtures/ops.pyi]