Skip to content

Add support for resolving function parameters#6

Open
glena wants to merge 1 commit into
mainfrom
german/resolve-function-params
Open

Add support for resolving function parameters#6
glena wants to merge 1 commit into
mainfrom
german/resolve-function-params

Conversation

@glena

@glena glena commented Jan 15, 2026

Copy link
Copy Markdown

Resolve function dependencies, returning a wrapper that auto-injects.

The returned function has injectable parameters automatically resolved from
the DI container. Only non-injectable parameters (primitives, untyped, etc.)
remain as required arguments.

The return type is Callable[..., Any] because Python's type system cannot
express "function with some parameters removed." However, the runtime
signature (via __signature__) is correctly modified, so IDEs will show
accurate autocomplete for the wrapped function.

For full static type safety, you can manually annotate the resolved function:

    def greet(screen: Screen, name: str, count: int = 1) -> str:
        return f"{screen.display(name)} x{count}"

    # Manual annotation provides full type checking
    wrapped: Callable[[str], str] = injector.resolve(greet)  # type: ignore
    wrapped("Alice")  # ✅ Type checker validates this
    wrapped(123)      # ❌ Type error: Expected str, got int

Example:

    class Screen:
        def say(self, text: str):
            print(text)

    def salute(medium: Screen, content: str):
        medium.say(content)

    configuration = Configuration()
    configuration.bind(Screen).globally().to_instance(Screen())

    injector = Injector(configuration)
    new_salute = injector.resolve(salute)

    # Only 'content' is required - 'medium' is auto-injected
    new_salute("hello")

    # Check runtime signature to see actual parameters
    import inspect
    print(inspect.signature(new_salute))  # (content: str) -> None

Args:

  • fn: The function to wrap with dependency injection

Returns:

  • A wrapper function with injectable parameters resolved automatically.
    For static type safety, manually annotate with the expected signature.

@glena glena marked this pull request as ready for review January 20, 2026 11:55
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