Skip to content

container.set_provider doesn't work with str subclasses #479

@ilsurih

Description

@ilsurih

DynamicContainer's set_provider does not accept str sub-class instances as a parameter. The following code is to demonstrate the issue:

from enum import Enum

from dependency_injector import containers, providers


class MyEnum(str, Enum):
    val = "thing"


class MyClass:
    thing: str = None


obj1 = MyClass()
obj2 = containers.DynamicContainer()

setattr(obj1, MyEnum.val, providers.Object("a value"))
assert obj1.thing is not None                                                         # works!

setattr(obj2, MyEnum.val, providers.Object("a value"))              # fails!
# same as obj2.set_provider(MyEnum.val, providers.Object("a value"))
assert obj2.thing is not None

fails with TypeError:

Traceback (most recent call last):
  File ".../scratches/scratch.py", line 10, in <module>
    container.set_provider(MyStr(), providers.Object("lol"))
  File "src/dependency_injector/containers.pyx", line 190, in dependency_injector.containers.DynamicContainer.set_provider
TypeError: Argument 'name' has incorrect type (expected str, got MyStr)

For another class MyClass we can use MyEnum.val to set attribute.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions