Skip to content

Injection scope as a class#65

Merged
kuyugama merged 28 commits intomainfrom
kh/feat/scope-as-a-class
Mar 12, 2026
Merged

Injection scope as a class#65
kuyugama merged 28 commits intomainfrom
kh/feat/scope-as-a-class

Conversation

@kuyugama
Copy link
Copy Markdown
Member

No description provided.

@kuyugama kuyugama self-assigned this Jan 10, 2026
@kuyugama kuyugama added the enhancement New feature or request label Jan 10, 2026
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.
@kuyugama kuyugama merged commit dbece7f into main Mar 12, 2026
13 checks passed
@kuyugama kuyugama deleted the kh/feat/scope-as-a-class branch March 12, 2026 11:11
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