-
-
Notifications
You must be signed in to change notification settings - Fork 387
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: CLI - litestar run
raises 'Scope' is not defined
when using Request
in type annotation
#3895
Comments
I am not able to reproduce this, neither by cloning the MCVE repo and running the suggested docker command nor by trying to replicate the steps I assumed are relevant. Could you please provide a self-contained example (ideally just a snippet of code) that produces the described behaviour? |
litestar run
raises 'Scope' is not defined
when using Request
in type annotation
I wasn't able to reproduce on MacOS but Centos7 showed me the same error (3.10.0-1160.118.1.el7.x86_64) |
that Centos7 is on an AMD cpu, but I can reproduce on an Intel CPU with Debian 12.5 (6.1.0-21-amd64) |
I can reproduce just running locally, still not sure what's happening but in the meantime if you modify your |
so in essence the way I understand what's happening here is:
i'm not sure how to tackle this:
|
It doesn't load the wrong thing really. It does what it's supposed to do. The logic it follows is this:
The issue occurs within the last step, specifically on this line: litestar/litestar/cli/_utils.py Lines 339 to 341 in b79519d
We're using However, there's a more fundamental issue with this approach as well: We can never be sure that Maybe we should just avoid |
@provinzkraut it's still not clear to me what's wrong with the MCVE or why it only happens in certain environments |
I'm not sure about that either. Especially running in docker, I would absolutely expect this to work and, unless I'm severely mistaken, the cause should absolutely be independent of platform or even Python version |
yeah it's quite a mistery 🤷 at first I thought I had something weird on my pip cache or some hidden file or environment variable that was causing the issue. I can add that I see the exception regardless of the value of the parameter |
I found the reason for the inconsistent behavior across the systems: it's the order of the files found when iterating over the I found that on an ARM machine the bug does not happen, while on my computer does, so I used a debugger to run the code side by side to find where they diverge. Litestar by default checks the Doing so it calls iterdir which according to Python documentation has an "arbitrary" order. On the ARM instance the order of the iteration for the MCVE repo is this: This explains why the bug cannot be reproduced reliably, it depends on the order in which the filesystem returns the files which is arbitrary. It can change even by moving the folder around, which caused me a lot of confusion during troubleshooting :) The behavior can be made predictable by replacing the For my case, I "solved" by importing the modules this way: from typing import TYPE_CHECKING
from litestar import Router, post
if TYPE_CHECKING:
from litestar import Request
from litestar.datastructures import State and then using string for the type hints: @post("/log")
async def log(
data: dict[str, str], request: "Request[None, None, State]", headers: dict[str, str]
) -> dict[str, bool]: this way I think a relatively safe way to avoid this behavior (which can be very nasty to google and troubleshoot, since the stack trace doesn't even show where the import is happening) is to add this before the call to if value.__module__.startswith("litestar."):
# maybe also show a warning?
continue this way any import to Litestar itself will not be examined for autodiscovery. Still it creates an hidden dependency in case in the future a Litestar function is supposed to be "autodiscovered". A nicer solution would be to be able to reproduce the |
Thinking more about it, it could be better to just catch this exception during discovery and show a warning when there's a |
Kudos for the low level debugging @jacopofar! |
Another possible way to make autodiscovery more reliable is to collect all of the apps found and check that there's only one after the folders have been scanned. This would avoid both this unpredictable behavior (not the error itself, just the fact it manifests randomly), detect the case in which there are multiple apps defined (which also is unpredictable at the moment) and keep the lazy iteration. I can do a PR for that too, it's a small change. |
Thanks @jacopofar for the great write up! I think there are two things to consider here:
|
Why should one not use autodiscovery in production? Indeed setting the variable with I think at this point the bug is understood and can be closed, I'd like to go on and implement some check to better report the issue to the user (i.e. report which file caused the issue), even without avoiding it, because this is almost inevitable if using |
Description
Hello, not 100% sure this is a bug, probably I'm missing something.
My app started breaking when I added an import like
from litestar import Request
orfrom litestar.datastructures import State
, both imports are used only to add type hints to my app code.Running
litestar run
now gives an exception like this:Complete trace
investigating a bit I found that it fails when I import that module because, for what I understand, Litestar is scanning the modules to find the CLI commands as explained in the docs, and doing so tries to get their types. It invokes
typing_extension
which callstyping.get_type_hints
from the standard library, which iterates over the elements in__mro__
for theRequest
class and findsASGIConnection
. This class has a scope member of type Scope which is a type that is imported only for type checking so at runtime it cannot be found.A similar thing happens with
from litestar.datastructures import State
, which I use for type hints.I'm very confused by this, I think this is a basic function and indeed if I create an app from scratch (like the hello world on the homepage) and import this there's no issue. Maybe something else is interfering.
URL to code causing the issue
https://github.com/jacopofar/litestar-mcve
MCVE
Steps to reproduce
Screenshots
No response
Logs
Litestar Version
2.13.0
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: