fix: warn instead of error when extra args passed to sampling methods from UI#1369
fix: warn instead of error when extra args passed to sampling methods from UI#1369mihow wants to merge 2 commits into
Conversation
✅ Deploy Preview for antenna-preview canceled.
|
✅ Deploy Preview for antenna-ssec canceled.
|
📝 WalkthroughWalkthroughSampling population now filters stored kwargs against the selected method signature. Interval sampling accepts and forwards an optional ChangesSampling compatibility
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 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.
Pull request overview
This PR addresses backward/forward compatibility between stored kwargs on SourceImageCollection records and the current sampling method signatures by filtering unsupported kwargs (logging a warning instead of raising). It also extends the interval sampler to accept max_num, preventing failures for legacy capture sets that still store max_num in their JSON config.
Changes:
- Filter
SourceImageCollection.kwargsagainst the target sampling method’s signature before calling it, and warn on ignored keys. - Add
max_numsupport tosample_intervaland pass it through tosample_captures_by_interval. - Update/extend tests to ensure extra kwargs no longer break population and that interval sampling respects
max_num.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
ami/main/models.py |
Filters stored sampling kwargs by method signature, warns on ignored kwargs, and adds max_num support to interval sampling. |
ami/main/tests.py |
Updates the “extra arguments” test to assert successful population and adds coverage for max_num in interval sampling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ignored_kwargs = sorted(set(kwargs) - set(filtered_kwargs)) | ||
| if ignored_kwargs: | ||
| task_logger.warning( | ||
| "Ignoring unsupported arguments for samplng method %s: %s", |
| self, | ||
| minute_interval: int = 10, | ||
| max_num: int | None = None, | ||
| exclude_events: list[int] = [], |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
ami/main/models.py (1)
5113-5116: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winEnforce
max_numlimit globally across all deployments.While passing
max_numtosample_captures_by_intervalin the loop above is an excellent optimization to avoid fetching unnecessary rows from the database, it only limits the count per deployment. If a project has 5 deployments andmax_num=10, the current logic will collect up to 50 captures in total.To properly enforce the global
max_numlimit for the collection, slice the combined and chronologically sorted final list before returning it. This guarantees we return the absolute earliestmax_numcaptures across the entire project.🐛 Proposed fix
# Return results in a deterministic order. Sort by timestamp (oldest first), # then by primary key to stabilize ordering when timestamps are equal. captures_list = sorted(captures, key=lambda s: (s.timestamp is None, s.timestamp, s.pk)) + if max_num is not None: + captures_list = captures_list[:max_num] return captures_list🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ami/main/models.py` around lines 5113 - 5116, Update the final return path after sorting captures_list to enforce max_num globally: slice the combined, chronologically ordered list to at most max_num entries before returning. Keep the existing deterministic timestamp and primary-key ordering intact.
🧹 Nitpick comments (2)
ami/main/models.py (1)
4927-4931: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueFix typographical error in the log message.
There is a minor typo in the log message ("samplng" instead of "sampling").
♻️ Proposed fix
task_logger.warning( - "Ignoring unsupported arguments for samplng method %s: %s", + "Ignoring unsupported arguments for sampling method %s: %s", method_name, ", ".join(ignored_kwargs), )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ami/main/models.py` around lines 4927 - 4931, Correct the typo in the warning message within the unsupported-arguments logging call, changing “samplng” to “sampling” while preserving the existing formatting and arguments.ami/main/tests.py (1)
1435-1447: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winStrengthen test to verify global enforcement of
max_num.This test currently passes despite the per-deployment bug in
sample_intervalbecauseself.project_oneonly contains a single deployment by default.To ensure this test correctly validates that
max_numapplies as a global limit (rather than returningmax_numcaptures per deployment), consider adding a second deployment with its own captures.♻️ Proposed test update
def test_interval_sample_accepts_max_num(self): - from ami.main.models import SourceImageCollection + from ami.main.models import SourceImageCollection, Deployment + + # Add a second deployment to ensure max_num acts as a global limit + deployment_two = Deployment.objects.create(name="Test Deployment Two", project=self.project_one) + create_captures(deployment=deployment_two, num_nights=1, images_per_night=2, interval_minutes=1) collection = SourceImageCollection.objects.create( name="Test Interval Sample With Max Num", project=self.project_one, method="interval", kwargs={"minute_interval": 1, "max_num": 1}, ) collection.save() collection.populate_sample() self.assertEqual(collection.images.count(), 1)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ami/main/tests.py` around lines 1435 - 1447, Add a second deployment with its own captures to test_interval_sample_accepts_max_num before populating the sample, then assert the collection still contains exactly one image. Ensure the fixtures exercise multiple deployments so max_num is validated as a global limit rather than a per-deployment limit.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@ami/main/models.py`:
- Around line 5113-5116: Update the final return path after sorting
captures_list to enforce max_num globally: slice the combined, chronologically
ordered list to at most max_num entries before returning. Keep the existing
deterministic timestamp and primary-key ordering intact.
---
Nitpick comments:
In `@ami/main/models.py`:
- Around line 4927-4931: Correct the typo in the warning message within the
unsupported-arguments logging call, changing “samplng” to “sampling” while
preserving the existing formatting and arguments.
In `@ami/main/tests.py`:
- Around line 1435-1447: Add a second deployment with its own captures to
test_interval_sample_accepts_max_num before populating the sample, then assert
the collection still contains exactly one image. Ensure the fixtures exercise
multiple deployments so max_num is validated as a global limit rather than a
per-deployment limit.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7d652d5a-4f38-4cf6-ba72-22154fefc9c6
📒 Files selected for processing (2)
ami/main/models.pyami/main/tests.py
Fixes #1266 by filtering out unsupported arguments. Also adds support for max number of captures to the interval sampler.
Summary by CodeRabbit
New Features
Bug Fixes