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: App import not working on doc cli when importing from other directory #1087

Closed
reritom opened this issue Dec 22, 2023 · 7 comments · Fixed by #1088
Closed

Bug: App import not working on doc cli when importing from other directory #1087

reritom opened this issue Dec 22, 2023 · 7 comments · Fixed by #1088
Labels
bug Something isn't working

Comments

@reritom
Copy link

reritom commented Dec 22, 2023

Describe the bug
This behaviour was working before, but seems to have stopped working from 0.3.7 onward.

This is sort of blocking me, because trying to use older versions of faststream aren't working due pinning issues (Lancetnik/FastDepends#51) with fast-depends, and I haven't managed to find a working combination of faststream+fastdepends manually.

How to reproduce
Include source code:

# app/__init__.py
from faststream import FastStream

faststream_app = FastStream("My app")
# main.py
from app import faststream_app

__all__ = ["faststream_app"]
├── app
 │  └── __init__.py
└── main.py

And/Or steps to reproduce the behavior:

  1. Create the two above files. app/__init__ is like a local module, and main.py is an entrypoint that consumes that local module.
  2. In the directory of main.py run faststream docs gen main:faststream_app
  3. See the error ModuleNotFoundError: No module named 'app. Note that running python main.py has no import error, also adding init on the same level as main.py doesn't help (but probably shouldn't anyway). Adding the directory of main.py to PATH doesn't seem to help either.
Traceback (most recent call last):

  File "/Users/me/Documents/Projects/test/.venv/lib/python3.9/site-packages/faststream/cli/utils/imports.py", line 129, in import_from_string
    module = importlib.import_module(  # nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import

  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)

  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

  File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked

ModuleNotFoundError: No module named 'main'


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/Users/me/Documents/Projects/test/.venv/bin/faststream", line 8, in <module>
    sys.exit(cli())

  File "/Users/me/Documents/Projects/test/.venv/lib/python3.9/site-packages/faststream/cli/docs/app.py", line 90, in gen
    _, app_obj = import_from_string(app)

  File "/Users/me/Documents/Projects/test/.venv/lib/python3.9/site-packages/faststream/cli/utils/imports.py", line 135, in import_from_string
    instance = try_import_app(module_path, app_name)

  File "/Users/me/Documents/Projects/test/.venv/lib/python3.9/site-packages/faststream/cli/utils/imports.py", line 27, in try_import_app
    app_object = import_object(module, app)

  File "/Users/me/Documents/Projects/test/.venv/lib/python3.9/site-packages/faststream/cli/utils/imports.py", line 70, in import_object
    loader.exec_module(mod)

  File "<frozen importlib._bootstrap_external>", line 850, in exec_module

  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

  File "/Users/me/Documents/Projects/test/main.py", line 1, in <module>
    from app import faststream_app

ModuleNotFoundError: No module named 'app'

Expected behavior
The docs should be generated

Observed behavior
Import error

Environment
Running FastStream 0.3.9 with CPython 3.9.12 on Darwin

@Lancetnik
Copy link
Collaborator

@reritom the last update for CLI import rules was 0.2.15.
Are you sure about 0.3.7 breaking changes?

@reritom
Copy link
Author

reritom commented Dec 22, 2023

You're right. 0.3.7 looked like the breaking change, but it seems it was because Lancetnik/FastDepends#51 was causing an error which I misinterpreted. I imagine you are correct that this issue comes from 0.2.15

@reritom
Copy link
Author

reritom commented Dec 22, 2023

Just checked at it seems the change in 0.2.14 is what caused the issue. 0.2.13 is the last version where it works.

I am unblocked for now by pinning to 0.2.13 and fast-depends 0.2.8.

@Lancetnik
Copy link
Collaborator

@reritom I am just checked it with 0.3.9 (latest) and it works fine, can you check it again, please?

@reritom
Copy link
Author

reritom commented Dec 22, 2023

In a fresh venv with the following dependencies I am still encountering this issue.

(.venv) tom:test $ pip freeze
annotated-types==0.6.0
anyio==4.2.0
click==8.1.7
exceptiongroup==1.2.0
fast-depends==2.2.8
faststream==0.3.9
idna==3.6
pydantic==2.5.3
pydantic_core==2.14.6
sniffio==1.3.0
typer==0.9.0
typing_extensions==4.9.0
uvloop==0.19.0

@Lancetnik
Copy link
Collaborator

Lancetnik commented Dec 22, 2023

Well, I found the problem and solution too
Wait for fix a bit, I'll introduce it as a part of 0.3.10
Thanks for the report!

@reritom
Copy link
Author

reritom commented Dec 22, 2023

Perfect, thank you

github-merge-queue bot pushed a commit that referenced this issue Dec 23, 2023
* fix (#1087): add app_dir option to docs gen & serve commands

* lint: raise TypeError correctly

* lint: if TYPE_CHECKING imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants