Skip to content

v4#210

Merged
xelandernt merged 14 commits intomainfrom
v4
Apr 29, 2026
Merged

v4#210
xelandernt merged 14 commits intomainfrom
v4

Conversation

@xelandernt
Copy link
Copy Markdown
Collaborator

@xelandernt xelandernt commented Apr 20, 2026

Performance, typing, #192

@xelandernt xelandernt self-assigned this Apr 20, 2026
@xelandernt xelandernt added the enhancement New feature or request label Apr 20, 2026
@xelandernt xelandernt added this to the 4.0.0 milestone Apr 20, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
tests/experimental/test_container_2.py 100.00% <100.00%> (ø)
tests/providers/test_attr_getter.py 100.00% <100.00%> (ø)
tests/providers/test_base.py 100.00% <100.00%> (ø)
tests/providers/test_collections.py 100.00% <100.00%> (ø)
tests/providers/test_context_resources.py 100.00% <100.00%> (ø)
tests/providers/test_local_singleton.py 100.00% <100.00%> (ø)
tests/providers/test_resources.py 100.00% <100.00%> (ø)
tests/providers/test_singleton.py 100.00% <100.00%> (ø)
tests/test_container.py 100.00% <100.00%> (ø)
tests/test_injection.py 100.00% <100.00%> (ø)
... and 17 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR appears to be the “v4” performance/refactor update for the DI system, focusing on reducing per-resolution overhead (argument resolution, injection planning) and tightening context-scope initialization behavior.

Changes:

  • Add cached argument/keyword resolution helpers and reuse precomputed “is provider” flags across multiple providers (Factory/Singleton/Resource/etc.).
  • Refactor context-scope/context-resource initialization and injection context handling (including new sync injection context path).
  • Make collection providers return immutable/read-only results (tuple + mappingproxy) and update docs/tests/benchmarks accordingly.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
that_depends/providers/base.py Adds shared arg/kwarg resolution helpers and scope init-order caching/invalidation for providers.
that_depends/providers/mixin.py Introduces argument-registration state tracking to avoid repeated parent/child graph registration.
that_depends/providers/singleton.py Uses new resolution helpers + caches provider-ness of args/kwargs; adds early instance return.
that_depends/providers/local_singleton.py Same resolution/registration optimizations as Singleton, adapted to thread-local instance storage.
that_depends/providers/factories.py Uses new resolution helpers + caches provider-ness of args/kwargs; avoids repeated checks.
that_depends/providers/context_resources.py Refactors SupportsContext to a runtime-checkable Protocol; updates container_context provider discovery/caching; introduces sync injection context objects.
that_depends/providers/collection.py Changes List/Dict providers to return immutable/read-only Sequence/Mapping views.
that_depends/injection.py Adds cached injection “plan” and new sync injection stack; reworks scope context initialization setup.
that_depends/meta.py Adds type->provider lookup caching; removes context-management from metaclass.
that_depends/container.py Moves container context-management methods onto BaseContainer class (async/sync context entry).
that_depends/entities/resource_context.py Adds helper to reset context state and uses it during teardown.
that_depends/experimental/providers.py Updates LazyProvider context support handling (protocol typing + method forwarding) and adds regression coverage.
that_depends/integrations/faststream.py Updates typing for SupportsContext usage and middleware cloning.
tests/test_meta.py Adds coverage for explicit default_scope behavior on BaseContainerMeta.
tests/providers/test_context_resources.py Updates tests for new ResourceContext state helper and default-scope behavior.
tests/providers/test_collections.py Updates expectations for immutable Sequence/Mapping results and verifies immutability.
tests/experimental/test_container_2.py Adds coverage for LazyProvider behavior when context method access raises AttributeError.
docs/providers/context-resources.md Updates docs wording to “SupportsContext protocol”.
docs/providers/collections.md Updates docs/examples to reflect immutable/read-only collection results.
examples/benchmark/RESULTS.md Adds benchmark results for 4.0 vs prior versions.
pyproject.toml Updates dev deps (adds pyrefly).
Justfile Adds pyrefly and tweaks mypy invocation options for lint targets.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread that_depends/providers/context_resources.py
Comment thread that_depends/integrations/faststream.py
Comment thread that_depends/providers/base.py
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 32 out of 32 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

that_depends/injection.py:478

  • Scope filtering uses identity comparisons (is) between ContextScope objects. Because ContextScope.__eq__ is name-based (and inject() already checks scope == ContextScopes.ANY), using is here will skip context initialization when scope is an equivalent but different instance, leading to runtime failures when resolving ContextResources. Use == / != for provider_scope comparisons.
        provider_scope = provider._scope  # noqa: SLF001
        if provider_scope is ContextScopes.ANY or provider_scope is scope:
            if stack is None:
                msg = (
                    f"No stack exists, cannot initialize context for {provider} using scope {scope}.\n"
                    f"Note: @inject cannot initialize context for ContextResources when wrapping a generator."
                )
                raise ContextProviderError(msg)
            await stack.enter_async_context(provider.context_async(force=True))

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread that_depends/providers/context_resources.py Outdated
Comment thread that_depends/providers/context_resources.py Outdated
Comment thread that_depends/providers/context_resources.py Outdated
@xelandernt xelandernt merged commit cf7378e into main Apr 29, 2026
12 checks passed
@xelandernt xelandernt deleted the v4 branch April 29, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants