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

Dependency fails when provided as generator #12

Open
joeblackwaslike opened this issue Apr 4, 2022 · 1 comment
Open

Dependency fails when provided as generator #12

joeblackwaslike opened this issue Apr 4, 2022 · 1 comment

Comments

@joeblackwaslike
Copy link

def get_db():
    db = current_app.extensions['sqlalchemy'].db
    try:
        yield db.session
    except Exception:
        db.session.rollback()
    finally:
        db.session.close()


@dependant
def create_model(data: MagicClientCreate, db: Session = Depends(get_db)):
    magic_client = MagicClient(**data.dict())
    db.add(magic_client)
    db.commit()
    db.refresh(magic_client)
    return magic_client

Fails with the following error:

AttributeError: 'generator' object has no attribute 'add'

I see towards the end of solve_dependencies generator support was commmented out

        else:
            solved = await run_in_threadpool(call, **sub_values)
        if sub_dependant.name is not None:
            values[sub_dependant.name] = solved
        if sub_dependant.cache_key not in dependency_cache:
            dependency_cache[sub_dependant.cache_key] = solved
        """
        # generator support
        elif is_gen_callable(call) or is_async_gen_callable(call):
            stack = scope.get("fastapi_astack") if scope else None
            if stack is None:
                raise RuntimeError(
                    asynccontextmanager_error_message
                )  # pragma: no cover
            solved = await solve_generator(
                call=call, stack=stack, sub_values=sub_values
            )
        """

    return values, errors, dependency_cache

I'm curious why this is, this is the most common use case for Depends in FastAPI?

@joeblackwaslike
Copy link
Author

To clarify, perhaps I can solve whatever problem you ran into before, provided I'm able to gain enough context into the issue. :)

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

1 participant