Pylance complaining about missing parameters with injected classes
See original GitHub issue
When using
@inject
I have issues with pylance getting mad that there are missing parameters for the classes I inject in. Is there a fix for this?
Issue Analytics
- State:
- Created a year ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Issues · kodemore/kink - GitHub
Dependency injection container made for Python. ... Pylance complaining about missing parameters with injected classes.
Read more >Visual Studio Code Pylance (report Missing Imports )
As asked, your question doesn't specify whether or not your imported module is correctly installed. If it isn't, then this answer will not...
Read more >Python's “type hints” are a bit of a disappointment to me
I believe it was Typer (the CLI argument parsing library) where if you declare an ... Of course it's not a Python problem,...
Read more >Pylance: our new and improved Python experience in VS Code
Get a glimpse of the most performant and user-friendly editing experience for Python in VS Code ever. The newest version of the extension ......
Read more >How to solve Pylance 'missing imports' in vscode
Here is how you can solve this issue: Make sure you selected the right python interpreter for your... Tagged with python, vscode.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dkraczkowski sounds great! Writing a
mypy
plugin will be pretty easy with this, since the plugin will know which fields will be injected. The algorithm for the plugin will be as follows:kink.inject.inject
functionmypy.types.Callable
, inspect its argumentsInjectable[X]
as optional, effectively making mypy ignore them.Does this make sense? I’m open to any other suggestions!
@and3rson I think you are right and that’s the problem that the author has. But my point is you should not be doing this- you should not be instantiating class without arguments. Instead, you should request it from the DI.
Instead of doing this:
the Author should just do this:
There is no need to instantiate
TemperatureService
manually when you can simply request it from the di, and if mypy will complain that it does not know what the instance is simply tell it, like below:Your example after refactoring
How it works
When you use the
@inject
decorator, it injects dependencies and registers the class itself in the di container. This means every class annotated with@inject
can be requested from the DI container (kink.di
), without the need to define a factory for them. Please consider the following example:Hope this helps