Skip to content

4.0: closing a root container no longer resets overrides #580

Closed

Description

@lesnik512

This was generated by AI during a pre-4.0 audit of main at 9cd88e7.

Decision (maintainer): closing a root container no longer resets overrides.

Observation

close_sync/close_async on a root call overrides_registry.reset_override() (container.py:210,218). Overrides are configuration, closing is about cached-instance lifetime, and the coupling bites when an app's lifespan closes and reopens the root mid-test (arq burst cycles, test clients that run lifespan): the override disappears silently and the test hits the real service. Reopen restores clear_cache=False instances but not overrides, so a reopened container is configured differently from the one that was closed. OverrideHandle.__exit__ then restores a snapshot taken before the reset (documented in recipes/testing-overrides.md:14), so the two mechanisms contradict each other.

Proposal

  • Remove the reset from both close methods. Overrides live until reset_override() or the handle's __exit__.
  • Docs: providers/lifecycle.md:147-149, recipes/testing-overrides.md:14,16 (drop "a root close clears all overrides"; keep the teardown advice at :104).
  • Migration guide entry under 4.0 breaking changes: "Closing a root container no longer clears overrides; reset them with reset_override() or use with container.override(...)."

Downstream

chat-app, fastapi-sqlalchemy-template and litestar-sqlalchemy-template already reset overrides explicitly in their conftest teardown; modern-di-pytest's README uses the with handle. Re-check in the compat run (#579).

Acceptance

Failing test first: override, close the root, reopen, resolve returns the override; same for close_async. Handle exit after a close restores the prior override (no reset in between). Replaces item 4 of #567.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-for-agentFully specified, ready for an AFK agent

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions