Skip to content

Bug: Signature types issue, NameError #4870

Description

@floxay

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

  1. Run main.py
  2. See NameError: NameError: name 'NamedDependency' is not defined

Litestar Version

2.24.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions