-
Notifications
You must be signed in to change notification settings - Fork 219
refactor: separate metric definition from metric value and share comp… #154
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
base: main
Are you sure you want to change the base?
Conversation
…arator instances - Add _MetricDefinition class to separate metric type (definition) from metric value (measurement) - Implement comparator caching via _get_shared_comparator() to ensure only one comparator object exists per unique (method, target, epsilon) combination - All solutions using the same comparison method now share the same comparator instance, reducing memory usage - Maintain full backward compatibility - no changes to Metric class API - Remove TODO comment as the refactoring addresses the concern about mixing metric and metric value concerns
Greptile OverviewGreptile SummaryThis PR refactors the
These issues need to be resolved before merging, as they could lead to incorrect metric comparisons in production. Confidence Score: 2/5
Important Files ChangedFile Analysis
|
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.
Additional Comments (1)
-
plexe/internal/models/entities/metric.py, line 247 (link)syntax: redundant condition:
(self.is_worst and other.is_worst)is unnecessary sinceself.is_worstalready covers this case
1 file reviewed, 3 comments
| def __eq__(self, other) -> bool: | ||
| """Check if two metric definitions are equal.""" | ||
| if not isinstance(other, _MetricDefinition): | ||
| return False | ||
| return ( | ||
| self.name == other.name | ||
| and self.comparator.comparison_method == other.comparator.comparison_method | ||
| and self.comparator.target == other.comparator.target | ||
| ) |
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.
logic: epsilon is missing from equality check - two metric definitions with different epsilon values will be considered equal, causing incorrect behavior when comparing metrics with different epsilon tolerances
| def __eq__(self, other) -> bool: | |
| """Check if two metric definitions are equal.""" | |
| if not isinstance(other, _MetricDefinition): | |
| return False | |
| return ( | |
| self.name == other.name | |
| and self.comparator.comparison_method == other.comparator.comparison_method | |
| and self.comparator.target == other.comparator.target | |
| ) | |
| def __eq__(self, other) -> bool: | |
| """Check if two metric definitions are equal.""" | |
| if not isinstance(other, _MetricDefinition): | |
| return False | |
| return ( | |
| self.name == other.name | |
| and self.comparator.comparison_method == other.comparator.comparison_method | |
| and self.comparator.target == other.comparator.target | |
| and self.comparator.epsilon == other.comparator.epsilon | |
| ) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: plexe/internal/models/entities/metric.py
Line: 163:171
Comment:
**logic:** `epsilon` is missing from equality check - two metric definitions with different epsilon values will be considered equal, causing incorrect behavior when comparing metrics with different epsilon tolerances
```suggestion
def __eq__(self, other) -> bool:
"""Check if two metric definitions are equal."""
if not isinstance(other, _MetricDefinition):
return False
return (
self.name == other.name
and self.comparator.comparison_method == other.comparator.comparison_method
and self.comparator.target == other.comparator.target
and self.comparator.epsilon == other.comparator.epsilon
)
```
How can I resolve this? If you propose a fix, please make it concise.| def __hash__(self) -> int: | ||
| """Hash the metric definition.""" | ||
| return hash((self.name, self.comparator.comparison_method, self.comparator.target)) |
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.
logic: epsilon is missing from hash calculation - must include all fields used in __eq__ to maintain hash contract
| def __hash__(self) -> int: | |
| """Hash the metric definition.""" | |
| return hash((self.name, self.comparator.comparison_method, self.comparator.target)) | |
| def __hash__(self) -> int: | |
| """Hash the metric definition.""" | |
| return hash((self.name, self.comparator.comparison_method, self.comparator.target, self.comparator.epsilon)) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: plexe/internal/models/entities/metric.py
Line: 173:175
Comment:
**logic:** `epsilon` is missing from hash calculation - must include all fields used in `__eq__` to maintain hash contract
```suggestion
def __hash__(self) -> int:
"""Hash the metric definition."""
return hash((self.name, self.comparator.comparison_method, self.comparator.target, self.comparator.epsilon))
```
How can I resolve this? If you propose a fix, please make it concise.- Add epsilon to __eq__ method to ensure metrics with different epsilon values are correctly differentiated - Add epsilon to __hash__ method to maintain hash contract (must include all fields used in __eq__) - Fix redundant condition in Metric.__gt__ method This fixes critical bugs identified in code review that could cause incorrect metric comparisons.
- Reformat code to comply with black formatting standards - Fixes CI formatting check failure
This PR addresses the TODO comment in metric.py by refactoring the Metric class to separate concerns and optimize memory usage.
Changes
-Memory optimization: All solutions using the same comparison method now share the same comparator instance, reducing memory usage
Resolves the TODO comment about mixing metric and metric value concerns
Reduces memory usage by sharing comparator instances across all solutions
Cleaner separation of concerns (definition vs value)
No breaking changes - all existing code continues to work