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

Add bind_by_generic #119

Open
woile opened this issue Oct 23, 2024 · 0 comments
Open

Add bind_by_generic #119

woile opened this issue Oct 23, 2024 · 0 comments

Comments

@woile
Copy link
Contributor

woile commented Oct 23, 2024

hey I was wondering if we can add a bind_by_generic function.

For example, it would be nice to be able to bind something like this:

import typing

KT = typing.TypeVar("KT")
VT = typing.TypeVar("VT")

class Record(typing.Generic[KT, VT]):
    def __init__(self, key: KT, value: VT):
        self.key = key
        self.value = value

def func_hinted(record: Record[str, int]) -> Record[str, int]:
    return record

def func_base(record: Record) -> Record:
    return record

I came up with this function, and it works for my case:

import inspect
from typing import Any, get_origin

from di._container import BindHook
from di._utils.inspect import get_type
from di.api.dependencies import DependentBase


def bind_by_generic(
    provider: DependentBase[Any],
    dependency: type,
) -> BindHook:
    """Hook to substitute the matched dependency based on its generic."""

    def hook(
        param: inspect.Parameter | None, dependent: DependentBase[Any]
    ) -> DependentBase[Any] | None:
        if dependent.call == dependency:
            return provider
        if param is None:
            return None

        type_annotation_option = get_type(param)
        if type_annotation_option is None:
            return None
        type_annotation = type_annotation_option.value
        if get_origin(type_annotation) is dependency:
            return provider
        return None

    return hook

I was wondering if it makes sense, or should it be part of bind_by_type, thoughts? I'm happy to send a PR if welcomed.

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

No branches or pull requests

1 participant