Ignore mypy lint errors for other files#15266
Conversation
mypy sometimes returns errors for files that aren't the same as the file it was called against. This PR adds some filtering based on the current file name. fixes microsoft#10190
src/client/linters/mypy.ts
Outdated
| @@ -14,10 +15,19 @@ export class MyPy extends BaseLinter { | |||
|
|
|||
| protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> { | |||
| const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX); | |||
There was a problem hiding this comment.
Another option: generate the regex used to extract the message info at runtime with the current file name substituted instead of the file capture group, causing the messages for the other files to be ignored
src/client/linters/mypy.ts
Outdated
|
|
||
| protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> { | ||
| const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX); | ||
| const fileName = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1); |
There was a problem hiding this comment.
original approach was to modify the ILintMessage to include an optional file property and then filter based off that here
regex approach seems less invasive
There was a problem hiding this comment.
also wondering the best way to write a test for this
|
|
||
| protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> { | ||
| const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX); | ||
| const relativeFilePath = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1); |
There was a problem hiding this comment.
document.uri.fsPath gives us "/Users/foo/workspace-project/foo/bar.py" but we want the path relative to the workspace: foo/bar.py
``` ##########Linting Output - mypy########## foo/__init__.py:4:5: error: Statement is unreachable [unreachable] foo/bar.py:2:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] Found 2 errors in 2 files (checked 1 source file) ```
Codecov Report
@@ Coverage Diff @@
## main #15266 +/- ##
=======================================
Coverage 65% 65%
=======================================
Files 558 561 +3
Lines 26646 26982 +336
Branches 3840 3914 +74
=======================================
+ Hits 17322 17562 +240
- Misses 8617 8687 +70
- Partials 707 733 +26
|
|
Thanks for the changes, I'll have a look as soon as possible. FYI #15505 could affect this PR. |
karrtikr
left a comment
There was a problem hiding this comment.
LGTM once the offset is adjusted via rebase
src/test/linters/mypy.unit.test.ts
Outdated
| [lines[2], undefined], | ||
| ]; | ||
| for (const [line, expected] of tests) { | ||
| const msg = parseLine(line, getRegex('foo/bar.py'), LinterId.MyPy); |
|
@sbdchd The tests seem to be failing, can you please rebase again? The fixes may have landed on |
|
I think I fixed the tests, I'm having trouble getting the test suite to fail locally If I throw an exception or |
|
Interesting, we generally use |
mypy sometimes returns errors for files that aren't the same as the file
it was called against.
This PR adds some filtering based on the current file name.
Test file structure:
Before:
After:

fixes #10190
related #15216