You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
URL to code causing the issue
No response
MCVE
# in the file dummy.pyasyncdefget_dummy() ->"DummyClass":
returnDummyClass()
classDummyClass:
def__init__(self) ->None:
self.some_property="test"# in the file handler.pyfromlitestar.diimportProvidefromlitestarimportwebsocket_listenerfromdummyimportget_dummyfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:
fromdummyimportDummyClass@websocket_listener("/session/push", dependencies={"dummy_class": Provide(get_dummy)})asyncdefwebsocket_dummy(data: str, dummy_class: "DummyClass") ->str:
returndummy_class.some_property# in the file main.pyfromlitestarimportLitestarfromhandlerimportwebsocket_dummyfromdummyimportDummyClassapp=Litestar(route_handlers=[websocket_dummy], signature_types=[DummyClass])
Steps to reproduce
1. Run the app
2. A NameError with name 'DummyClass' is not defined is triggered
3. see Stacktrace in logs
Screenshots
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
Logs
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
Description
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.
URL to code causing the issue
No response
MCVE
Steps to reproduce
Screenshots
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
Logs
Litestar Version
2.9.1
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: