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

FAST002 + B008 when using DI with FastAPI and Ruff #850

Open
TojikCZ opened this issue Jan 31, 2025 · 2 comments
Open

FAST002 + B008 when using DI with FastAPI and Ruff #850

TojikCZ opened this issue Jan 31, 2025 · 2 comments

Comments

@TojikCZ
Copy link

TojikCZ commented Jan 31, 2025

Hi,
I am new to dependency_injector.
I am trying to use the FastAPI example code and ruff configured like so:

[tool.ruff.lint]
select = ["ALL"]
ignore = [
    "FIX", # ignore TODOs
    "TD",  # ignore TODOs syntax
    "D104",  # Missing docstring in public package (__init__.py)
    "D203",  # 1 blank line required before class docstring, incomp. with D211
    "D213",  # multi-line-summary-second-line D212

    # ruff warning, don't format these, conflicts with linter
    "COM812", # missing-trailing-comma
    "ISC001", # single-line-implicit-string-concatenation
]

I am getting B008 and FAST002

FastAPI dependency without `Annotated`
Do not perform function call `Depends` in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable

Note: I cannot reproduce the FAST002 on the dependency_injector FastAPI basic example, just the B008 shows up.

Anyway, I tried doing this

"""FastAPI widh dependency_injector example."""
from typing import Annotated

from dependency_injector import containers, providers
from dependency_injector.wiring import Provide, inject
from fastapi import Depends, FastAPI, Response, status


class Service:
    """A mock service."""

    async def process(self) -> str:
        """Return 'OK'."""
        return "OK"


class Container(containers.DeclarativeContainer):
    """Container providing the mock Service."""

    service = providers.Factory(Service)


app = FastAPI()

@app.api_route("/")
@inject
async def index(
        response: Response,
        service: Annotated[Service, Depends(Provide[Container.service])]
) -> dict[str, str]:
    """Verify that the injection works."""
    result = await service.process()
    response.status_code = status.HTTP_200_OK
    return {"result": result}

container = Container()
container.wire(modules=[__name__])

Now ruff is happy, but DI breaks.

AttributeError: 'Provide' object has no attribute 'process'

I tried other variations, but nothing worked.

Is there a way to write this so ruff and mypy are happy, and DI continues to work?
Thank you

@ZipFile
Copy link
Contributor

ZipFile commented Jan 31, 2025

Hello. We've merged #721 just recently and haven't had a release yet, as it needs some docs and examples to be updated. I'll work on it this weekend, so stay tuned.

@TojikCZ
Copy link
Author

TojikCZ commented Jan 31, 2025

Oh, awesome. Sorry for the dupe

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