Skip to content

fix: Wrong linenumbers reported when linting#2985

Merged
johanneskoester merged 11 commits intosnakemake:mainfrom
Kleinjohann:fix/wrong_lineno_when_linting
Sep 4, 2024
Merged

fix: Wrong linenumbers reported when linting#2985
johanneskoester merged 11 commits intosnakemake:mainfrom
Kleinjohann:fix/wrong_lineno_when_linting

Conversation

@Kleinjohann
Copy link
Copy Markdown
Contributor

@Kleinjohann Kleinjohann commented Jul 25, 2024

This PR addresses two issues that led to incorrect linenumbers reported when running snakemake --lint.

  1. If an error was encountered in --lint mode, the stacktrace wasn't cut to remove Snakemake internals and the linenumbers were not adjusted, because the linemap was not available. Fixed by storing the workflow object to make the linemap accessible.
  2. For rule lints, the linemap wasn't applied, resulting in wrong linenumbers in the output of snakemake --lint. Fixed by applying the linemap before outputting the lints.

I added tests for both cases.

Fixes #2574.

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

  • New Features

    • Enhanced linting functionality with improved line number retrieval for rules.
    • Introduced a new method to centralize line number logic in the linting process.
    • Added a new test for handling specific error conditions in Snakemake workflows.
  • Bug Fixes

    • Improved accuracy in asserting reported line numbers for unused parameters during linting tests.
  • Documentation

    • New configuration file for testing, improving organization and readability of test setups.

@Kleinjohann Kleinjohann changed the title Fix: Wrong linenumbers reported when linting fix: Wrong linenumbers reported when linting Jul 25, 2024
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 13, 2024

Walkthrough

The recent changes enhance the Snakemake codebase by improving the linting functionality, refining error handling for workflows, and introducing a new test configuration. Key modifications include better line number accuracy in error messages and the ability to store workflow objects internally, which optimizes subsequent operations. These updates aim to address reported issues, particularly incorrect error line reporting, leading to a more robust and maintainable system.

Changes

Files Change Summary
snakemake/api.py Enhanced lint method to store the workflow object in _workflow_store, improving state management.
snakemake/linting/rules.py Added get_lineno method to centralize line number retrieval, updating item_desc_plain and item_desc_json methods to use it, improving code maintainability.
tests/test_issue2574/Snakefile Introduced a new Snakefile for testing, accessing a non-existent configuration key to highlight potential errors, with a simple print statement for debugging.
tests/test_issue2574/config.yaml New configuration file defining structured test parameters.
tests/test_linting.py Updated test_lint function to assert correct line number reporting for unused parameters in linting cases.
tests/tests.py Added test_issue2574 function to validate behavior on encountering a KeyError, ensuring accurate error reporting in workflows.

Assessment against linked issues

Objective Addressed Explanation
Wrong error line numbers reported (Issue #2574)
Improve accuracy of line number reporting
Enhance maintainability of linting and workflows
Introduce structured testing configurations

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 resolve resolve all the CodeRabbit review comments.
  • @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.
Early access features: disabled

We are currently testing the following features in early access:

  • Anthropic claude-3-5-sonnet for code reviews: Anthropic claims that the new Claude model has stronger code understanding and code generation capabilities than their previous models. Note: Our default code review model was also updated late last week. Please compare the quality of the reviews between the two models by toggling the early access feature.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.
  • Please join our Discord Community to provide feedback and report issues on the discussion post.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 59150d3 and ae0a659.

Files selected for processing (6)
  • snakemake/api.py (1 hunks)
  • snakemake/linting/rules.py (1 hunks)
  • tests/test_issue2574/Snakefile (1 hunks)
  • tests/test_issue2574/config.yaml (1 hunks)
  • tests/test_linting.py (1 hunks)
  • tests/tests.py (1 hunks)
Additional context used
Ruff
tests/test_linting.py

48-48: Found assertion on exception e in except block, use pytest.raises() instead

(PT017)

snakemake/linting/rules.py

14-14: Missing return type annotation for public function item_desc_json

(ANN201)


14-14: Missing type annotation for self in method

(ANN101)


14-14: Missing type annotation for function argument rule

(ANN001)


18-18: Missing return type annotation for public function get_lineno

(ANN201)


18-18: Missing type annotation for self in method

(ANN101)


18-18: Missing type annotation for function argument rule

(ANN001)

tests/tests.py

2051-2051: Missing return type annotation for public function test_issue2574

Add return type annotation: None

(ANN201)


2054-2054: Function call with shell=True parameter identified, security issue

(S604)


2054-2054: Undefined name subprocess

(F821)


2055-2055: Undefined name subprocess

(F821)


2055-2055: Undefined name subprocess

(F821)

Additional comments not posted (5)
tests/test_issue2574/config.yaml (1)

1-2: Configuration file looks good.

The configuration file is minimal and appropriate for testing purposes.

tests/test_issue2574/Snakefile (1)

1-4: Snakefile setup is appropriate for testing.

The Snakefile is set up to intentionally access a non-existent key, which is suitable for testing error line number reporting.

snakemake/linting/rules.py (2)

11-12: Centralized line number retrieval is a good practice.

The use of get_lineno centralizes the logic for line number retrieval, enhancing maintainability.


15-16: Consistent use of get_lineno enhances maintainability.

The method's update to use get_lineno aligns with best practices for centralized logic.

snakemake/api.py (1)

354-354: Storing the workflow object improves efficiency.

The addition of self._workflow_store = workflow in the lint method enhances efficiency by avoiding redundant calls to _get_workflow.

Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! Can you address the AI review comments? Also, the code should be formatted with black.

@johanneskoester johanneskoester self-assigned this Aug 13, 2024
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
tests/test_issue2574/Snakefile (1)

4-4: Config key does not exist, but variable "X" is used.

The config key "unexisting_key" does not exist in the configuration file, confirming the intention to test error handling. However, the variable "X" is used in the Snakefile, contradicting the initial assumption that it was unused. Please review the usage of "X" to ensure it aligns with the intended test logic.

Analysis chain

Verify the non-existence of the config key and the unused variable.

The code segment is accessing a non-existing config key "unexisting_key" and assigning it to an unused variable "X".

Verify the non-existence of the config key:

Verify the unused variable:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the non-existence of the config key "unexisting_key".

# Test: Check if the config key exists. Expect: The config key does not exist.
if grep -q "unexisting_key" "tests/test_issue2574/config.yaml"; then
  echo "The config key exists."
else
  echo "The config key does not exist."
fi

Length of output: 128


Script:

#!/bin/bash
# Description: Verify the unused variable "X".

# Test: Check if the variable is used. Expect: The variable is not used.
if grep -q "X" "tests/test_issue2574/Snakefile"; then
  echo "The variable is used."
else
  echo "The variable is not used."
fi

Length of output: 95

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ae0a659 and 2b59080.

Files selected for processing (4)
  • snakemake/linting/rules.py (1 hunks)
  • tests/test_issue2574/Snakefile (1 hunks)
  • tests/test_linting.py (1 hunks)
  • tests/tests.py (1 hunks)
Additional context used
Path-based instructions (3)
tests/test_linting.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

snakemake/linting/rules.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

tests/tests.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

Learnings (1)
tests/tests.py (1)
Learnt from: johanneskoester
PR: snakemake/snakemake#2985
File: tests/tests.py:2051-2051
Timestamp: 2024-08-13T09:25:24.046Z
Learning: In the Snakemake repository, avoid suggesting return type annotations for test functions.
Ruff
tests/tests.py

2054-2054: Undefined name subprocess

(F821)


2057-2057: Undefined name subprocess

(F821)


2058-2058: Undefined name subprocess

(F821)

Additional comments not posted (7)
tests/test_issue2574/Snakefile (2)

1-1: Verify the existence of the config file.

Ensure that the config file "config.yaml" exists in the same directory as this Snakefile.

Run the following script to verify the existence of the config file:

Verification successful

Config file exists as expected.

The config file "config.yaml" is present in the directory "tests/test_issue2574", confirming that the Snakemake file's reference is valid. No issues found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the config file "config.yaml".

# Test: Check if the config file exists. Expect: The config file exists.
if [ -f "tests/test_issue2574/config.yaml" ]; then
  echo "The config file exists."
else
  echo "The config file does not exist."
fi

Length of output: 102


6-6: Verify the purpose of the print statement.

The code segment is printing the number 1 without any specific purpose.

Verify the purpose of the print statement:

Verification successful

The print statement appears to be used for debugging purposes.

The print(1) statement follows an attempt to access a non-existing key in a configuration file, suggesting it might be used to confirm that the script reaches this point despite the error. There is no indication of any other specific purpose for this print statement.

  • Location: tests/test_issue2574/Snakefile, line 6
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the purpose of the print statement.

# Test: Check if the print statement is used for any specific purpose. Expect: The print statement is not used for any specific purpose.
if grep -q "print(1)" "tests/test_issue2574/Snakefile"; then
  echo "The print statement is used for a specific purpose."
else
  echo "The print statement is not used for any specific purpose."
fi

Length of output: 164


Script:

#!/bin/bash
# Extract lines around the print statement to understand its context.
grep -A 5 -B 5 "print(1)" tests/test_issue2574/Snakefile

Length of output: 125

tests/test_linting.py (1)

46-48: LGTM!

The code changes are approved. The assertion correctly checks that the output contains the expected line number for the specific linting case.

snakemake/linting/rules.py (4)

7-7: LGTM!

The import statement is correct and necessary for using the Rule type annotation in the get_lineno method.


12-13: LGTM!

The change to call self.get_lineno(rule) instead of directly accessing rule.lineno is correct and improves the accuracy of line number reporting by allowing for potential adjustments based on the workflow's context.


16-17: LGTM!

The change to call self.get_lineno(rule) instead of directly accessing rule.lineno is correct and improves the accuracy of line number reporting by allowing for potential adjustments based on the workflow's context.


19-23: LGTM!

The new get_lineno method is correctly implemented and improves the accuracy of line number reporting by allowing for potential adjustments based on the workflow's context. The type annotations for the rule parameter and return value are also correct and improve code clarity and maintainability.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a 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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2b59080 and 58018fa.

Files selected for processing (1)
  • tests/tests.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/tests.py

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 58018fa and 7fb773b.

Files selected for processing (1)
  • snakemake/linting/rules.py (1 hunks)
Additional context used
Path-based instructions (1)
snakemake/linting/rules.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

Ruff
snakemake/linting/rules.py

6-6: snakemake.io.is_flagged imported but unused

Remove unused import: snakemake.io.is_flagged

(F401)

Additional comments not posted (5)
snakemake/linting/rules.py (5)

4-4: LGTM!

The import statement for Union from the typing module is necessary for using the Union type in the type annotation of the new get_lineno method.


8-8: LGTM!

The import statement for Rule from the snakemake.rules module is necessary for using the Rule type in the type annotation of the rule parameter in the new get_lineno method.


13-14: LGTM!

The change to call the new get_lineno method in the item_desc_plain method improves the accuracy of line number reporting by allowing for potential adjustments based on the workflow's context.


17-18: LGTM!

The change to call the new get_lineno method in the item_desc_json method improves the accuracy of line number reporting by allowing for potential adjustments based on the workflow's context.


20-24: LGTM!

The new get_lineno method encapsulates the logic for line number retrieval within its own method, improving the maintainability and clarity of the code. The type annotations for the rule parameter and the return type enhance the code clarity and maintainability.

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Sep 2, 2024

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a 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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7fb773b and e9a7997.

Files selected for processing (1)
  • snakemake/linting/rules.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • snakemake/linting/rules.py

@johanneskoester johanneskoester merged commit 3a8bd36 into snakemake:main Sep 4, 2024
johanneskoester added a commit that referenced this pull request Sep 4, 2024
🤖 I have created a release *beep* *boop*
---


##
[8.19.1](v8.19.0...v8.19.1)
(2024-09-04)


### Bug Fixes

* fix issues with misinterpretation of max-jobs-per-timespan and
max-jobs-per-seconds
([#3067](#3067))
([d82453b](d82453b))
* pip deployment path
([#3062](#3062))
([bf9305b](bf9305b))
* return empty set if rate limiter at max
([#3060](#3060))
([4e59963](4e59963))
* use wrapt_timeout_decorator, instead of stopit
([#2938](#2938))
([3b64e41](3b64e41))
* Wrong linenumbers reported when linting
([#2985](#2985))
([3a8bd36](3a8bd36))


### Documentation

* update `doc-environment.yml` file and Documentation Setup
documentation
([#3058](#3058))
([a540a2e](a540a2e))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Johannes Köster <johannes.koester@tu-dortmund.de>
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.

Wrong error line numbers reported in version 8.0.1

2 participants