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

NameError: name 'AsyncSession' is not defined #248

Open
tyki6 opened this issue Jul 29, 2023 · 1 comment
Open

NameError: name 'AsyncSession' is not defined #248

tyki6 opened this issue Jul 29, 2023 · 1 comment

Comments

@tyki6
Copy link

tyki6 commented Jul 29, 2023

When I type my dependencies, I got an error
Example:

from sqlalchemy.ext.asyncio import AsyncSession
from fastapi_cache.decorator import cache
from fastapi import APIRouter, Depends

user_router = APIRouter(
    prefix="/users",
    tags=["users"],
)

@user_router.get(
    "/me/contact",
)
@cache(expire=60 * 60 * 12)
async def get_user_router(
    session: AsyncSession = Depends(get_db_async),
) -> User:
  return await session.get(User, 1)

Error Log:

  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/tyki6/home/sulico/sulico/sulico-back/main.py", line 39, in <module>
    from src.user.user_router import user_router
  File "/home/tyki6/home/sulico/sulico/sulico-back/src/user/user_router.py", line 202, in <module>
    @user_router.get(
     ^^^^^^^^^^^^^^^^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/routing.py", line 669, in decorator
    self.add_api_route(
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/routing.py", line 608, in add_api_route
    route = route_class(
            ^^^^^^^^^^^^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/routing.py", line 454, in __init__
    self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 292, in get_dependant
    endpoint_signature = get_typed_signature(call)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 252, in get_typed_signature
    typed_params = [
                   ^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 257, in <listcomp>
    annotation=get_typed_annotation(param.annotation, globalns),
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tyki6/.cache/pypoetry/virtualenvs/sulico-back-pBwmz66F-py3.11/lib/python3.11/site-packages/fastapi/dependencies/utils.py", line 268, in get_typed_annotation
    annotation = evaluate_forwardref(annotation, globalns, globalns)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pydantic/typing.py", line 66, in pydantic.typing.evaluate_forwardref
  File "/home/tyki6/.pyenv/versions/3.11.4/lib/python3.11/typing.py", line 877, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'AsyncSession' is not defined

if I omit AsyncSession for session type with session = Depends(get_db_async) everything is ok.
Anyone can tell me how to solve that?

@tyki6 tyki6 changed the title AsyncSession is not defined NameError: name 'AsyncSession' is not defined Jul 29, 2023
@mkdir700
Copy link
Contributor

Have you not installed SQLAlchemy for the extension dependency of asynchronous functionality?

Use the following command to install it:

pip install sqlalchemy[asyncio]

Reference:https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#asyncio-platform-installation-notes-including-apple-m1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants