Skip to content

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

Merged
tiangolo merged 3 commits intomasterfrom
starlette-lifespan-types
Mar 14, 2023
Merged

🎨 Fix types for lifespan, upgrade Starlette to 0.26.1#9245
tiangolo merged 3 commits intomasterfrom
starlette-lifespan-types

Conversation

@tiangolo
Copy link
Copy Markdown
Member

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

@tiangolo tiangolo added the bug Something isn't working label Mar 12, 2023
@github-actions
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@adriangb
Copy link
Copy Markdown
Contributor

You also need to add self: AppType

@tiangolo
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

@tiangolo
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@tiangolo
Copy link
Copy Markdown
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