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

bug: Type casting with `as_` requires double method call with nested configuration using Factory

Open tmeckel opened this issue 2 years ago • 0 comments
trafficstars

The following code

self.deployments_service = providers.Factory(
    DeploymentService,
    base_url=self.config.deployments.base_url(),
    timeout=self.config.deployments.timeout.as_(tuple),
    transport=httpx.AsyncHTTPTransport(
        retries=self.config.deployments.retries.as_int()
    ),
)

requires a "double function call" on the as_int type cast method:

self.deployments_service = providers.Factory(
    DeploymentService,
    base_url=self.config.deployments.base_url(),
    timeout=self.config.deployments.timeout.as_(tuple),
    transport=httpx.AsyncHTTPTransport(
        retries=self.config.deployments.retries.as_int()()
    ),
)

otherwise the following exception is raised by the httpx package

 File "/.venv/lib/python3.10/site-packages/httpcore/_async/connection.py", line 126, in _connect
    if retries_left <= 0:
TypeError: '<=' not supported between instances of 'dependency_injector.providers.TypedConfigurationOption' and 'int'

Using a type cast as_ Method on a service parameter directly works like expected with a single method call as_int().

The config object used in the above mentioned code snippets is initialized by

self.config = providers.Configuration(strict=True)
self.config.from_yaml("./config.yml")
self.config.from_yaml("./config.local.yml", required=False)

tmeckel avatar Mar 31 '23 15:03 tmeckel