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

mypy and type inference with @inject decorators #80

Open
li-dennis opened this issue Aug 11, 2021 · 5 comments
Open

mypy and type inference with @inject decorators #80

li-dennis opened this issue Aug 11, 2021 · 5 comments

Comments

@li-dennis
Copy link

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 microsoft/pyright#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
Copy link
Owner

Hi Dennis,

Could you give some examples, please?

@remko
Copy link

remko commented Mar 12, 2022

@ivankorobkov

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

....
refresh_cache()

gives the error

 Argument missing for parameter "cache"

@fibigerg
Copy link

fibigerg commented Sep 7, 2022

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
Copy link
Owner

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
Copy link

x0y-gt commented Mar 27, 2024

Any updates with this?

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

5 participants