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

Support for arbitrary callables #53

Open
MrGreenTea opened this issue Apr 23, 2020 · 1 comment
Open

Support for arbitrary callables #53

MrGreenTea opened this issue Apr 23, 2020 · 1 comment

Comments

@MrGreenTea
Copy link

MrGreenTea commented Apr 23, 2020

What is the reasoning for only allowing classes for injection and not allowing all callables?

Most of my providers/factories are simple callables / functions. Adding an unnecessary self parameter does not feel right to me.

An example of what I am trying to do:

def factory_1(some_value): return some_value + 2
def some_value(a: int): return a * 2

data = {"a": 1}

class MySpec(pinject.BindingSpec):
    def configure(self, bind):
        for k, v in data.items(): bind(k, to_instance=v)

graph = pinject.new_object_graph(binding_specs=[MySpec()])

print(graph.provide(factory_1))

I would expect the output to be 4

Some things I tried:

setattr(Spec, "provide_some_value", pinject.provide("some_value")(some_value)

For this to work I need to change some_value to

def some_value(self, a: int): ...

But when I want users to provide their custom providers I don't' want to force them to add this unneeded self.

I hope I could make it understood what I want to achieve. An inspiration are pytest fixtures.

@cal-pratt
Copy link

cal-pratt commented Aug 26, 2021

I was going to leave a comment here, but I realized I slightly misunderstood the issue at hand.

In the new issue I posted, I have a workaround to call a top-level-function and inject the args, but it would not support injecting params based on other function names. i.e. resolving the argument some_value to the function some_value.

See here if you're interested #62

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

2 participants