Skip to content

Incorrect constraint inference for unions with nested generics #9435

Open

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

  • Mypy version used: 0.781
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8.5
  • Operating system and version: Windows 10
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