ivankorobkov / python-inject

Python dependency injection
Apache License 2.0
701 stars 79 forks source link

mypy and type inference with @inject decorators #80

Open li-dennis opened 3 years ago

li-dennis commented 3 years ago

Hi,

We've been making use of your library quite a lot and it's been a joy to use! The only challenge I've really run into is handling pyright/mypy errors with the @inject decorator. The decorator of course modifies the type signature of the function/classes, but pyright, mypy, pylint etc don't really have knowledge of this so they throw warnings that certain parameters are missing.

Are there any workarounds to this? I've been trying to figure something out but so far it seems like the likely answer is "no". This is the closest https://github.com/microsoft/pyright/issues/774

Other than that the only ideas I've come up is converting the injected parameters from required to optional. But that's a less than ideal solution since often time these are classes which can be expensive to instantiate, and performance matters for my application.

Best

ivankorobkov commented 3 years ago

Hi Dennis,

Could you give some examples, please?

remko commented 2 years ago

@ivankorobkov

@inject.autoparams()
def refresh_cache(cache: RedisCache):
    pass

....
refresh_cache()

gives the error

 Argument missing for parameter "cache"
fibigerg commented 2 years ago

Hi, this is definitely a problem for projects that use mypy and type linters. With these linters getting more popular, is there any suggestion how to make this library compatible? Wrapping in Optional[] is not really a great solution, because it forces checks for non None when the injected value is used.

ivankorobkov commented 2 years ago

Hi @fibigerg

Thank for you comment.

I have very little knowledge about mypy or type linters. If some can find a solution and will make a PR, I will gladly merge it and release the next version.

x0y-gt commented 7 months ago

Any updates with this?