question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pylance complaining about missing parameters with injected classes

See original GitHub issue

image 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:open
  • Created a year ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
and3rsoncommented, Oct 13, 2022

@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:

  • Register a hook for kink.inject.inject function
  • When the hook is called and the decorated object is mypy.types.Callable, inspect its arguments
  • Mark the arguments of type Injectable[X] as optional, effectively making mypy ignore them.

Does this make sense? I’m open to any other suggestions!

2reactions
dkraczkowskicommented, Oct 13, 2022

@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:

di[TemperatureService] = TemperatureService()

the Author should just do this:

instance = di[TemperatureService]

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:

instance: TemperatureService = di[TemperatureService]

Your example after refactoring

from kink import di, inject

class User:
    def __init__(self, name):
        self.name = name

@inject
class UserRepository:
    def save(self, user: User) -> None:
        pass

# ...

 @inject
class UserService:
    def  __init__(self, repository: UserRepository):
        self.repository = repository
   
    def create_user(self, name: str) -> User: 
        user = User(name=name)
        self.repository.save(user)
        return user

# pylint  no longer complains here

service: UserService = di[UserService]
service.create_user("Foo Bar")

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:

from kink import di, inject

@inject
class B:
    def __init__(self):
        ...

@inject
class A:
    def __init__(self, b: B):
        self.b = b

assert isinstance(di[A], A)
assert isinstance(di[B], B)

Hope this helps

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found