Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/2 Fixes/10190.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Prevent mypy errors for other files showing in current file.
(thanks [Steve Dignam](https://github.com/sbdchd))
9 changes: 7 additions & 2 deletions src/client/linters/mypy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { CancellationToken, OutputChannel, TextDocument } from 'vscode';
import '../common/extensions';
import { escapeRegExp } from 'lodash';
import { Product } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { BaseLinter } from './baseLinter';
import { ILintMessage } from './types';

export const REGEX = '(?<file>[^:]+):(?<line>\\d+)(:(?<column>\\d+))?: (?<type>\\w+): (?<message>.*)\\r?(\\n|$)';
export function getRegex(filepath: string): string {
return `${escapeRegExp(filepath)}:(?<line>\\d+)(:(?<column>\\d+))?: (?<type>\\w+): (?<message>.*)\\r?(\\n|$)`;
}
const COLUMN_OFF_SET = 1;

export class MyPy extends BaseLinter {
Expand All @@ -14,7 +17,9 @@ 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);
const relativeFilePath = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document.uri.fsPath gives us "/Users/foo/workspace-project/foo/bar.py" but we want the path relative to the workspace: foo/bar.py

const regex = getRegex(relativeFilePath);
const messages = await this.run([document.uri.fsPath], document, cancellation, regex);
messages.forEach((msg) => {
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.mypyCategorySeverity);
msg.code = msg.type;
Expand Down
40 changes: 38 additions & 2 deletions src/test/linters/mypy.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { expect } from 'chai';
import { parseLine } from '../../client/linters/baseLinter';
import { REGEX } from '../../client/linters/mypy';
import { getRegex } from '../../client/linters/mypy';
import { ILintMessage, LinterId } from '../../client/linters/types';

// This following is a real-world example. See gh=2380.
Expand Down Expand Up @@ -55,9 +55,45 @@ suite('Linting - MyPy', () => {
],
];
for (const [line, expected] of tests) {
const msg = parseLine(line, REGEX, LinterId.MyPy, 1);
const msg = parseLine(line, getRegex('provider.pyi'), LinterId.MyPy, 1);

expect(msg).to.deep.equal(expected);
}
});
test('regex excludes unexpected files', () => {
// mypy run against `foo/bar.py` returning errors for foo/__init__.py
const outputWithUnexpectedFile = `\
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)
`;

const lines = outputWithUnexpectedFile.split('\n');
const tests: [string, ILintMessage | undefined][] = [
[lines[0], undefined],
[
lines[1],
{
code: undefined,
message:
'Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]',
column: 13,
line: 2,
type: 'error',
provider: 'mypy',
},
],
[lines[2], undefined],
];
for (const [line, expected] of tests) {
const msg = parseLine(line, getRegex('foo/bar.py'), LinterId.MyPy, 1);

expect(msg).to.deep.equal(expected);
}
});
test('getRegex escapes filename correctly', () => {
expect(getRegex('foo/bar.py')).to.eql(
String.raw`foo/bar\.py:(?<line>\d+)(:(?<column>\d+))?: (?<type>\w+): (?<message>.*)\r?(\n|$)`,
);
});
});