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

Wrong None detection. #4743

Closed
Hudrolax opened this issue Aug 21, 2023 · 4 comments
Closed

Wrong None detection. #4743

Hudrolax opened this issue Aug 21, 2023 · 4 comments
Assignees
Labels
duplicate This issue or pull request already exists

Comments

@Hudrolax
Copy link

from asyncpg import Connection, Record

async def get_user_settings(conn: Connection, user_id: int) -> Record | None:
    """The function gets user settings."""
    result = await conn.fetchrow("""
        SELECT * FROM user_settings WHERE user_id = $1
    """, user_id)
    return result

After update, pylance says "None" is not awaitable. But I clearly defined the type. With the old version, I have no problem with this piece of code.

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Aug 21, 2023
@erictraut
Copy link
Contributor

Thanks for the bug report. I think this is already fixed in last week's pre-release version of pylance, and the fix will be in this week's production version. You can temporarily switch to the pre-release version of pylance, and the error should go away.

@Hudrolax
Copy link
Author

Thank you! I confirm, the bug was fixed in the last pre-release version.

@Elchinchel
Copy link

I dont know if this related to this issue, but seems similar.

In release (2023.8.30) version i have bug with this code:

class Param:
    bar: int

async def foo(param):
    return param.bar

async def bar():
    reveal_type(foo)  # Type of "foo" is "(param: Unknown) -> Coroutine[Any, Any, Unknown]"
    reveal_type(foo(int()))  # Type of "foo(int())" is "Unknown"  # Just as example of param without 'bar' attribute
    reveal_type(foo(Param()))  # Type of "foo(Param())" is "int"

    await foo(Param())  # "int" is not awaitable

Switching to pre-release version fix issue, but there is another one:

async def foo():
    reveal_type(foo)  # Type of "foo" is "() -> Coroutine[Any, Any, None]"
    reveal_type(foo())  # Type of "foo()" is "None"

    await foo()  # "None" is not awaitable

Hope this helps.

@erictraut
Copy link
Contributor

@Elchinchel, the last code sample that you posted above is a duplicate of microsoft/pyright#5745, which will be fixed in this week's pylance pre-release build. It's specific to recursive coroutine calls. You can work around it in the meantime by adding an explicit -> None return type annotation to the function.

@heejaechang heejaechang added duplicate This issue or pull request already exists and removed needs repro Issue has not been reproduced yet labels Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

4 participants