-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add linting for ifEmpty(null) #3411
Open
lmReef
wants to merge
9
commits into
nf-core:dev
Choose a base branch
from
lmReef:add_linting_for_ifEmpty_null_dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1682d06
add linting for ifEmpty(null) in pipeline and subworkflow linters
e832a97
comment formatting
c198c45
[automated] Update CHANGELOG.md
nf-core-bot 62118b7
Update CHANGELOG.md
lmReef e3db52d
Merge branch 'dev' into add_linting_for_ifEmpty_null_dev
mashehu def95d7
[automated] Fix code linting
nf-core-bot fba7d88
remove latin encoding, improve regex
775da79
fix latin1 encoding, remove unused import, remove gitignore file filt…
49e67a6
add test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# pipeline_if_empty_null | ||
|
||
```{eval-rst} | ||
.. automethod:: nf_core.pipelines.lint.PipelineLint.pipeline_if_empty_null | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
docs/api/_src/subworkflow_lint_tests/subworkflow_if_empty_null.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# subworkflow_if_empty_null | ||
|
||
```{eval-rst} | ||
.. automethod:: nf_core.subworkflows.lint.SubworkflowLint.subworkflow_if_empty_null | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import logging | ||
import os | ||
import re | ||
from pathlib import Path | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def pipeline_if_empty_null(self, root_dir=None): | ||
"""Check for ifEmpty(null) | ||
|
||
There are two general cases for workflows to use the channel operator `ifEmpty`: | ||
1. `ifEmpty( [ ] )` to ensure a process executes, for example when an input file is optional (although this can be replaced by `toList()`). | ||
2. When a channel should not be empty and throws an error `ifEmpty { error ... }`, e.g. reading from an empty samplesheet. | ||
|
||
There are multiple examples of workflows that inject null objects into channels using `ifEmpty(null)`, which can cause unhandled null pointer exceptions. | ||
This lint test throws warnings for those instances. | ||
""" | ||
passed = [] | ||
warned = [] | ||
file_paths = [] | ||
pattern = re.compile(r"ifEmpty\s*\(\s*null\s*\)") | ||
|
||
# Pipelines don"t provide a path, so use the workflow path. | ||
# Modules run this function twice and provide a string path | ||
if root_dir is None: | ||
root_dir = self.wf_path | ||
|
||
for root, dirs, files in os.walk(root_dir, topdown=True): | ||
for fname in files: | ||
try: | ||
with open(Path(root, fname), encoding="latin1") as fh: | ||
for line in fh: | ||
if re.findall(pattern, line): | ||
warned.append( | ||
f"`ifEmpty(null)` found in `{ | ||
fname}`: _{line}_" | ||
) | ||
file_paths.append(Path(root, fname)) | ||
except FileNotFoundError: | ||
log.debug( | ||
f"Could not open file { | ||
fname} in pipeline_if_empty_null lint test" | ||
) | ||
|
||
if len(warned) == 0: | ||
passed.append("No `ifEmpty(null)` strings found") | ||
|
||
# return file_paths for use in subworkflow lint | ||
return {"passed": passed, "warned": warned, "file_paths": file_paths} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import logging | ||
|
||
from nf_core.pipelines.lint.pipeline_if_empty_null import pipeline_if_empty_null | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def subworkflow_if_empty_null(_, subworkflow): | ||
"""Check for ifEmpty(null) | ||
|
||
There are two general cases for workflows to use the channel operator `ifEmpty`: | ||
1. `ifEmpty( [ ] )` to ensure a process executes, for example when an input file is optional (although this can be replaced by `toList()`). | ||
2. When a channel should not be empty and throws an error `ifEmpty { error ... }`, e.g. reading from an empty samplesheet. | ||
|
||
There are multiple examples of workflows that inject null objects into channels using `ifEmpty(null)`, which can cause unhandled null pointer exceptions. | ||
This lint test throws warnings for those instances. | ||
""" | ||
|
||
# Main subworkflow directory | ||
swf_results = pipeline_if_empty_null(None, root_dir=subworkflow.component_dir) | ||
for i, warning in enumerate(swf_results["warned"]): | ||
subworkflow.warned.append(("subworkflow_if_empty_null", warning, swf_results["file_paths"][i])) | ||
for i, passed in enumerate(swf_results["passed"]): | ||
subworkflow.passed.append(("subworkflow_if_empty_null", passed, subworkflow.component_dir)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from pathlib import Path | ||
|
||
import yaml | ||
|
||
import nf_core.pipelines.create | ||
import nf_core.pipelines.lint | ||
|
||
from ..test_lint import TestLint | ||
|
||
|
||
class TestLintIfEmptyNull(TestLint): | ||
def setUp(self) -> None: | ||
super().setUp() | ||
self.new_pipeline = self._make_pipeline_copy() | ||
self.nf_core_yml_path = Path(self.new_pipeline) / ".nf-core.yml" | ||
with open(self.nf_core_yml_path) as f: | ||
self.nf_core_yml = yaml.safe_load(f) | ||
|
||
def test_if_empty_null_throws_warn(self): | ||
"""Tests finding ifEmpty(null) in file throws warn in linting""" | ||
# Create a file and add examples that should fail linting | ||
txt_file = Path(self.new_pipeline) / "docs" / "test.txt" | ||
with open(txt_file, "w") as f: | ||
f.writelines( | ||
[ | ||
"ifEmpty(null)\n", | ||
"ifEmpty (null)\n", | ||
"ifEmpty( null )\n", | ||
"ifEmpty ( null )\n", | ||
".ifEmpty(null)\n", | ||
". ifEmpty(null)\n", | ||
"|ifEmpty(null)\n", | ||
"| ifEmpty(null)\n", | ||
] | ||
) | ||
lint_obj = nf_core.pipelines.lint.PipelineLint(self.new_pipeline) | ||
lint_obj._load() | ||
result = lint_obj.pipeline_if_empty_null() | ||
assert len(result["warned"]) == 8 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have latin1 enconding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interestingly without either
encoding="latin1"
orerrors="ignore"
it fails to decode files both during the pytest and when testing the linting on a new pipeline.I took this from the file pipelines/lint/pipeline_todos.py. Is this a me issue or should I add
latin1
back in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I just realized template_strings.py uses
encoding="latin1"
for reading files too, I'll add it back in for nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad, let's add
encoding="latin1"
back, thanks!