Skip to content

Commit 77f3612

Browse files
author
Mikhail Arkhipov
authored
Disable pylint print statement flag + fix venv detection (#954)
* Basic tokenizer * Fixed property names * Tests, round I * Tests, round II * tokenizer test * Remove temorary change * Fix merge issue * Merge conflict * Merge conflict * Completion test * Fix last line * Fix javascript math * Make test await for results * Add license headers * Rename definitions to types * License headers * Fix typo in completion details (typo) * Fix hover test * Russian translations * Update to better translation * Fix typo * #70 How to get all parameter info when filling in a function param list * Fix #70 How to get all parameter info when filling in a function param list * Clean up * Clean imports * CR feedback * Trim whitespace for test stability * More tests * Better handle no-parameters documentation * Better handle ellipsis and Python3 * #385 Auto-Indentation doesn't work after comment * #141 Auto indentation broken when return keyword involved * Undo changes * #627 Docstrings for builtin methods are not parsed correctly * reStructuredText converter * Fix: period is not an operator * Minor fixes * Restructure * Tests * Tests * Code heuristics * Baselines * HTML handling * Lists * State machine * Baselines * Squash * no message * Whitespace difference * Update Jedi to 0.11.1 * Enable Travis * Test fixes * Undo change * Jedi 0.11 with parser * Undo changes * Undo changes * Test fixes * More tests * Tests * Fix pylint search * Handle quote escapes in strings * Escapes in strings * CR feedback * Discover pylintrc better + tests * Fix .pyenv/versions search * Fix multiple linters output * Better handle markdown underscore * Test * Fix 916: PyLint checks wrong files * Test stability * Try increase timeout * Make sure linting is enabled in tests * Try another way of waiting * Simplify * Fix clear diags on close tests * Try writing settings directly * Increase timeout * Measure test time * Measure time * Simplify * Set timeout * Better venv detection * Add test * More reliable check * Fix pylint switch key * Remove incorrect flag * Disable print * Require pylint 1.8 on CI * Fix working directory for standalone files * Use an 'elif' * Separate file for pylint root config
1 parent 8c94314 commit 77f3612

File tree

7 files changed

+82
-9
lines changed

7 files changed

+82
-9
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
autopep8==1.2.1
22
yapf==0.6.2
3-
pylint==1.5.4
3+
pylint==1.8.2
44
pep8==1.7.0
55
prospector==0.11.7
66
flake8==2.6.0

src/client/interpreter/virtualEnvs/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export class VirtualEnvironmentManager implements IVirtualEnvironmentManager {
1414
}
1515
public async getEnvironmentName(pythonPath: string): Promise<string> {
1616
// https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
17-
const output = await this.processService.exec(pythonPath, ['-c', 'import sys;print(hasattr(sys, "real_prefix"))']);
17+
// hasattr(sys, 'real_prefix') works for virtualenv while
18+
// '(hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))' works for venv
19+
const code = 'import sys\nif hasattr(sys, "real_prefix"):\n print("virtualenv")\nelif hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix:\n print("venv")';
20+
const output = await this.processService.exec(pythonPath, ['-c', code]);
1821
if (output.stdout.length > 0) {
19-
const result = output.stdout.trim();
20-
if (result === 'True') {
21-
return 'virtualenv';
22-
}
22+
return output.stdout.trim();
2323
}
2424
return '';
2525
}

src/client/linters/baseLinter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export abstract class BaseLinter implements ILinter {
7070
protected getWorkspaceRootPath(document: vscode.TextDocument): string {
7171
const workspaceFolder = this.workspace.getWorkspaceFolder(document.uri);
7272
const workspaceRootPath = (workspaceFolder && typeof workspaceFolder.uri.fsPath === 'string') ? workspaceFolder.uri.fsPath : undefined;
73-
return typeof workspaceRootPath === 'string' ? workspaceRootPath : __dirname;
73+
return typeof workspaceRootPath === 'string' ? workspaceRootPath : path.dirname(document.uri.fsPath);
7474
}
7575
protected get logger(): ILogger {
7676
return this.serviceContainer.get<ILogger>(ILogger);

src/client/linters/pylint.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export class Pylint extends BaseLinter {
4141
&& !await Pylint.hasConfigurationFile(this.fileSystem, this.getWorkspaceRootPath(document), this.platformService)) {
4242
minArgs = [
4343
'--disable=all',
44-
'--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode'
44+
'--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode',
45+
'–-disable=print-statement'
4546
];
4647
}
4748
const args = [
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { expect } from 'chai';
5+
import { Container } from 'inversify';
6+
import * as TypeMoq from 'typemoq';
7+
import { BufferDecoder } from '../../client/common/process/decoder';
8+
import { ProcessService } from '../../client/common/process/proc';
9+
import { IBufferDecoder, IProcessService } from '../../client/common/process/types';
10+
import { VirtualEnvironmentManager } from '../../client/interpreter/virtualEnvs';
11+
import { ServiceContainer } from '../../client/ioc/container';
12+
import { ServiceManager } from '../../client/ioc/serviceManager';
13+
14+
suite('Virtual environment manager', () => {
15+
let serviceManager: ServiceManager;
16+
let serviceContainer: ServiceContainer;
17+
let process: TypeMoq.IMock<IProcessService>;
18+
19+
setup(async () => {
20+
const cont = new Container();
21+
serviceManager = new ServiceManager(cont);
22+
serviceContainer = new ServiceContainer(cont);
23+
});
24+
25+
test('Plain Python environment suffix', async () => await testSuffix(''));
26+
test('Venv environment suffix', async () => await testSuffix('venv'));
27+
test('Virtualenv Python environment suffix', async () => await testSuffix('virtualenv'));
28+
29+
test('Run actual virtual env detection code', async () => {
30+
serviceManager.addSingleton<IProcessService>(IProcessService, ProcessService);
31+
serviceManager.addSingleton<IBufferDecoder>(IBufferDecoder, BufferDecoder);
32+
const venvManager = new VirtualEnvironmentManager(serviceContainer);
33+
const name = await venvManager.getEnvironmentName('python');
34+
const result = name === '' || name === 'venv' || name === 'virtualenv';
35+
expect(result).to.be.equal(true, 'Running venv detection code failed.');
36+
});
37+
38+
async function testSuffix(expectedName: string) {
39+
process = TypeMoq.Mock.ofType<IProcessService>();
40+
serviceManager.addSingletonInstance<IProcessService>(IProcessService, process.object);
41+
42+
const venvManager = new VirtualEnvironmentManager(serviceContainer);
43+
process
44+
.setup(x => x.exec('python', TypeMoq.It.isAny()))
45+
.returns(() => Promise.resolve({
46+
stdout: expectedName,
47+
stderr: ''
48+
}));
49+
50+
const name = await venvManager.getEnvironmentName('python');
51+
expect(name).to.be.equal(expectedName, 'Virtual envrironment name suffix is incorrect.');
52+
}
53+
});

src/test/linters/lint.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ suite('Linting', () => {
231231
});
232232
test('PyLint with config in root', async () => {
233233
await fs.copy(path.join(pylintConfigPath, '.pylintrc'), path.join(workspaceUri.fsPath, '.pylintrc'));
234-
await testLinterMessages(Product.pylint, path.join(pylintConfigPath, 'file.py'), []);
234+
await testLinterMessages(Product.pylint, path.join(pylintConfigPath, 'file2.py'), []);
235235
});
236236
test('Flake8 with config in root', async () => {
237237
await testLinterMessages(Product.flake8, path.join(flake8ConfigPath, 'file.py'), filteredFlake8MessagesToBeReturned);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""pylint option block-disable"""
2+
3+
__revision__ = None
4+
5+
class Foo(object):
6+
"""block-disable test"""
7+
8+
def __init__(self):
9+
pass
10+
11+
def meth1(self, arg):
12+
"""meth1"""
13+
print self.blop
14+
15+
def meth2(self, arg):
16+
"""meth2"""
17+
# pylint: disable=unused-argument
18+
print self\
19+
+ "foo"

0 commit comments

Comments
 (0)