Skip to content

Commit

Permalink
fix: Workaround for 'Shadows built-in name input' inspection on lam…
Browse files Browse the repository at this point in the history
…bda parameters, see #133
  • Loading branch information
iromeo committed Sep 8, 2020
1 parent 25a75f8 commit c74d94e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Fixed:
- Expected `collections.iterable` but got `output:` false positive (see #317)
- Expected type `Iterable` (..), git `input: ` instead (see #319)
- Expected type `Iterable` (..), git `input: ` instead (see #320)
- Workaround for: 'Shadows built-in name `input`' inspection on lambda parameters (see #133)

Version 0.8.0 build #0.8.0.214
-------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.jetbrains.snakecharm.inspections

import com.intellij.psi.PsiElement
import com.jetbrains.python.inspections.PyInspectionExtension
import com.jetbrains.python.psi.types.PyType
import com.jetbrains.python.psi.types.TypeEvalContext
import com.jetbrains.snakecharm.lang.psi.SmkRuleOrCheckpointArgsSection
import com.jetbrains.snakecharm.lang.psi.types.SmkAvailableForSubscriptionType

class SmkIgnorePyInspectionExtension: PyInspectionExtension() {
Expand All @@ -13,7 +15,8 @@ class SmkIgnorePyInspectionExtension: PyInspectionExtension() {
return false
}

// ignoreShadowed
override fun ignoreShadowed(element: PsiElement) = element is SmkRuleOrCheckpointArgsSection

// ignoreMissingDocstring
// ignoreMethodParameters
// getFunctionParametersFromUsage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package com.jetbrains.snakecharm.lang.highlighter

import com.intellij.psi.PsiFile
import com.jetbrains.python.inspections.PyShadowingBuiltinsInspection
import com.jetbrains.python.inspections.PyUnboundLocalVariableInspection
import com.jetbrains.python.inspections.PyUnreachableCodeInspection
import com.jetbrains.python.psi.PythonVisitorFilter
import com.jetbrains.python.psi.PyElementVisitor
import com.jetbrains.python.psi.PythonVisitorFilter
import com.jetbrains.python.validation.ReturnAnnotator

class SnakemakeVisitorFilter: PythonVisitorFilter {
class SnakemakeVisitorFilter : PythonVisitorFilter {
private val unsupportedClasses = listOf(
/** Instead use [com.jetbrains.snakecharm.lang.validation.SmkReturnAnnotator] **/
ReturnAnnotator::class.java,
// [HACK] See https://github.com/JetBrains-Research/snakecharm/issues/14
PyUnreachableCodeInspection::class.java,
// TODO: Need API for: PyResolveUtil.allowForwardReferences(node)
PyUnboundLocalVariableInspection::class.java
/** Instead use [com.jetbrains.snakecharm.lang.validation.SmkReturnAnnotator] **/
ReturnAnnotator::class.java,
// [HACK] See https://github.com/JetBrains-Research/snakecharm/issues/14
PyUnreachableCodeInspection::class.java,
// TODO: Need API for: PyResolveUtil.allowForwardReferences(node)
PyUnboundLocalVariableInspection::class.java,
// See https://github.com/JetBrains-Research/snakecharm/issues/133, API required
PyShadowingBuiltinsInspection::class.java

// other possible candidates to disable
// //inspections
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Feature: Fixes PyShadowingBuiltinsInspection related false positives
Issue #133

@ignore("Is disabled dut to a workaround for #133")
Scenario: PyShadowingBuiltinsInspection works in snakemake files
Given a snakemake project
Given I open a file "foo.smk" with text
"""
input = 1
"""
And PyShadowingBuiltinsInspection inspection is enabled
Then I expect inspection weak warning on <input> with message
"""
Shadows built-in name 'input'
"""
When I check highlighting weak warnings

Scenario Outline: Lambda params do not shadow builtin names
Given a snakemake project
Given I open a file "foo.smk" with text
"""
<rule_like> rule_133:
input: "in"
params:
methylomes2=lambda wildcards, input: "",
"""
And PyShadowingBuiltinsInspection inspection is enabled
Then I expect no inspection weak warnings
When I check highlighting weak warnings
Examples:
| rule_like |
| rule |
| checkpoint |
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Feature: Fixes PyShadowingNamesInspection related false positives
Issue #133

Scenario: PyShadowingNamesInspection works in snakemake files
Given a snakemake project
Given I open a file "foo.smk" with text
"""
resources = 1
wd = 1
rule rule_133:
input: "in"
output: "out.txt"
threads: 5
params:
methylomes3=lambda wd, resources: "",
"""
And PyShadowingNamesInspection inspection is enabled
# warning only for resources and wd because they == 1, not for input, output, threads
Then I expect inspection weak warning on <wd> in <wd,> with message
"""
Shadows name 'wd' from outer scope
"""
Then I expect inspection weak warning on <resources> in <resources:> with message
"""
Shadows name 'resources' from outer scope
"""

When I check highlighting weak warnings

Scenario Outline: Lambda params do not shadow section names
Given a snakemake project
Given I open a file "foo.smk" with text
"""
<rule_like> rule_133:
input: "in"
output: "out.txt"
threads: 5
params:
methylomes1=lambda wildcards, output: "",
methylomes2=lambda wildcards, input: "",
methylomes3=lambda wildcards, resources: "",
methylomes4=lambda wildcards, threads: ""
"""
And PyShadowingNamesInspection inspection is enabled
Then I expect no inspection weak warnings
When I check highlighting weak warnings
Examples:
| rule_like |
| rule |
| checkpoint |

0 comments on commit c74d94e

Please sign in to comment.