Incorrect constraint inference for unions with nested generics 路 Issue #9435 路 python/mypy (original) (raw)

Skip to content

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

@Jackenmen

Description

@Jackenmen

馃悰 Bug Report

From my search, it looks like an issue similar to #6417, but happening to unions of generic and nested generic.
mypy has troubles with inferring a proper constraint and passing a nested generic to function that should accept such nested generic fails.
The issue can be worked around with @overloads.

To Reproduce

Minimum reproduction:

from typing import List, TypeVar, Union

_T = TypeVar("_T")

def func(param: Union[List[_T], List[List[_T]]]) -> _T: ...

func([1]) func([[1]])

A more real-live example of where this is an issue is tornado's gen function (overloads there are the part of workaround for this issue):
https://github.com/tornadoweb/tornado/pull/2909/files

Expected Behavior

No errors.

Actual Behavior

main.py:10: error: Argument 1 to "func" has incompatible type "List[List[int]]"; expected "Union[List[<nothing>], List[List[<nothing>]]]"

Your Environment