microsoft / microsoft/pyright

warning is emitted for unused arguments/variables prefixed with underscore

Open Beginner friendly
#11,387 0 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**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:

```python
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: https://github.com/microsoft/pyright/blob/28ea84a49e5943cf33e59616efb87057ef52397b/packages/pyright-internal/src/analyzer/checker.ts#L3862-L3872

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

```diff
--- 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);
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start in packages/pyright-internal/src/analyzer/checker.ts around lines 3862-3872 and reproduce the warning with the Python example in the issue. Confirm that underscore-prefixed unused arguments and variables no longer emit warnings while other unused-symbol diagnostics remain unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
devtools
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.