Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement DI tools for gokart #400

Closed
wants to merge 5 commits into from
Closed

feat: implement DI tools for gokart #400

wants to merge 5 commits into from

Conversation

hiro-o918
Copy link
Contributor

Background

Tasks sometimes uses another class like API clients in the run.
gokart.TaskOnKart requires to create an instance in its task, because the parameters can only be injected.

Currently, we must write the following codes to do so.

Overwrite constructor

class Foo(gokart.TaskOnKart):
    name = luigi.Parameter()

    def __init__(*args, **kwargs):
        self._client = Client(...)
        super(Foo, self).__init__(*args, **kwargs)

    def run(self):
        self._client.foo()

Create Instance in run

class Foo(gokart.TaskOnKart):
    name = luigi.Parameter()

    def run(self):
        client = Client(name=self.foo)
        client.foo()

It seems to be noisy for me.

This PR aims...

Introduce a DI system inspired by FastAPI Depends.
The following is an example of this feature.

def build_client(name: str) -> Client # name is resolved by task parameter
    return Client(name=name)


class Foo(gokart.TaskOnKart):
    name = luigi.Parameter()

    def run(self, client: Client= gokart.Depends(build_client)):
        client.foo()

Please also check the test codes which shows the example of resolving dependencies in recursive manner.
Let me know your frank opinion for this feature.

@hiro-o918 hiro-o918 marked this pull request as draft October 8, 2024 10:47
@hiro-o918 hiro-o918 force-pushed the feature/di branch 2 times, most recently from 5f411d3 to 88fce07 Compare October 8, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant