pyright icon indicating copy to clipboard operation
pyright copied to clipboard

pyright 1.1.300+: inconsistent type inference depending on order of `Union` members

Open pinterior opened this issue 3 years ago • 2 comments

Describe the bug

destructing Union by generic function results unexpected type inference. the behavior seems depending on type names or order of Union members.

To Reproduce

from typing import Iterable, Tuple, Type, TypeVar, Union

A = TypeVar("A")
B = TypeVar("B")

def d(u: Union[A, B], a: Type[A], b: Type[B]) -> Tuple[Iterable[A], Iterable[B]]:
    ...

class Eins:
    pass

class Zwei:
    pass

class Drei:
    pass

def f12(x: Union[Eins, Zwei]):
    return d(x, Eins, Zwei)

def f13(x: Union[Eins, Drei]):
    return d(x, Eins, Drei)

def f21(x: Union[Zwei, Eins]):
    return d(x, Zwei, Eins)

def f23(x: Union[Zwei, Drei]):
    return d(x, Zwei, Drei)

def f31(x: Union[Drei, Eins]):
    return d(x, Drei, Eins)

def f32(x: Union[Drei, Zwei]):
    return d(x, Drei, Zwei)

reveal_type(f12)
reveal_type(f13)
reveal_type(f21)
reveal_type(f23)
reveal_type(f31)
reveal_type(f32)
$ PYRIGHT_PYTHON_FORCE_VERSION=1.1.299 pyright de.py
(snip)
de.py
  de.py:36:13 - information: Type of "f12" is "(x: Eins | Zwei) -> Tuple[Iterable[Eins], Iterable[Zwei]]"
  de.py:37:13 - information: Type of "f13" is "(x: Eins | Drei) -> Tuple[Iterable[Eins], Iterable[Drei]]"
  de.py:38:13 - information: Type of "f21" is "(x: Zwei | Eins) -> Tuple[Iterable[Zwei], Iterable[Eins]]"
  de.py:39:13 - information: Type of "f23" is "(x: Zwei | Drei) -> Tuple[Iterable[Zwei], Iterable[Drei]]"
  de.py:40:13 - information: Type of "f31" is "(x: Drei | Eins) -> Tuple[Iterable[Drei], Iterable[Eins]]"
  de.py:41:13 - information: Type of "f32" is "(x: Drei | Zwei) -> Tuple[Iterable[Drei], Iterable[Zwei]]"
$ PYRIGHT_PYTHON_FORCE_VERSION=1.1.311 pyright de.py
(snip)
de.py
  de.py:36:13 - information: Type of "f12" is "(x: Eins | Zwei) -> Tuple[Iterable[Eins], Iterable[Zwei]]"
  de.py:37:13 - information: Type of "f13" is "(x: Eins | Drei) -> Tuple[Iterable[Drei | Eins], Iterable[Eins | Drei]]"
  de.py:38:13 - information: Type of "f21" is "(x: Zwei | Eins) -> Tuple[Iterable[Eins | Zwei], Iterable[Zwei | Eins]]"
  de.py:39:13 - information: Type of "f23" is "(x: Zwei | Drei) -> Tuple[Iterable[Drei | Zwei], Iterable[Zwei | Drei]]"
  de.py:40:13 - information: Type of "f31" is "(x: Drei | Eins) -> Tuple[Iterable[Drei], Iterable[Eins]]"
  de.py:41:13 - information: Type of "f32" is "(x: Drei | Zwei) -> Tuple[Iterable[Drei], Iterable[Zwei]]"

Expected behavior

return types same as 1.1.299 are inferred

VS Code extension or command-line

command-line tool, 1.1.311

pinterior avatar May 31 '23 13:05 pinterior

This is by design, so I don't consider it a bug.

You've created an ambiguous situation with the function d because of the union with two type variables A and B.

Prior to version 1.1.300, pyright was inconsistent in how it handled assignments of one union to another; it was previously based on the order in which the union was defined. Around version 1.1.300, I addressed this non-determinism by always sorting the elements in a union in a deterministic manner prior to performing the assignment.

The details of how subtypes are sorted is an internal implementation detail, but the sorting algorithm involves (among other things) class names. That way, the union Eins | Zwei will always be treated the same as the union Zwei | Eins for determining type compatibility and performing TypeVar assignments. In particular, they will both be treated as the subtype Eins followed by the subtype Zwei.

What you're perceiving as "inconsistent" behavior is actually a result of this deterministic and consistent sorting.

I recommend avoiding the use of unions with "naked" TypeVars because they often lead to ambiguous (and sometimes unexpected) solutions in the constraint solver.

erictraut avatar May 31 '23 15:05 erictraut

i understand, it's time to use something like Either[L, R].

pinterior avatar Jun 01 '23 04:06 pinterior