Skip to content

feat: auto detect string resources in resource definitions given by --default-resources and --set-resources in the string yields a syntax error when being interpreted as a python expression; this will obviate the need to quote such expressions in many cases; further, fix interpretation of quoted resources by removing quotes after intepretation#3532

Merged
johanneskoester merged 6 commits intomainfrom
feat/handle-syntax-error-resource-expr
Apr 14, 2025

Conversation

@johanneskoester
Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester commented Apr 14, 2025

Description

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (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

  • Bug Fixes
    • Improved error handling when evaluating resource expressions to better handle invalid input.
    • Updated test commands to specify core usage for more consistent test execution.
  • New Features
    • Added tests for resource quoting via profile and CLI options.
    • Introduced a new test rule that verifies resource value handling.
  • Improvements
    • Enhanced validation for resource configuration formats with clearer error messages.
    • Refined resource value parsing to remove unnecessary quotes for consistency.

…-default-resources and --set-resources in the string yields a syntax error when trying to be interpreted as a python expression; this will obviate the need to quote such expressions in many cases
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 14, 2025

📝 Walkthrough

Walkthrough

The changes update the exception handling logic in the eval_resource_expression function within src/snakemake/resources.py to catch both NameError and SyntaxError exceptions, instead of only NameError. Additionally, an unused import of logger from snakemake.logging was removed from the same file. The parse_set_resources function in src/snakemake/cli.py was modified to strip surrounding quotes from resource string values. In src/snakemake/profiles.py, stricter validation was added to ensure values are dictionaries in format_two_level_dict, raising a WorkflowError if not. Two test functions in tests/tests.py were modified to include the --cores 1 option in their Snakemake command invocations. New tests, a Snakefile rule, a shell script, and profile configurations were added to test resource quoting behavior.

Changes

File(s) Change Summary
src/snakemake/resources.py Removed unused logger import; expanded exception handling in eval_resource_expression to catch both NameError and SyntaxError.
src/snakemake/cli.py Modified parse_set_resources to strip surrounding quotes from resource string values.
src/snakemake/profiles.py Added validation in format_two_level_dict to ensure all values are dicts; raise WorkflowError if not; updated method signature and calls accordingly.
tests/tests.py Added --cores 1 option to Snakemake command strings in test_ancient_cli and test_issue3361_pass; added new tests test_resource_quoting_profile, test_resource_quoting_cli, test_default_resource_quoting_cli, and test_default_resource_quoting_profile.
tests/test_resource_quoting/Snakefile Added rule a that writes the resource test value to test.txt.
tests/test_resource_quoting/expected-results/test.txt Added file containing "foo".
tests/test_resource_quoting/qsub Added shell script simulating job submission and logging for test resource quoting.
tests/test_resource_quoting/test-profile/config.yaml Added profile config YAML setting resource test to 'foo' string.
tests/test_resource_quoting/test-profile-default/config.yaml Added profile config YAML setting default resource test to 'foo' string.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ResourcesModule

    Caller->>ResourcesModule: eval_resource_expression(value)
    alt Expression evaluates successfully
        ResourcesModule-->>Caller: return evaluated result
    else NameError or SyntaxError occurs
        ResourcesModule-->>Caller: return original value
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7408f23 and 8907dfa.

⛔ Files ignored due to path filters (1)
  • tests/test_resource_quoting/test-profile-default/gurobi.log is excluded by !**/*.log
📒 Files selected for processing (1)
  • tests/test_resource_quoting/test-profile-default/config.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/test_resource_quoting/test-profile-default/config.yaml
⏰ Context from checks skipped due to timeout of 90000ms (34)
  • GitHub Check: tests (10, windows-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py312)
  • GitHub Check: tests (10, ubuntu-latest, py311)
  • GitHub Check: tests (9, windows-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py312)
  • GitHub Check: tests (9, ubuntu-latest, py311)
  • GitHub Check: tests (8, windows-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py312)
  • GitHub Check: tests (8, ubuntu-latest, py311)
  • GitHub Check: tests (7, windows-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py312)
  • GitHub Check: tests (7, ubuntu-latest, py311)
  • GitHub Check: tests (6, windows-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py312)
  • GitHub Check: tests (6, ubuntu-latest, py311)
  • GitHub Check: tests (5, windows-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py312)
  • GitHub Check: tests (5, ubuntu-latest, py311)
  • GitHub Check: tests (4, windows-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py312)
  • GitHub Check: tests (4, ubuntu-latest, py311)
  • GitHub Check: tests (3, macos-latest, py312)
  • GitHub Check: tests (3, windows-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py312)
  • GitHub Check: tests (3, ubuntu-latest, py311)
  • GitHub Check: tests (2, macos-latest, py312)
  • GitHub Check: tests (2, windows-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py312)
  • GitHub Check: tests (2, ubuntu-latest, py311)
  • GitHub Check: tests (1, macos-latest, py312)
  • GitHub Check: tests (1, windows-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py312)
  • GitHub Check: tests (1, ubuntu-latest, py311)
  • GitHub Check: apidocs

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@johanneskoester johanneskoester changed the title feat: auto detect string resources in resource definitions given by --default-resources and --set-resources in the string yields a syntax error when trying to be interpreted as a python expression; this will obviate the need to quote such expressions in many cases feat: auto detect string resources in resource definitions given by --default-resources and --set-resources in the string yields a syntax error when trying to be interpreted as a python expression; this will obviate the need to quote such expressions in many cases; further, fix interpretation of quoted resources by removing quotes after intepretation Apr 14, 2025
@johanneskoester johanneskoester changed the title feat: auto detect string resources in resource definitions given by --default-resources and --set-resources in the string yields a syntax error when trying to be interpreted as a python expression; this will obviate the need to quote such expressions in many cases; further, fix interpretation of quoted resources by removing quotes after intepretation feat: auto detect string resources in resource definitions given by --default-resources and --set-resources in the string yields a syntax error when being interpreted as a python expression; this will obviate the need to quote such expressions in many cases; further, fix interpretation of quoted resources by removing quotes after intepretation Apr 14, 2025
@johanneskoester johanneskoester merged commit 81f09bd into main Apr 14, 2025
48 checks passed
@johanneskoester johanneskoester deleted the feat/handle-syntax-error-resource-expr branch April 14, 2025 21:25
johanneskoester pushed a commit that referenced this pull request Apr 14, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.2.0](v9.1.11...v9.2.0)
(2025-04-14)


### Features

* auto detect string resources in resource definitions given by
--default-resources and --set-resources in the string yields a syntax
error when being interpreted as a python expression; this will obviate
the need to quote such expressions in many cases; further, fix
interpretation of quoted resources by removing quotes after
intepretation
([#3532](#3532))
([81f09bd](81f09bd))


### Bug Fixes

* restore accidentally changed behavior of --cores. If not specified,
Snakemake now complains again, asking for --cores to be specified (of
--jobs in case of remote exec). As before, if a default is desired, it
can be easily set via a profile
([#3531](#3531))
([11b2b30](11b2b30))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
…-default-resources and --set-resources in the string yields a syntax error when being interpreted as a python expression; this will obviate the need to quote such expressions in many cases; further, fix interpretation of quoted resources by removing quotes after intepretation (snakemake#3532)

### Description

<!--Add a description of your PR here-->

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`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).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **Bug Fixes**
- Improved error handling when evaluating resource expressions to better
handle invalid input.
- Updated test commands to specify core usage for more consistent test
execution.
- **New Features**
	- Added tests for resource quoting via profile and CLI options.
	- Introduced a new test rule that verifies resource value handling.
- **Improvements**
- Enhanced validation for resource configuration formats with clearer
error messages.
- Refined resource value parsing to remove unnecessary quotes for
consistency.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
🤖 I have created a release *beep* *boop*
---


##
[9.2.0](snakemake/snakemake@v9.1.11...v9.2.0)
(2025-04-14)


### Features

* auto detect string resources in resource definitions given by
--default-resources and --set-resources in the string yields a syntax
error when being interpreted as a python expression; this will obviate
the need to quote such expressions in many cases; further, fix
interpretation of quoted resources by removing quotes after
intepretation
([snakemake#3532](snakemake#3532))
([81f09bd](snakemake@81f09bd))


### Bug Fixes

* restore accidentally changed behavior of --cores. If not specified,
Snakemake now complains again, asking for --cores to be specified (of
--jobs in case of remote exec). As before, if a default is desired, it
can be easily set via a profile
([snakemake#3531](snakemake#3531))
([11b2b30](snakemake@11b2b30))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant