Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThe changes implement field-level caching in Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
tests/test_baker.py (1)
1550-1575: Isolate_fields_cachestate per test.These tests mutate
baker.Baker._fields_cachedirectly and only partially clean it. Use a fixture to snapshot/restore the full cache to avoid order-dependent behavior.🧪 Suggested test isolation pattern
class TestGetFieldsCaching: """Tests for Baker.get_fields() caching and id()-based filtering.""" + + `@pytest.fixture`(autouse=True) + def restore_fields_cache(self): + original = baker.Baker._fields_cache.copy() + try: + yield + finally: + baker.Baker._fields_cache = original🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/test_baker.py` around lines 1550 - 1575, The tests mutate baker.Baker._fields_cache and only partially clean it, causing order-dependent failures; fix by isolating _fields_cache for these tests: snapshot the original baker.Baker._fields_cache at test start (or use a fixture) and restore it after each test (or set baker.Baker._fields_cache = {} before calling Baker), updating the tests like test_get_fields_result_is_cached, test_get_fields_different_models_cached_separately, and test_get_fields_includes_all_non_reverse_fields to perform the snapshot/restore (or use a pytest fixture that yields and restores baker.Baker._fields_cache) so each test runs with a known, clean cache state.model_bakery/baker.py (1)
351-352: Use an immutable cache value to protect shared field cache integrity.
get_fields()currently returns a shared mutableset. A single accidental mutation can affect all future calls for that model. Prefer caching afrozenset.🛡️ Proposed change
- _fields_cache: dict[type[Model], set[Any]] = {} + _fields_cache: dict[type[Model], frozenset[Any]] = {} - def get_fields(self) -> set[Any]: + def get_fields(self) -> frozenset[Any]: model = self.model if model not in self._fields_cache: related_ids = {id(f) for f in model._meta.related_objects} - self._fields_cache[model] = { + self._fields_cache[model] = frozenset( f for f in model._meta.get_fields() if id(f) not in related_ids - } + ) return self._fields_cache[model]Also applies to: 438-445
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@model_bakery/baker.py` around lines 351 - 352, The shared _fields_cache currently stores and returns mutable set objects; change the cache to store immutable frozenset values and update get_fields (and any other places that currently insert or return sets, e.g., the logic referenced around get_fields at lines ~438-445) so it converts computed field sets to frozenset before caching and always returns a frozenset to callers; make sure any code that previously mutated the returned set either copies to a new set before mutation or is updated to work with the immutable frozenset, referencing _fields_cache and get_fields to locate the changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@CHANGELOG.md`:
- Line 13: The changelog contains a typo where the function call is written as
get_fields()() in the sentence about caching results from Baker.get_fields();
update the text to use a single call, Baker.get_fields(), so the line reads
"Cache Baker.get_fields() results per model..." (ensure the symbol
Baker.get_fields() is used exactly once).
---
Nitpick comments:
In `@model_bakery/baker.py`:
- Around line 351-352: The shared _fields_cache currently stores and returns
mutable set objects; change the cache to store immutable frozenset values and
update get_fields (and any other places that currently insert or return sets,
e.g., the logic referenced around get_fields at lines ~438-445) so it converts
computed field sets to frozenset before caching and always returns a frozenset
to callers; make sure any code that previously mutated the returned set either
copies to a new set before mutation or is updated to work with the immutable
frozenset, referencing _fields_cache and get_fields to locate the changes.
In `@tests/test_baker.py`:
- Around line 1550-1575: The tests mutate baker.Baker._fields_cache and only
partially clean it, causing order-dependent failures; fix by isolating
_fields_cache for these tests: snapshot the original baker.Baker._fields_cache
at test start (or use a fixture) and restore it after each test (or set
baker.Baker._fields_cache = {} before calling Baker), updating the tests like
test_get_fields_result_is_cached,
test_get_fields_different_models_cached_separately, and
test_get_fields_includes_all_non_reverse_fields to perform the snapshot/restore
(or use a pytest fixture that yields and restores baker.Baker._fields_cache) so
each test runs with a known, clean cache state.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e85fd69f-794e-4bea-a68a-fad357e16576
📒 Files selected for processing (3)
CHANGELOG.mdmodel_bakery/baker.pytests/test_baker.py
Describe the change
This change caches
get_fields()call that can be expensive on larger models. This change almost halved our test execution time:Before:
After:
PR Checklist
Summary by CodeRabbit
Changed
Tests