Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Inject dependency into decorators #59

Open
tc8 opened this issue Feb 6, 2021 · 0 comments
Open

Inject dependency into decorators #59

tc8 opened this issue Feb 6, 2021 · 0 comments

Comments

@tc8
Copy link

tc8 commented Feb 6, 2021

Hello,

How can I use Pinject to inject dependencies into decorators?

For example, say I have a decorator like:

# foo.py
def cache_decorator(cache_client):
    def decorator(func):
        def wrapper(*args, **kwargs):
            cache_client.read()
            result = func(*args, **kwargs)
            cache_client.write
            return result
        return wrapper
    return decorator

class A:
  @cache_decorator(dependency_I_want_to_inject)
  def foo(bar):
    bar.baz()

The goal is to swap the cache_client with a mock for unit testing.

Is something like this possible with Pinject?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant