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 typing? #99

Closed
pySilver opened this issue Sep 13, 2024 · 5 comments · Fixed by #103
Closed

Wrong typing? #99

pySilver opened this issue Sep 13, 2024 · 5 comments · Fixed by #103

Comments

@pySilver
Copy link

Following tutorial I saw that Pyright complains about typing. Indeed types are little different in runtime.

from django.apps import AppConfig
from django_asgi_lifespan.register import register_lifespan_manager

from config.context import lifespan_manager

class CoreConfig(AppConfig):
    name = "project.core"

    def ready(self):
        register_lifespan_manager(context_manager=lifespan_manager)  # <-- here is the breakpoint

lifespan_manager is a simple callable up until it is called:

type(lifespan_manager)
PyDev console: using IPython 8.26.0
Out[1]: function
type(lifespan_manager())
Out[2]: contextlib._AsyncGeneratorContextManager
@illagrenan
Copy link
Owner

Thanks for the report. Could you please send me the error that Pyright is reporting? Thanks!

@pySilver
Copy link
Author

pySilver commented Sep 15, 2024

@illagrenan here is my context.py which is slightly different than one in your examples (since we return generator, not a mapping) in fact:

from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING

import aiohttp

if TYPE_CHECKING:
    from django_asgi_lifespan.types import State


@asynccontextmanager
async def lifespan_manager() -> AsyncGenerator["State", None]:
    state: State = {
        "aiohttp_client": aiohttp.ClientSession(),
    }

    try:
        yield state
    finally:
        await state["aiohttp_client"].close()

and here is my apps.py:

from django.apps import AppConfig
from django_asgi_lifespan.register import register_lifespan_manager

from config.context import lifespan_manager


class CoreConfig(AppConfig):
    name = "mybaze.core"

    def ready(self):
        register_lifespan_manager(
            context_manager=lifespan_manager,  # pyright: ignore [reportArgumentType] # https:/illagrenan/django-asgi-lifespan/issues/99
        )

Here is full error without suppression:
Argument of type "() -> _AsyncGeneratorContextManager[State]" cannot be assigned to parameter "context_manager" of type "LifespanManager" in function "register_lifespan_manager"   "function" is incompatible with protocol "AsyncContextManager[MutableMapping[str, Any]]"     "__aenter__" is not present     "__aexit__" is not present Pyright (reportArgumentType)

P.s. Take my words with grain of salt - I'm not very experienced with complex type hinting in python yet.

@illagrenan
Copy link
Owner

illagrenan commented Oct 12, 2024

Thanks again for the report. You were right that the typing for LifespanManager was wrong. Originally, I didn't check the example codes using mypy, that's now fixed. I've released a new version, so you can check if Pyright is now OK.

@pySilver
Copy link
Author

thanks! no complains anymore!

@illagrenan
Copy link
Owner

That's great! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants