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

🎨 Fix types for lifespan, upgrade Starlette to 0.26.1 #9245

Merged
merged 3 commits into from
Mar 14, 2023

Conversation

tiangolo
Copy link
Member

🎨 Fix types for lifespan, depends on encode/starlette#2077

@tiangolo tiangolo added the bug Something isn't working label Mar 12, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Mar 12, 2023

@github-actions github-actions bot temporarily deployed to pull request March 12, 2023 11:59 Inactive
@github-actions
Copy link
Contributor

📝 Docs preview for commit 2e373fa at: https://640dbed4fbc9f768d2b35cd4--fastapi.netlify.app

@adriangb
Copy link
Contributor

You also need to add self: AppType

@tiangolo
Copy link
Member Author

Good point, thanks @adriangb! Trying it locally I realized I had to redefine AppType to be bound to FastAPI as well.

@adriangb
Copy link
Contributor

Hmm I’m surprised the bound=Starlette should work and the fact that you have self: AppType should make the AppType always match self which is going to be a FastAPI instance.

@github-actions github-actions bot temporarily deployed to pull request March 14, 2023 01:48 Inactive
@github-actions
Copy link
Contributor

📝 Docs preview for commit 4578cfb at: https://640fd2b293def71708f521d2--fastapi.netlify.app

@tiangolo
Copy link
Member Author

I'm trying with this example:

from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager

from fastapi import FastAPI


@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncGenerator[None, None]:
    ...
    yield
    ...


app = FastAPI(lifespan=lifespan)

And I get this error:

mypy sand.py
fastapi/applications.py:112: error: "AppType" has no attribute "openapi_schema"
fastapi/applications.py:113: error: "AppType" has no attribute "openapi_url"
fastapi/applications.py:114: error: "AppType" has no attribute "title"
fastapi/applications.py:115: error: "AppType" has no attribute "version"
fastapi/applications.py:126: error: "AppType" has no attribute "dependency_overrides"
fastapi/applications.py:153: error: "AppType" has no attribute "setup"
Found 6 errors in 1 file (checked 1 source file)

...I guess it would be fine for external things to have AppType be bound to Starlette, but for the internals of FastAPI it seems to be a problem. But I guess it would be fine then to have the custom AppType here.

@tiangolo tiangolo changed the title 🎨 Fix types for lifespan, depends on https://github.com/encode/starlette/pull/2077 🎨 Fix types for lifespan, upgrade Starlette to 0.26.1 Mar 14, 2023
@tiangolo tiangolo merged commit 25aabe0 into master Mar 14, 2023
@tiangolo tiangolo deleted the starlette-lifespan-types branch March 14, 2023 02:19
@adriangb
Copy link
Contributor

@tiangolo
Copy link
Member Author

Ah, yeah, the error is not in the lifespan, but that the class doesn't have the custom attributes FastAPI defines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants