Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow Pylance to be used with Python 2 if explicitly chosen #16210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2021
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
1 change: 1 addition & 0 deletions news/2 Fixes/16204.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow Pylance to be used with Python 2 if "Pylance" is explicitly set as the language server.
17 changes: 13 additions & 4 deletions src/client/activation/activationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ export class LanguageServerExtensionActivationService
return configurationService.getSettings(this.resource).languageServer;
}

private getCurrentLanguageServerTypeIsDefault(): boolean {
const configurationService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
return configurationService.getSettings(this.resource).languageServerIsDefault;
}

private async createRefCountedServer(
resource: Resource,
interpreter: PythonEnvironment | undefined,
Expand Down Expand Up @@ -240,10 +245,14 @@ export class LanguageServerExtensionActivationService
}
}

if (serverType === LanguageServerType.Node && interpreter && interpreter.version) {
if (interpreter.version.major < 3) {
sendTelemetryEvent(EventName.JEDI_FALLBACK);
serverType = LanguageServerType.Jedi;
// If "Pylance" was explicitly chosen, use it even though it's not guaranteed to work
// properly with Python 2.
if (!this.getCurrentLanguageServerTypeIsDefault()) {
if (serverType === LanguageServerType.Node && interpreter && interpreter.version) {
if (interpreter.version.major < 3) {
sendTelemetryEvent(EventName.JEDI_FALLBACK);
serverType = LanguageServerType.Jedi;
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/client/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export class PythonSettings implements IPythonSettings {

public languageServer: LanguageServerType = LanguageServerType.Microsoft;

public languageServerIsDefault = true;

public logging: ILoggingSettings = { level: LogLevel.Error };

public useIsolation = true;
Expand Down Expand Up @@ -288,21 +290,19 @@ export class PythonSettings implements IPythonSettings {
let userLS = pythonSettings.get<string>('languageServer');
userLS = systemVariables.resolveAny(userLS);

let ls: LanguageServerType;

// Validate the user's input; if invalid, set it to the default.
if (
!userLS ||
userLS === 'Default' ||
!Object.values(LanguageServerType).includes(userLS as LanguageServerType)
) {
ls = this.defaultLS?.defaultLSType ?? LanguageServerType.Jedi;
this.languageServer = this.defaultLS?.defaultLSType ?? LanguageServerType.Jedi;
this.languageServerIsDefault = true;
} else {
ls = userLS as LanguageServerType;
this.languageServer = userLS as LanguageServerType;
this.languageServerIsDefault = false;
}

this.languageServer = ls;

this.jediPath = systemVariables.resolveAny(pythonSettings.get<string>('jediPath'))!;
if (typeof this.jediPath === 'string' && this.jediPath.length > 0) {
this.jediPath = getAbsolutePath(systemVariables.resolveAny(this.jediPath), workspaceRoot);
Expand Down
1 change: 1 addition & 0 deletions src/client/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export interface IPythonSettings {
readonly onDidChange: Event<void>;
readonly experiments: IExperiments;
readonly languageServer: LanguageServerType;
readonly languageServerIsDefault: boolean;
readonly defaultInterpreterPath: string;
readonly logging: ILoggingSettings;
readonly useIsolation: boolean;
Expand Down
11 changes: 8 additions & 3 deletions src/test/common/configSettings/configSettings.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ suite('Python Settings', async () => {
config.verifyAll();
});

function testLanguageServer(languageServer: LanguageServerType, expectedValue: LanguageServerType) {
function testLanguageServer(
languageServer: LanguageServerType,
expectedValue: LanguageServerType,
isDefault: boolean,
) {
test(languageServer, () => {
expected.pythonPath = 'python3';
expected.languageServer = languageServer;
Expand All @@ -200,16 +204,17 @@ suite('Python Settings', async () => {
settings.update(config.object);

expect(settings.languageServer).to.be.equal(expectedValue);
expect(settings.languageServerIsDefault).to.be.equal(isDefault);
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a basic test of the settings, but this settings test doesn't bother using IDefaultLanguageServer, which is dubious.

Copy link
Member

Choose a reason for hiding this comment

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

configSettings needs a clean up. It has lots of minor issues that keep causing problems. We will address these when we get to that clean up.

config.verifyAll();
});
}

suite('languageServer settings', async () => {
Object.values(LanguageServerType).forEach(async (languageServer) => {
testLanguageServer(languageServer, languageServer);
testLanguageServer(languageServer, languageServer, false);
});

testLanguageServer('invalid' as LanguageServerType, LanguageServerType.Jedi);
testLanguageServer('invalid' as LanguageServerType, LanguageServerType.Jedi, true);
});

function testExperiments(enabled: boolean) {
Expand Down