Lancetnik / FastDepends

FastDepends - FastAPI Dependency Injection system extracted from FastAPI and cleared of all HTTP logic. Async and sync modes are both supported.
https://lancetnik.github.io/FastDepends/
MIT License
240 stars 7 forks source link

Sample code has pyright type checking issues #106

Open skewty opened 2 weeks ago

skewty commented 2 weeks ago
from typing import Annotated

from fast_depends import Depends, Provider, inject

provider = Provider()

def abc_func() -> int:
    raise NotImplementedError

def real_func() -> int:
    return 1

@inject(dependency_overrides_provider=provider)  # pyright: ignore [reportCallIssue]
def func(dependency: Annotated[int, Depends(abc_func)]) -> int:
    return dependency

with provider.scope(abc_func, real_func):
    assert func() == 1  # pyright: ignore [reportCallIssue]

I added the pyright ignore comments to make pyright happy. Can the signatures be adjusted to alleviate this usability issue?

skewty commented 1 week ago

I get how

@inject(dependency_overrides_provider=provider)  # pyright: ignore [reportCallIssue]
def func(dependency: int = Depends(abc_func)) -> int:
    return dependency

fixes one of them. It is perhaps wise to ensure this is the recommended way in the documentation and why it is the recommended way (an explanation of the troubles one may encounter if using just Annotation).

That just leaves the type hinting of inject which seems to need some adjustment to appease pyright (haven't tried mypy against it yet).