-
Notifications
You must be signed in to change notification settings - Fork 1
feat(di): resolve dependency injection gaps #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gautam-7-7
wants to merge
2
commits into
nitrocloudofficial:main
Choose a base branch
from
Gautam-7-7:feature/di-gaps-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| nitrostack.log | ||
Binary file not shown.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,162 @@ | ||
| from typing import Any, Dict, List, Type, Union | ||
| import inspect | ||
| import threading | ||
| import typing | ||
| from typing import Any, Dict, List, Type, Union, Optional | ||
|
|
||
| def _get_class_dependencies(cls: Type) -> List[Any]: | ||
| """Helper to inspect constructor signature and retrieve type-annotated dependencies.""" | ||
| if not hasattr(cls, "__init__") or cls.__init__ is object.__init__: | ||
| return [] | ||
|
|
||
| # Get signature and type hints of the __init__ method | ||
| sig = inspect.signature(cls.__init__) | ||
| try: | ||
| # Resolve any forward references/strings in annotations relative to the class's module | ||
| type_hints = typing.get_type_hints(cls.__init__) | ||
| except Exception: | ||
| type_hints = {} | ||
|
|
||
| deps = [] | ||
| for param_name, param in sig.parameters.items(): | ||
| if param_name == "self": | ||
| continue | ||
| # Skip *args and **kwargs | ||
| if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD): | ||
| continue | ||
|
|
||
| # Use typing.get_type_hints if available, fallback to parameter.annotation | ||
| dep_type = type_hints.get(param_name, param.annotation) | ||
| if dep_type is inspect.Parameter.empty: | ||
| # If a dependency is missing annotations and has no default value, we cannot resolve it. | ||
| if param.default is inspect.Parameter.empty: | ||
| from nitrostack.core.errors import DependencyResolutionError | ||
| raise DependencyResolutionError( | ||
| f"Parameter '{param_name}' of class '{cls.__name__}' is missing type annotations and cannot be auto-resolved." | ||
| ) | ||
| else: | ||
| continue | ||
|
|
||
| # If the parameter has a default value, we only resolve it if it is registered in the DIContainer. | ||
| # Otherwise, we skip it and let Python use the default value. | ||
| if param.default is not inspect.Parameter.empty: | ||
| container = DIContainer.get_instance() | ||
| if dep_type not in container._registry and dep_type not in container._instances: | ||
| continue | ||
|
|
||
| deps.append(dep_type) | ||
|
|
||
| return deps | ||
|
|
||
| class DIContainer: | ||
| _instance = None | ||
| _instance_lock = threading.Lock() | ||
|
|
||
| @classmethod | ||
| def get_instance(cls) -> "DIContainer": | ||
| if cls._instance is None: | ||
| cls._instance = cls() | ||
| with cls._instance_lock: | ||
| if cls._instance is None: | ||
| cls._instance = cls() | ||
| return cls._instance | ||
|
|
||
| @classmethod | ||
| def reset(cls) -> None: | ||
| """Reset the singleton container (useful for testing).""" | ||
| cls._instance = None | ||
| with cls._instance_lock: | ||
| cls._instance = None | ||
|
|
||
| def __init__(self): | ||
| self._registry: Dict[Any, Type] = {} | ||
| self._instances: Dict[Any, Any] = {} | ||
| self._lock = threading.RLock() | ||
|
|
||
| def register(self, cls: Type) -> None: | ||
| def register(self, cls: Type, token: Optional[Any] = None) -> None: | ||
| """Register a provider class.""" | ||
| self._registry[cls] = cls | ||
| with self._lock: | ||
| reg_token = token if token is not None else cls | ||
| self._registry[reg_token] = cls | ||
| if token is not None: | ||
| self._registry[cls] = cls | ||
|
|
||
| def register_value(self, token: Any, value: Any) -> None: | ||
| """Register a constant value or instantiated service with a token.""" | ||
| self._instances[token] = value | ||
| # Also map token to its type if possible | ||
| if not isinstance(token, str): | ||
| self._registry[token] = type(value) | ||
| with self._lock: | ||
| self._instances[token] = value | ||
| # Also map token to its type if possible | ||
| if not isinstance(token, str): | ||
| self._registry[token] = type(value) | ||
|
|
||
| def resolve(self, token: Any) -> Any: | ||
| def resolve(self, token: Any, resolving: Optional[List[Any]] = None) -> Any: | ||
| """ | ||
| Resolve a dependency by token (class type or string key). | ||
| Instantiates classes if not already instantiated. | ||
| """ | ||
| # 1. Check if we already have a cached instance | ||
| if token in self._instances: | ||
| return self._instances[token] | ||
|
|
||
| # 2. Check if the token is registered as a class | ||
| cls = self._registry.get(token) | ||
|
|
||
| # 3. If not registered, but it's a class type, check if it's decorated with @injectable | ||
| if cls is None and isinstance(token, type): | ||
| cls = token | ||
| # We auto-register it to make usage easier | ||
| self.register(cls) | ||
|
|
||
| from nitrostack.core.errors import DependencyResolutionError | ||
|
|
||
| if cls is None: | ||
| raise DependencyResolutionError(f"Dependency '{token}' is not registered in the DIContainer.") | ||
|
|
||
| # 4. Resolve dependencies of the class | ||
| deps = getattr(cls, "_mcp_deps", []) | ||
| resolved_args = [] | ||
| for dep in deps: | ||
| resolved_args.append(self.resolve(dep)) | ||
|
|
||
| # 5. Instantiate the class | ||
| try: | ||
| instance = cls(*resolved_args) | ||
| except Exception as e: | ||
| raise DependencyResolutionError(f"Failed to instantiate class '{cls.__name__}' due to: {e}") from e | ||
|
|
||
| # 6. Cache and return the singleton instance | ||
| self._instances[token] = instance | ||
| return instance | ||
|
|
||
| def injectable(deps: List[Any] = None): | ||
| if resolving is None: | ||
| resolving = [] | ||
|
|
||
| with self._lock: | ||
| # 1. Check if we already have a cached instance | ||
| if token in self._instances: | ||
| return self._instances[token] | ||
|
|
||
| # 2. Circular dependency detection | ||
| if token in resolving: | ||
| from nitrostack.core.errors import CircularDependencyError | ||
| path = " -> ".join(str(t) for t in resolving + [token]) | ||
| raise CircularDependencyError(f"Circular dependency detected: {path}") | ||
|
|
||
| # 3. Check if the token is registered as a class | ||
| cls = self._registry.get(token) | ||
|
|
||
| # 4. If not registered, but it's a class type, check if it's decorated with @injectable | ||
| if cls is None and isinstance(token, type): | ||
| cls = token | ||
| # We auto-register it to make usage easier | ||
| self.register(cls) | ||
|
|
||
| from nitrostack.core.errors import DependencyResolutionError | ||
|
|
||
| if cls is None: | ||
| raise DependencyResolutionError(f"Dependency '{token}' is not registered in the DIContainer.") | ||
|
|
||
| # 5. Resolve dependencies of the class | ||
| deps = getattr(cls, "_mcp_deps", None) | ||
| if deps is None: | ||
| deps = _get_class_dependencies(cls) | ||
|
|
||
| resolved_args = [] | ||
| next_resolving = resolving + [token] | ||
| for dep in deps: | ||
| resolved_args.append(self.resolve(dep, next_resolving)) | ||
|
|
||
| # 6. Instantiate the class | ||
| try: | ||
| instance = cls(*resolved_args) | ||
| except Exception as e: | ||
| from nitrostack.core.errors import DIError | ||
| if isinstance(e, DIError): | ||
| raise | ||
| raise DependencyResolutionError(f"Failed to instantiate class '{cls.__name__}' due to: {e}") from e | ||
|
|
||
| # 7. Cache and return the singleton instance | ||
| self._instances[token] = instance | ||
| self._instances[cls] = instance | ||
|
|
||
| provide = getattr(cls, "_mcp_provide", None) | ||
| if provide is not None: | ||
| self._instances[provide] = instance | ||
|
|
||
| return instance | ||
|
|
||
| def injectable(deps: Optional[List[Any]] = None, provide: Optional[Any] = None): | ||
| """ | ||
| Decorator to mark a class as Injectable. | ||
| Requires explicit list of dependencies. | ||
| Allows custom token registration and automatic/explicit dependency resolution. | ||
| """ | ||
| if deps is None: | ||
| deps = [] | ||
| def decorator(cls: Type): | ||
| cls._mcp_deps = deps | ||
| cls._mcp_provide = provide | ||
| # Automatically register with the DIContainer | ||
| DIContainer.get_instance().register(cls) | ||
| DIContainer.get_instance().register(cls, token=provide) | ||
| return cls | ||
| return decorator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-6.45 KB
tests/__pycache__/test_widget_metadata.cpython-312-pytest-9.0.2.pyc
Binary file not shown.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this cover recursive pycache and .pyc files ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it does