python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Configuration provider passed as object instead of resolved value in provider injection

Open nichmorgan opened this issue 4 days ago • 3 comments

When passing a Configuration provider (e.g., config.github.token) as a parameter to another provider, dependency-injector passes the provider object itself instead of resolving it to the configured value. This causes the receiving class to get an object reference rather than the expected string value.

Minimal Reproducible Example

import logging
from dependency_injector import containers, providers
from github import Auth

class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    configure_logging = providers.Resource(
        logging.basicConfig,
        level=logging.INFO,
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        datefmt="%H:%M:%S",
    )

    # This passes the config provider object, not the resolved string value
    github_token = providers.Singleton(Auth.Token, token=config.github.token)

# Usage
container = Container()
container.config.github.token.from_value("ghp_mytoken123")
container.init_resources()

# Expected: Auth.Token receives "ghp_mytoken123"
# Actual: Auth.Token receives <object object at 0x...>

Evidence

The receiving class (Auth.Token) gets a provider object instead of the string:

Image

Workaround Observation

Interestingly, explicitly calling the configuration provider elsewhere appears to resolve it:

# Adding this line makes the injection work
# ... Previus Reproducible Example
assert isinstance(container.config.github.token(), str)  # ✅ Works

This suggests the provider isn't being automatically resolved during injection, but calling it explicitly triggers resolution that affects subsequent uses.

Expected Behavior

Configuration providers should be automatically resolved when injected as parameters to other providers. The dependency injection framework should handle this transparently.

Environment

  • dependency-injector version: 4.48.2
  • Python version: 3.12
  • OS: Windows

nichmorgan avatar Dec 08 '25 19:12 nichmorgan