Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type inference when a lambda is conditionally reassigned #8229

Closed
wch opened this issue Jun 25, 2024 · 2 comments
Closed

Incorrect type inference when a lambda is conditionally reassigned #8229

wch opened this issue Jun 25, 2024 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@wch
Copy link

wch commented Jun 25, 2024

With pyright 1.1.369, I'm running into a problem where if a lambda function is reassigned within an if statement, it can cause an incorrect inferred type of an async function that calls that lambda. Best to just illustrate with an example:

import asyncio


def foo(replace_inner: bool) -> None:
    inner = lambda: None

    async def wrapper():
        inner()

    wrapped_fn = wrapper()
    asyncio.create_task(wrapped_fn)

    if replace_inner:
        inner = lambda: None

When I run pyright on it, I get the following:

❯ pyright test.py
/Users/winston/test.py
  /Users/winston/test.py:11:25 - error: Argument of type "None" cannot be assigned to parameter "coro" of type "_CoroutineLike[_T@create_task]" in function "create_task"
    Type "None" is incompatible with type "_CoroutineLike[_T@create_task]"
      "None" is incompatible with "Generator[Any, None, _T@create_task]"
      "None" is incompatible with "Coroutine[Any, Any, _T@create_task]" (reportArgumentType)
1 error, 0 warnings, 0 informations 

When I hover over warpped_fn, it says the the type is None, when it should be Coroutine[Any, Any, None]:

image

Note that the following three modified versions of the code do not have this error (the change in each function has a comment above it):

def foo2(replace_inner: bool) -> None:
    inner = lambda: None

    # Unlike in foo(), this inner function now has an explicit return type.
    async def wrapper() -> None:
        inner()

    wrapped_fn = wrapper()
    asyncio.create_task(wrapped_fn)

    if replace_inner:
        inner = lambda: None



def foo3(replace_inner: bool) -> None:
    inner = lambda: None

    async def wrapper():
        inner()

    wrapped_fn = wrapper()
    asyncio.create_task(wrapped_fn)

    # Unlike in foo(), the following line is not in an if statement.
    inner = lambda: None


def foo4(replace_inner: bool) -> None:
    # This time inner is a def function, instead of a lambda.
    def inner() -> None:
        return

    async def wrapper():
        inner()

    wrapped_fn = wrapper()
    asyncio.create_task(wrapped_fn)

    if replace_inner:
        inner = lambda: None
@wch wch added the bug Something isn't working label Jun 25, 2024
@erictraut
Copy link
Collaborator

Thanks for the bug report. I'm able to repro, and I've confirmed that it doesn't occur in 1.1.368. I'll investigate further.

erictraut added a commit that referenced this issue Jun 27, 2024
…unction with an inferred return type is a coroutine (async) and is referenced within the function body in which it's declared. This addresses #8232 and #8229.
erictraut added a commit that referenced this issue Jun 27, 2024
…unction with an inferred return type is a coroutine (async) and is referenced within the function body in which it's declared. This addresses #8232 and #8229. (#8251)
@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Jun 27, 2024
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.370

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants