Skip to content

Commit 434eded

Browse files
committed
code review fixes
1 parent a840443 commit 434eded

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/client/linters/mypy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class MyPy extends BaseLinter {
1616
}
1717

1818
protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
19-
const fileName = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1);
20-
const regex = getRegex(fileName);
19+
const relativeFilePath = document.uri.fsPath.slice(this.getWorkspaceRootPath(document).length + 1);
20+
const regex = getRegex(relativeFilePath);
2121
const messages = await this.run([document.uri.fsPath], document, cancellation, regex);
2222
messages.forEach((msg) => {
2323
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.mypyCategorySeverity);

src/test/linters/mypy.unit.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,41 @@ suite('Linting - MyPy', () => {
6060
expect(msg).to.deep.equal(expected);
6161
}
6262
});
63+
test('regex excludes unexpected files', () => {
64+
// mypy run against `foo/bar.py` returning errors for foo/__init__.py
65+
const outputWithUnexpectedFile = `
66+
foo/__init__.py:4:5: error: Statement is unreachable [unreachable]
67+
foo/bar.py:2:14: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
68+
Found 2 errors in 2 files (checked 1 source file)
69+
`;
70+
71+
const lines = outputWithUnexpectedFile.split('\n');
72+
const tests: [string, ILintMessage | undefined][] = [
73+
[lines[0], undefined],
74+
[lines[1], undefined],
75+
[
76+
lines[2],
77+
{
78+
code: undefined,
79+
message:
80+
'Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]',
81+
column: 14,
82+
line: 2,
83+
type: 'error',
84+
provider: 'mypy',
85+
},
86+
],
87+
[lines[3], undefined],
88+
];
89+
for (const [line, expected] of tests) {
90+
const msg = parseLine(line, getRegex('foo/bar.py'), LinterId.MyPy);
91+
92+
expect(msg).to.deep.equal(expected);
93+
}
94+
});
95+
test('getRegex escapes filename correctly', () => {
96+
expect(getRegex('foo/bar.py')).to.eql(
97+
String.raw`foo/bar\.py:(?<line>\d+)(:(?<column>\d+))?: (?<type>\w+): (?<message>.*)\r?(\n|$)`,
98+
);
99+
});
63100
});

0 commit comments

Comments
 (0)