Skip to content

Add injection contexts that share scope, cache, overrides and lifecycle between multiple injections#61

Open
kuyugama wants to merge 49 commits intomainfrom
feat/injection-context
Open

Add injection contexts that share scope, cache, overrides and lifecycle between multiple injections#61
kuyugama wants to merge 49 commits intomainfrom
feat/injection-context

Conversation

@kuyugama
Copy link
Copy Markdown
Member

No description provided.

@kuyugama kuyugama self-assigned this Nov 26, 2025
@kuyugama kuyugama added the enhancement New feature or request label Nov 26, 2025
@kuyugama
Copy link
Copy Markdown
Member Author

kuyugama commented Jan 4, 2026

Idea: Create dependency roles in injection context. They could be used to deal with lifespan dependencies that should be closed after each injection.

Possible roles:
stable - Lifespan dependency can be shared between multiple injections
unstable - Lifespan dependency should be closed immediately after injection and should not be added into cache.

It is likely, that it would require to rewrite entire injection system to support it, so, I'll just put it here for now.

This commit deprecates the use of dictionaries as scopes in favor of the new `fundi.Scope` object. This change makes dependency injection scopes more explicit, robust, and feature-rich.

The new `fundi.Scope` provides three distinct stores for dependency resolution:
- Named values: For resolving dependencies by parameter name.
- Typed instances: For resolving dependencies by type annotation.
- Type factories: For creating instances on-demand when resolved by type.

Key changes include:
- Introduction of the `fundi.Scope` and `fundi.Type` classes.
- Updates across all documentation and examples to adopt the new `Scope` object.
- A complete rewrite of the scope documentation (`docs/howtos/scope.rst`) to explain the new features.
- Exposure of `Scope` and `Type` in the top-level `fundi` namespace.
- Minor fixes to docstrings for better rendering.
This change refactors `configurable_dependency` to be more type-safe and explicit by introducing two new protocols: `DependencyConfiguratorProtocol` and `ConfiguredDependencyProtocol`.

`DependencyConfiguratorProtocol` types the decorator itself, indicating that it returns a `ConfiguredDependencyProtocol`. `ConfiguredDependencyProtocol` types the configured dependency, ensuring it has the `__fundi_configuration__` attribute.

The original configurator is now stored in the `origin` attribute of the wrapped function, allowing for better introspection.
In `fundi.scan.scan`, the returned `CallableInfo` object's `side_effects` attribute was being populated with the raw, unscanned functions that were passed in.

This has been corrected to use the list of internally processed `SideEffect` objects, ensuring downstream consumers receive the properly scanned side effects. The list is converted to a tuple to ensure immutability of the `CallableInfo`.

Additionally, minor code cleanup was performed, removing unused imports from a test file and a debug print statement.
The `Scope.add_factory` method has been significantly improved to be more flexible and easier to use.

Key enhancements include:
1.  **Pass-through Scan Parameters:** It's now possible to pass all parameters from the `scan()` function (e.g., `caching`, `async_`, `generator`) directly to `add_factory`. This allows for fine-grained control over how the factory is scanned and how its results are cached, without needing to call `scan()` manually.

2.  **Automatic Type Inference:** The `type_` parameter is now optional. If it's not provided, `add_factory` will automatically infer the dependency type from the factory's return annotation, reducing boilerplate code. A `ValueError` is raised if the type cannot be inferred.

The tests and documentation have been updated to reflect these new capabilities.
Fix documentation for Scope value resolving by type
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.

1 participant