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

Inject dependency into decorators #59

@tc8

Description

@tc8

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions