Skip to content

Commit 97c139f

Browse files
committed
Revert calling mypy with relatives paths (regression) (microsoft#9502)
* Revert "fix(client/linters/mypy): call mypy incorrectly (microsoft#5834)" This reverts commit 1b6fbfb. * News file * Revert "Fix MyPy CI tests (microsoft#8518)" This reverts commit c634ffd.
1 parent c111e98 commit 97c139f

File tree

4 files changed

+39
-110
lines changed

4 files changed

+39
-110
lines changed

news/2 Fixes/9496.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Revert changes related to calling `mypy` with relative paths.

src/client/linters/mypy.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as path from 'path';
21
import { CancellationToken, OutputChannel, TextDocument } from 'vscode';
32
import '../common/extensions';
43
import { Product } from '../common/types';
@@ -14,9 +13,7 @@ export class MyPy extends BaseLinter {
1413
}
1514

1615
protected async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
17-
const cwd = this.getWorkspaceRootPath(document);
18-
const relativePath = path.relative(cwd, document.uri.fsPath);
19-
const messages = await this.run([relativePath], document, cancellation, REGEX);
16+
const messages = await this.run([document.uri.fsPath], document, cancellation, REGEX);
2017
messages.forEach(msg => {
2118
msg.severity = this.parseMessagesSeverity(msg.type, this.pythonSettings.linting.mypyCategorySeverity);
2219
msg.code = msg.type;

src/test/linters/lint.args.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ suite('Linting - Arguments', () => {
137137
});
138138
test('MyPy', async () => {
139139
const linter = new MyPy(outputChannel.object, serviceContainer);
140-
const expectedPath = workspaceUri ? path.join(path.basename(path.dirname(fileUri.fsPath)), path.basename(fileUri.fsPath)) : path.basename(fileUri.fsPath);
141-
const expectedArgs = [expectedPath];
140+
const expectedArgs = [fileUri.fsPath];
142141
await testLinter(linter, expectedArgs);
143142
});
144143
test('Pydocstyle', async () => {

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

Lines changed: 36 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@
66
// tslint:disable:no-object-literal-type-assertion
77

88
import { expect } from 'chai';
9-
import * as path from 'path';
10-
import * as sinon from 'sinon';
11-
import { anything, instance, mock, when } from 'ts-mockito';
12-
import { CancellationToken, CancellationTokenSource, TextDocument, Uri } from 'vscode';
13-
import { Product } from '../../client/common/types';
14-
import { ServiceContainer } from '../../client/ioc/container';
159
import { parseLine } from '../../client/linters/baseLinter';
16-
import { LinterManager } from '../../client/linters/linterManager';
17-
import { MyPy, REGEX } from '../../client/linters/mypy';
18-
import { ILinterManager, ILintMessage, LintMessageSeverity } from '../../client/linters/types';
19-
import { MockOutputChannel } from '../mockClasses';
10+
import { REGEX } from '../../client/linters/mypy';
11+
import { ILintMessage } from '../../client/linters/types';
2012

2113
// This following is a real-world example. See gh=2380.
22-
// tslint:disable:no-multiline-string no-any max-func-body-length
14+
// tslint:disable-next-line:no-multiline-string
2315
const output = `
2416
provider.pyi:10: error: Incompatible types in assignment (expression has type "str", variable has type "int")
2517
provider.pyi:11: error: Name 'not_declared_var' is not defined
@@ -30,30 +22,39 @@ suite('Linting - MyPy', () => {
3022
test('regex', async () => {
3123
const lines = output.split('\n');
3224
const tests: [string, ILintMessage][] = [
33-
[lines[1], {
34-
code: undefined,
35-
message: 'Incompatible types in assignment (expression has type "str", variable has type "int")',
36-
column: 0,
37-
line: 10,
38-
type: 'error',
39-
provider: 'mypy'
40-
} as ILintMessage],
41-
[lines[2], {
42-
code: undefined,
43-
message: 'Name \'not_declared_var\' is not defined',
44-
column: 0,
45-
line: 11,
46-
type: 'error',
47-
provider: 'mypy'
48-
} as ILintMessage],
49-
[lines[3], {
50-
code: undefined,
51-
message: 'Expression has type "Any"',
52-
column: 21,
53-
line: 12,
54-
type: 'error',
55-
provider: 'mypy'
56-
} as ILintMessage]
25+
[
26+
lines[1],
27+
{
28+
code: undefined,
29+
message: 'Incompatible types in assignment (expression has type "str", variable has type "int")',
30+
column: 0,
31+
line: 10,
32+
type: 'error',
33+
provider: 'mypy'
34+
} as ILintMessage
35+
],
36+
[
37+
lines[2],
38+
{
39+
code: undefined,
40+
message: "Name 'not_declared_var' is not defined",
41+
column: 0,
42+
line: 11,
43+
type: 'error',
44+
provider: 'mypy'
45+
} as ILintMessage
46+
],
47+
[
48+
lines[3],
49+
{
50+
code: undefined,
51+
message: 'Expression has type "Any"',
52+
column: 21,
53+
line: 12,
54+
type: 'error',
55+
provider: 'mypy'
56+
} as ILintMessage
57+
]
5758
];
5859
for (const [line, expected] of tests) {
5960
const msg = parseLine(line, REGEX, 'mypy');
@@ -62,72 +63,3 @@ suite('Linting - MyPy', () => {
6263
}
6364
});
6465
});
65-
66-
suite('Test Linter', () => {
67-
class TestMyPyLinter extends MyPy {
68-
// tslint:disable: no-unnecessary-override
69-
public async runLinter(document: TextDocument, cancellation: CancellationToken): Promise<ILintMessage[]> {
70-
return super.runLinter(document, cancellation);
71-
}
72-
public getWorkspaceRootPath(document: TextDocument): string {
73-
return super.getWorkspaceRootPath(document);
74-
}
75-
public async run(args: string[], document: TextDocument, cancellation: CancellationToken, regEx: string = REGEX): Promise<ILintMessage[]> {
76-
return super.run(args, document, cancellation, regEx);
77-
}
78-
public parseMessagesSeverity(error: string, severity: any): LintMessageSeverity {
79-
return super.parseMessagesSeverity(error, severity);
80-
}
81-
}
82-
83-
let linter: TestMyPyLinter;
84-
let getWorkspaceRootPathStub: sinon.SinonStub<[TextDocument], string>;
85-
let runStub: sinon.SinonStub<[string[], TextDocument, CancellationToken, (string | undefined)?], Promise<ILintMessage[]>>;
86-
const token = new CancellationTokenSource().token;
87-
teardown(() => sinon.restore());
88-
setup(() => {
89-
const linterManager = mock(LinterManager);
90-
when(linterManager.getLinterInfo(anything())).thenReturn({ product: Product.mypy } as any);
91-
const serviceContainer = mock(ServiceContainer);
92-
when(serviceContainer.get<ILinterManager>(ILinterManager)).thenReturn(instance(linterManager));
93-
getWorkspaceRootPathStub = sinon.stub(TestMyPyLinter.prototype, 'getWorkspaceRootPath');
94-
runStub = sinon.stub(TestMyPyLinter.prototype, 'run');
95-
linter = new TestMyPyLinter(instance(mock(MockOutputChannel)), instance(serviceContainer));
96-
});
97-
98-
test('Get cwd based on document', async () => {
99-
const fileUri = Uri.file(path.join('a', 'b', 'c', 'd', 'e', 'filename.py'));
100-
const cwd = path.join('a', 'b', 'c');
101-
const doc = { uri: fileUri } as any as TextDocument;
102-
getWorkspaceRootPathStub.callsFake(() => cwd);
103-
runStub.callsFake(() => Promise.resolve([]));
104-
105-
await linter.runLinter(doc, token);
106-
107-
expect(getWorkspaceRootPathStub.callCount).to.equal(1);
108-
expect(getWorkspaceRootPathStub.args[0]).to.deep.equal([doc]);
109-
});
110-
test('Pass relative path of document to linter', async () => {
111-
const fileUri = Uri.file(path.join('a', 'b', 'c', 'd', 'e', 'filename.py'));
112-
const cwd = path.join('a', 'b', 'c');
113-
const doc = { uri: fileUri } as any as TextDocument;
114-
getWorkspaceRootPathStub.callsFake(() => cwd);
115-
runStub.callsFake(() => Promise.resolve([]));
116-
117-
await linter.runLinter(doc, token);
118-
119-
expect(runStub.callCount).to.equal(1);
120-
expect(runStub.args[0]).to.deep.equal([[path.relative(cwd, fileUri.fsPath)], doc, token, REGEX]);
121-
});
122-
test('Return empty messages', async () => {
123-
const fileUri = Uri.file(path.join('a', 'b', 'c', 'd', 'e', 'filename.py'));
124-
const cwd = path.join('a', 'b', 'c');
125-
const doc = { uri: fileUri } as any as TextDocument;
126-
getWorkspaceRootPathStub.callsFake(() => cwd);
127-
runStub.callsFake(() => Promise.resolve([]));
128-
129-
const messages = await linter.runLinter(doc, token);
130-
131-
expect(messages).to.be.deep.equal([]);
132-
});
133-
});

0 commit comments

Comments
 (0)