Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: type annotation needs local import to work with websocket_listener #3594

Open
1 of 4 tasks
TheZwieback opened this issue Jun 24, 2024 · 0 comments
Open
1 of 4 tasks
Labels
Bug 🐛 This is something that is not working as expected

Comments

@TheZwieback
Copy link

TheZwieback commented Jun 24, 2024

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

# 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])

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

Litestar Version

2.9.1

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

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

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@TheZwieback TheZwieback added the Bug 🐛 This is something that is not working as expected label Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

1 participant