I have a bit of a weird issue right now. If I use type annotation of a class without importing the class locally the app will not start, although the forward ref is passed specifically for websocket_listener, regular HTTP handler work.
Currently not a critical issue, but worth checking out I think.
No response
# in the file dummy.py
async def get_dummy() -> "DummyClass":
return DummyClass()
class DummyClass:
def __init__(self) -> None:
self.some_property = "test"
# in the file handler.py
from litestar.di import Provide
from litestar import websocket_listener
from dummy import get_dummy
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dummy import DummyClass
@websocket_listener("/session/push", dependencies={"dummy_class": Provide(get_dummy)})
async def websocket_dummy(data: str, dummy_class: "DummyClass") -> str:
return dummy_class.some_property
# in the file main.py
from litestar import Litestar
from handler import websocket_dummy
from dummy import DummyClass
app = Litestar(route_handlers=[websocket_dummy], signature_types=[DummyClass])
1. Run the app
2. A NameError with name 'DummyClass' is not defined is triggered
3. see Stacktrace in logs
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
File "/root/innovation-framework/main.py", line 2, in <module>
from handler import websocket_dummy
File "/root/innovation-framework/handler.py", line 12, in <module>
@websocket_listener("/session/push", dependencies={"dummy_class": Provide(get_dummy)})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/innovation-framework/.venv/lib/python3.12/site-packages/litestar/handlers/websocket_handlers/listener.py", line 230, in __call__
parsed_signature = ParsedSignature.from_fn(fn, self.resolve_signature_namespace())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/innovation-framework/.venv/lib/python3.12/site-packages/litestar/utils/signature.py", line 214, in from_fn
fn_type_hints = get_fn_type_hints(fn, namespace=signature_namespace)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/innovation-framework/.venv/lib/python3.12/site-packages/litestar/utils/signature.py", line 167, in get_fn_type_hints
hints = get_type_hints(fn_to_inspect, globalns=namespace, include_extras=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/innovation-framework/.venv/lib/python3.12/site-packages/typing_extensions.py", line 1230, in get_type_hints
hint = typing.get_type_hints(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/typing.py", line 2301, in get_type_hints
hints[name] = _eval_type(value, globalns, localns, type_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/typing.py", line 415, in _eval_type
return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/typing.py", line 938, in _evaluate
eval(self.__forward_code__, globalns, locals_to_pass),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
NameError: name 'DummyClass' is not defined
2.9.1
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too