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.

Support for arbitrary callables #53

@MrGreenTea

Description

@MrGreenTea

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions