Description
signature_types does not make a TYPE_CHECKING-only name resolvable, even for
a bare-name annotation (in some cases?) and will result in a NameError.
I noticed this while upgrading Litestar and porting a codebase to Python 3.14.
MCVE
requirements for this example: uv pip install litestar[sqlalchemy,standard] aiosqlite
main.py
import uvicorn
from litestar import Litestar
from litestar.di import NamedDependency
from litestar.plugins.sqlalchemy import SQLAlchemyAsyncConfig, SQLAlchemyInitPlugin
from sqlalchemy.ext.asyncio import AsyncSession
from handler import handler
config = SQLAlchemyAsyncConfig(connection_string="sqlite+aiosqlite:///async.sqlite")
plugin = SQLAlchemyInitPlugin(config=config)
app = Litestar(
route_handlers=[handler],
plugins=[plugin],
signature_types=[
AsyncSession,
NamedDependency,
],
)
uvicorn.run(app)
handler.py
from typing import TYPE_CHECKING
from litestar import get
if TYPE_CHECKING:
from litestar.di import NamedDependency
from sqlalchemy.ext.asyncio import AsyncSession
@get("/")
async def handler(
db_session: NamedDependency[AsyncSession], # noqa: ARG001
) -> None:
return
Steps to reproduce
- Run main.py
- See NameError:
NameError: name 'NamedDependency' is not defined
Litestar Version
2.24.0
Description
signature_typesdoes not make aTYPE_CHECKING-only name resolvable, even fora bare-name annotation (in some cases?) and will result in a
NameError.I noticed this while upgrading Litestar and porting a codebase to Python 3.14.
MCVE
requirements for this example:
uv pip install litestar[sqlalchemy,standard] aiosqlitemain.pyhandler.pySteps to reproduce
NameError: name 'NamedDependency' is not definedLitestar Version
2.24.0