Skip to content

0.79.0

Choose a tag to compare

@antonrh antonrh released this 28 Jul 10:46
· 53 commits to main since this release

Global container

Sometimes the container is out of reach. A module the container imports cannot import it back, so container.ref() is not available there.

global_ref() references a dependency without a container:

# app/mail.py
from anydi import global_ref

mailer = global_ref(Mailer)


def send_welcome(email: str) -> None:
    mailer.send(email, "Welcome!")

Create the container with create_global_container():

# app/container.py
container = create_global_container()


@container.provider(scope="singleton")
def mailer() -> Mailer:
    return SmtpMailer()

The reference binds on first access, so import order does not matter. Overrides work as usual.

One container per process. create_global_container() raises if one is already set. Use reset_global_container() to replace it.

In tests the container fixture becomes the global container, so references resolve against it.

Use container.ref() where the container is reachable.

See Global Container and the example application.

What's Changed

  • Add global container and global references by @antonrh in #309

Full Changelog: 0.78.0...0.79.0