pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Pyright hangs with `reduce(lambda x, y: (x, y), ...)`

Open kbouchiat opened this issue 1 year ago • 2 comments

Description Pyright appears to hang indefinitely when calling functools.reduce with a tuple-ifying lambda.

Code

import functools

functools.reduce(lambda x, y: (x, y), range(3))

Environment

The code above was tested from the command-line, but is also verified to hang the language servers (for VS Code, Zed, ...).

$ pyright --version
pyright 1.1.390

kbouchiat avatar Dec 07 '24 12:12 kbouchiat

This looks the same as https://github.com/microsoft/pyright/issues/9536.

The reason why it hangs in your example and mine is because we're returning a tuple from the lambda.

If I change the return type in your example to list (functools.reduce(lambda x, y: [x, y], range(3))), here's what pyright reports:

Type parameter "_T_co@Iterable" is covariant, but "int" is not a subtype of "list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[<Recursive>]]]]]]]]]]]]]]]]]]]]]"
  "int" is not assignable to "list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[<Recursive>]]]]]]]]]]]]]]]]]]]]]"

Similar type gets revealed in my example too, if I change the return type to list:

def f[T](c: Callable[[T], T]) -> T: ...

reveal_type(f(lambda v: [v, v]))
Type of "f(lambda v: [v, v])" is "list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[list[<Recursive>]]]]]]]]]]]]]]]]]]]]]"

So I think the root cause is the same.

LeeeeT avatar Dec 07 '24 14:12 LeeeeT

This is addressed in pyright 1.1.391.

erictraut avatar Dec 18 '24 02:12 erictraut