GroupInstantiationError¶
Symptom¶
modern_di.exceptions.lifecycle.GroupInstantiationError: Dependencies cannot be instantiated
See: https://modern-di.modern-python.org/troubleshooting/group-instantiation-error/
.group_name holds the name of the Group subclass.
Cause¶
A Group subclass was called like a constructor, Dependencies(). A group is a namespace for
declaring providers as class attributes, and calling it always raises, with or without arguments.
You pass the class itself to Container(groups=[Dependencies]) and read its providers as
Dependencies.some_provider.
The habit often comes from dependency-injector and that-depends, where the container class is
also the runtime object you resolve from. The migration guides cover the difference:
from dependency-injector and
from that-depends.
Fix¶
Pass the class to a Container and resolve from the container:
# Works:
container = Container(groups=[Dependencies])
service = container.resolve_provider(Dependencies.service)
assert isinstance(service, Service)
See also¶
- Organize a large container with multiple Groups: splitting providers across several
Groupclasses. - Migration from
dependency-injector:Groupas schema,Containeras runtime. - Migration from
that-depends: the same split, coming fromBaseContainer.