Skip to content

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

# Broken:
deps = Dependencies()

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