Skip to content

fix: adopt modern-di 3.1 lifecycle and explicit validation - #8

Merged
lesnik512 merged 1 commit into
mainfrom
fix/modern-di-3.1-lifecycle-and-validate
Jul 28, 2026
Merged

fix: adopt modern-di 3.1 lifecycle and explicit validation#8
lesnik512 merged 1 commit into
mainfrom
fix/modern-di-3.1-lifecycle-and-validate

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Two changes, both caused by modern-di 3.1. This package pins modern-di>=3,<4, so users and CI pick 3.1 up on a routine upgrade.

1. The suite is already red on 3.1 (fixed here)

test_open_before_setup_di_fails_validation_for_hard_request_dependency expects root.open() to raise ValidationFailedError. In 3.1 open() validates nothing — validation is an explicit container.validate() call — so it does not raise. This failure predates this PR: it reproduces on main with 3.1.1 installed.

The ordering rule the test guards still exists; it just binds a different call now. Both siblings are rewritten to validate(), keeping their subject: validating before setup_di fails, because flask_request_provider does not exist yet; validating after it succeeds.

2. The documented example uses a deprecated no-op, and the prose is wrong

3.1 made Container(validate=...) a deprecated no-op emitting ValidateArgumentWarning. Worse, the README and architecture/dependency-injection.md both explain that .open() is "required under modern-di 3.x's mandatory-open lifecycle, since validate=True runs at open() time". Neither half is true in 3.1: a container is open from construction, and open() runs no validation.

README, examples/app.py and architecture/dependency-injection.md now build the container plain, drop the unnecessary .open(), and call container.validate() after setup_di. The genuinely caller-owned half — close_sync() at shutdown, since Flask has no shutdown hook — is unchanged and still documented.

The adapter's own child.open() in _enter_request is left as-is; it is a no-op on 3.1 rather than a bug, and removing it is a runtime change that belongs in its own PR. The architecture doc now says so instead of calling it required.

Historical planning/ files are untouched. Suite green (12 passed). The docs half matches the pattern reviewed in modern-di-fastapi#37.

🤖 Generated with Claude Code

The suite was already red against 3.1.1 on main: a test expected root.open() to
raise ValidationFailedError, but 3.1 validates nowhere implicitly. The ordering
rule it guards still exists and now binds validate(), so both siblings are
rewritten to that call - validating before setup_di fails because
flask_request_provider does not exist yet; after it, succeeds.

The README and architecture doc also explained that .open() is required and that
validate=True runs at open() time. Neither is true in 3.1: containers are open
from construction and open() runs no validation. Docs, example and architecture
now build the container plain, drop the unnecessary open(), and validate after
setup_di. The caller-owned close_sync() at shutdown is unchanged.
@lesnik512
lesnik512 merged commit 1a478cc into main Jul 28, 2026
6 checks passed
@lesnik512
lesnik512 deleted the fix/modern-di-3.1-lifecycle-and-validate branch July 28, 2026 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant