Skip to content

warning is emitted for unused arguments/variables prefixed with underscore #11387

@Rogach

Description

@Rogach

Describe the bug

A warning is emitted for unused arguments/variables, even though they are prefixed with an underscore.

Code or Screenshots

Here's an example:

def foo(_a):
    _b = 42
    return 3

Here, both _a and _b will have a warning on their lines, something like "_a" is not accessed.

Versions

pyright 1.1.407, 1.1.408 and latest git, running from Emacs with lsp-pyright.

Possible fix

The problem arises here:

if (nameNode) {
this._fileInfo.diagnosticSink.addUnusedCodeWithTextRange(
LocMessage.unaccessedSymbol().format({ name: nameNode.d.value }),
nameNode,
action
);
if (rule !== undefined && message && diagnosticLevel !== 'none') {
this._evaluator.addDiagnostic(rule, message, nameNode);
}
}

Looks like the diagnosticLevel !== 'none' check should be moved to the "if" one level back, like this:

--- a/packages/pyright-internal/src/analyzer/checker.ts
+++ b/packages/pyright-internal/src/analyzer/checker.ts
@@ -3859,14 +3859,14 @@ export class Checker extends ParseTreeWalker {
         }

         const action = rule === DiagnosticRule.reportUnusedImport ? { action: Commands.unusedImport } : undefined;
-        if (nameNode) {
+        if (nameNode && diagnosticLevel !== 'none') {
             this._fileInfo.diagnosticSink.addUnusedCodeWithTextRange(
                 LocMessage.unaccessedSymbol().format({ name: nameNode.d.value }),
                 nameNode,
                 action
             );

-            if (rule !== undefined && message && diagnosticLevel !== 'none') {
+            if (rule !== undefined && message) {
                 this._evaluator.addDiagnostic(rule, message, nameNode);
             }
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions