Refine matcher runtime deps in trigger_event and simplify _simple_run#63
Merged
JohnRichard4096 merged 1 commit intomainfrom May 3, 2026
Merged
Refine matcher runtime deps in trigger_event and simplify _simple_run#63JohnRichard4096 merged 1 commit intomainfrom
JohnRichard4096 merged 1 commit intomainfrom
Conversation
Contributor
Reviewer's GuideRefactors matcher execution to move runtime dependency resolution from _simple_run into trigger_event, simplifies _simple_run’s API and behavior, and updates tests and versioning to reflect the new dependency injection model and performance focus. Updated class diagram for matcher dependency injection modelclassDiagram
class MatcherFactory {
+trigger_event(event, *args, **kwargs, config)
+_simple_run(matcher_list, exception_ignored, extra_args, extra_kwargs) bool
+_do_runtime_resolve(runtime_args, runtime_kwargs, args2update, kwargs2update, session_args, session_kwargs, exception_ignored) bool
+_resolve_dependencies(signature, session_args, session_kwargs) (bool, list, dict, dict)
}
class FunctionData {
+function
+priority
}
class DependsFactory {
+call()
}
class BaseEvent {
}
class AmritaConfig {
}
MatcherFactory --> FunctionData : uses matcher_list
MatcherFactory --> DependsFactory : resolves_dependencies
MatcherFactory --> BaseEvent : consumes_event
MatcherFactory --> AmritaConfig : optional_config
FunctionData o--> HandlerFunction
class HandlerFunction {
+__call__(...)
}
Flow diagram for argument preparation and runtime DI resolution in trigger_eventflowchart TD
A[trigger_event called
with event, args, kwargs, config] --> B["Build s_args<br/>[event, args, config_if_present]"]
B --> C[Deep copy kwargs
to session_kwargs]
C --> D[Scan s_args
for DependsFactory
instances]
C --> E[Scan session_kwargs
for DependsFactory
instances]
D --> F{runtime_args or
runtime_kwargs exist?}
E --> F
F -- Yes --> G[Call _do_runtime_resolve
to fill s_args and
session_kwargs]
G --> H{resolved?}
H -- No --> I[Raise RuntimeError
Runtime arguments
cannot be resolved]
H -- Yes --> J[Proceed with
resolved s_args and
session_kwargs]
F -- No --> J
J --> K[Loop priorities]
K --> L[Call _simple_run with
matcher_list,
extra_args=s_args,
extra_kwargs=session_kwargs]
L --> M[Inside _simple_run:
reject DependsFactory in
extra_args or extra_kwargs]
M --> N[Resolve handler-level
dependencies and run
handlers]
N --> O[Return from
trigger_event]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Member
Author
|
@sourcery-ai title |
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
_simple_run,extra_argsis typed asIterable[Any]but is iterated multiple times and then combined into a list; consider tightening this toSequence[Any](or converting once at the top) to avoid surprises with one-shot iterables. - The runtime DI resolution logic is now duplicated between
trigger_eventand the previous_simple_runimplementation; consider extracting the common resolution into a shared helper to keep behavior consistent and reduce maintenance overhead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `_simple_run`, `extra_args` is typed as `Iterable[Any]` but is iterated multiple times and then combined into a list; consider tightening this to `Sequence[Any]` (or converting once at the top) to avoid surprises with one-shot iterables.
- The runtime DI resolution logic is now duplicated between `trigger_event` and the previous `_simple_run` implementation; consider extracting the common resolution into a shared helper to keep behavior consistent and reduce maintenance overhead.
## Individual Comments
### Comment 1
<location path="src/amrita_core/hook/matcher.py" line_range="369-378" />
<code_context>
/,
exception_ignored: tuple[type[BaseException], ...],
- extra_args: tuple,
+ extra_args: Iterable[Any],
extra_kwargs: dict[str, Any],
- config: AmritaConfig | None = None,
</code_context>
<issue_to_address>
**issue (bug_risk):** Using a generic Iterable for extra_args risks consuming single-use iterables before they are expanded.
Because extra_args is now Iterable[Any], it’s iterated once in the any(...) check and then expanded with *extra_args for session_args. For single-use iterables (e.g. generators), the first pass will exhaust them and session_args will miss those values.
To prevent this while keeping the general type, normalize extra_args at the start of _simple_run, for example:
```python
def _simple_run(..., extra_args: Iterable[Any], ...):
extra_args = tuple(extra_args)
...
```
Alternatively, change the type back to a multi-pass container like Sequence/tuple to make this usage explicit.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary by Sourcery
Refine matcher execution to centralize runtime dependency resolution in event triggering and simplify _simple_run arguments, while bumping the package version.
Enhancements:
Build:
Tests: