-
-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ general improvements #40
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
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
@codex review |
📝 WalkthroughWalkthroughAdds a configurable Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Pull request overview
This PR introduces general improvements focused on performance, safety, and correctness. The changes replace deep hashing with identity-based hashing in WeakWrapper, optimize copying strategies in encoding and merging, and add recursion depth guards to prevent stack overflows.
Changes:
- Switched
WeakWrapperfrom deep content hashing to identity-based hashing for stability with mutable objects - Optimized encoding by replacing deep copies with shallow copies (except when callable filters are used)
- Added configurable recursion depth guards in encoding to prevent stack overflows
- Updated
Utils.mergeto avoid unnecessary deep copies and handle non-dict mappings
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/qs_codec/models/weak_wrapper.py | Replaced deep content hashing with identity-based hashing using proxy identity |
| src/qs_codec/encode.py | Added recursion depth guard and switched from deep to shallow copying for mappings |
| src/qs_codec/utils/utils.py | Optimized merge to avoid deep copies and handle non-dict mapping targets |
| tests/unit/weakref_test.py | Updated tests to verify identity-based hashing and removed deep hash tests |
| tests/unit/utils_test.py | Added test for merging with MappingProxyType targets |
| tests/unit/encode_test.py | Added test for encoding depth guard functionality |
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #40 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 1267 1247 -20
=========================================
- Hits 1267 1247 -20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/qs_codec/utils/utils.py (1)
220-240:⚠️ Potential issue | 🟠 MajorIn-place mutation of
targetwhen it's adictmay violate caller-input immutability.When
targetis already adict, line 223 assignsmerge_target = target(same reference), and line 239 mutates it in place viaupdate(). This means callers passing adictdirectly toUtils.mergewill have their original mutated.While the internal encoder protects inputs with a shallow copy before recursion,
Utils.mergeis a public utility method. Direct callers could experience unexpected mutation.Consider always creating a shallow copy for dict targets:
🛡️ Proposed fix to avoid mutating caller input
# Prepare a mutable target we can merge into (avoid deepcopy for performance). merge_target: t.Dict[str, t.Any] - if isinstance(target, dict): - merge_target = target - else: - merge_target = dict(target) + merge_target = dict(target) # shallow copy preserves caller immutabilityBased on learnings: "DO NOT mutate caller inputs—copy/normalize (deepcopy for mappings, index-projection for sequences) before traversal".
🧹 Nitpick comments (1)
src/qs_codec/encode.py (1)
233-234: Consider extracting the error message to a constant (optional).The static analysis tool flags TRY003 for the inline message. While this is a minor style issue, extracting to a module-level constant could improve consistency:
💅 Optional: Extract error message constant
+_ERR_MAX_DEPTH = "Maximum encoding depth exceeded" + # ... if _depth > _get_max_encode_depth(): - raise ValueError("Maximum encoding depth exceeded") + raise ValueError(_ERR_MAX_DEPTH)
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.
Pull request overview
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
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.
Pull request overview
Copilot reviewed 13 out of 14 changed files in this pull request and generated 1 comment.
…ty in WeakWrapper
This pull request introduces a new configurable maximum encoding depth for the encoder, improves documentation and project guidelines, and makes some technical refinements to internal utilities. The most significant change is the addition of the
max_depthoption, which allows users to limit how deeply the encoder will traverse nested data structures, with robust handling to avoid exceeding Python’s recursion limit. Documentation and internal instructions are updated to reflect this, and some internal utilities are simplified for performance and clarity.Encode depth limit and safety improvements:
max_depthoption toEncodeOptions, allowing users to cap how deeply the encoder traverses nested objects. The effective limit is always capped to Python’s current recursion limit minus a safety margin to avoidRecursionError(src/qs_codec/models/encode_options.py,src/qs_codec/encode.py). [1] [2] [3] [4] [5]src/qs_codec/encode.py).ValueError("Maximum encoding depth exceeded")if the depth is exceeded, and validates thatmax_depthis a positive integer orNone(src/qs_codec/models/encode_options.py).Documentation and project guidance:
README.rst,docs/README.rst,docs/index.rst) to describe the newmax_depthoption, its default behavior, and usage examples. [1] [2] [3] [4]AGENTS.mdfile and expanded.github/copilot-instructions.mdto clarify project structure, coding conventions, testing strategy, and the new depth limit behavior. [1] [2] [3]Internal utility and technical changes:
WeakWrapperto use stable identity-based hashing rather than deep content hashing, improving performance and avoiding recursion issues (src/qs_codec/models/weak_wrapper.py). [1] [2] [3]src/qs_codec/utils/utils.py,src/qs_codec/encode.py). [1] [2]These changes together improve the safety, performance, and clarity of the encoder, while ensuring that both users and contributors have clear guidance on usage and extension.
Summary by CodeRabbit
New Features
Refactor
Documentation
Tests
Chores
✏️ Tip: You can customize this high-level summary in your review settings.