fix: implement Resources.setdefault, restoring backwards compatibility with old resources implementation#3968
Conversation
This provides backwards compatibility for attributes that previously used native dict before switching to the new Resources class in "fix: standardize handling of resources" (4cada18).
📝 WalkthroughWalkthroughAdded a new ChangesResources class enhancement
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 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.
🧹 Nitpick comments (1)
src/snakemake/resources.py (1)
1128-1132: Implementation looks correct.The method properly delegates to
__setitem__for value storage and correctly returns the storedResource. This provides the expected dict-likesetdefaultbehavior for backwards compatibility.Consider adding a return type annotation for API clarity:
- def setdefault(self, ix: str, val: ValidResource | Resource | None): + def setdefault(self, ix: str, val: ValidResource | Resource | None) -> Resource:,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/snakemake/resources.py` around lines 1128 - 1132, The setdefault method lacks a return type annotation; add an explicit return type (e.g., -> ValidResource | Resource | None or the precise Resource type used in this module) to the setdefault signature so callers and type checkers know what is returned, keeping the implementation as-is and referencing the existing setdefault method, __setitem__, and self._data for context.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/snakemake/resources.py`:
- Around line 1128-1132: The setdefault method lacks a return type annotation;
add an explicit return type (e.g., -> ValidResource | Resource | None or the
precise Resource type used in this module) to the setdefault signature so
callers and type checkers know what is returned, keeping the implementation
as-is and referencing the existing setdefault method, __setitem__, and
self._data for context.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ac45d0be-c7bb-4eb9-b082-47572046658d
📒 Files selected for processing (1)
src/snakemake/resources.py
🤖 I have created a release *beep* *boop* --- ## [9.21.1](v9.21.0...v9.21.1) (2026-05-29) ### Bug Fixes * add default json function to benchmarks ([#4128](#4128)) ([41fab22](41fab22)) * do not rerun when checkpoint job missing but downstream file exists ([#4124](#4124)) ([a060b93](a060b93)) * ensure that error logs contain all available details ([#4183](#4183)) ([74a86e9](74a86e9)) * handle missing pss attribute in benchmark on Windows ([#4160](#4160)) ([da52080](da52080)) * implement Resources.setdefault ([#3968](#3968)) ([2413e99](2413e99)) * reporting remote nodes number ([#3978](#3978)) ([8c534f0](8c534f0)) * resolve pathvars before constructing storage queries ([#3969](#3969)) ([bd15237](bd15237)) * storage temp() file cleanup with RemoteProvider ([#4189](#4189)) ([898bad1](898bad1)) * tolerate FileNotFoundError in drop_iocache ([#4153](#4153)) ([#4191](#4191)) ([ce26b28](ce26b28)) ### Documentation * Added guide on debugging workflows ([#4029](#4029)) ([3d052ae](3d052ae)) * **cli:** Remove broken ref bold markup ([#4204](#4204)) ([1200ebf](1200ebf)) * remove duplicated resources attribute in rules.rst ([#4190](#4190)) ([6c8ecdd](6c8ecdd)) * **rules:** Update script type hint advice ([#4193](#4193)) ([6108712](6108712)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
This provides backwards compatibility for attributes that previously used native dict before switching to the new Resources class in "fix: standardize handling of resources" (4cada18).
Follow-up to #3421
QC
test_resources_setdefault.pyor foldertest_resources_setdefault/Snakefilebe more appropriate?docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).Summary by CodeRabbit