Skip to content

Hide generic named default kernels from the kernel switcher #11763

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
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/11552.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Extra kernels that just say "Python 3 - python" are showing up in the raw kernel kernel picker.
10 changes: 10 additions & 0 deletions src/client/datascience/jupyter/kernels/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import type { Kernel } from '@jupyterlab/services';
import { IJupyterKernelSpec } from '../../types';
import { JupyterKernelSpec } from './jupyterKernelSpec';
// tslint:disable-next-line: no-var-requires no-require-imports
const NamedRegexp = require('named-js-regexp') as typeof import('named-js-regexp');

// Helper functions for dealing with kernels and kernelspecs

Expand All @@ -17,6 +19,8 @@ const connectionFilePlaceholder = '{connection_file}';
export function findIndexOfConnectionFile(kernelSpec: Readonly<IJupyterKernelSpec>): number {
return kernelSpec.argv.indexOf(connectionFilePlaceholder);
}

// Create a default kernelspec with the given display name
export function createDefaultKernelSpec(displayName?: string): IJupyterKernelSpec {
// This creates a default kernel spec. When launched, 'python' argument will map to using the interpreter
// associated with the current resource for launching.
Expand All @@ -32,3 +36,9 @@ export function createDefaultKernelSpec(displayName?: string): IJupyterKernelSpe

return new JupyterKernelSpec(defaultSpec);
}

// Check if a name is a default python kernel name and pull the version
export function detectDefaultKernelName(name: string) {
const regEx = NamedRegexp('python\\s*(?<version>(\\d+))', 'g');
return regEx.exec(name.toLowerCase());
}
16 changes: 14 additions & 2 deletions src/client/datascience/jupyter/kernels/kernelSelections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'use strict';

import { inject, injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken } from 'vscode';
import { PYTHON_LANGUAGE } from '../../../common/constants';
import { IFileSystem } from '../../../common/platform/types';
Expand All @@ -12,6 +13,7 @@ import * as localize from '../../../common/utils/localize';
import { IInterpreterSelector } from '../../../interpreter/configuration/types';
import { IKernelFinder } from '../../kernel-launcher/types';
import { IJupyterKernelSpec, IJupyterSessionManager } from '../../types';
import { detectDefaultKernelName } from './helpers';
import { KernelService } from './kernelService';
import { IKernelSelectionListProvider, IKernelSpecQuickPickItem, LiveKernelModel } from './types';

Expand Down Expand Up @@ -43,11 +45,11 @@ function getQuickPickItemForKernelSpec(
* @returns {IKernelSpecQuickPickItem}
*/
function getQuickPickItemForActiveKernel(kernel: LiveKernelModel, pathUtils: IPathUtils): IKernelSpecQuickPickItem {
const path = kernel.metadata?.interpreter?.path || kernel.path;
const pickPath = kernel.metadata?.interpreter?.path || kernel.path;
return {
label: kernel.display_name || kernel.name || '',
// If we have a session, use that path
detail: kernel.session.path || !path ? kernel.session.path : pathUtils.getDisplayName(path),
detail: kernel.session.path || !pickPath ? kernel.session.path : pathUtils.getDisplayName(pickPath),
description: localize.DataScience.jupyterSelectURIRunningDetailFormat().format(
kernel.lastActivityTime.toLocaleString(),
kernel.numberOfConnections.toString()
Expand Down Expand Up @@ -128,6 +130,16 @@ export class InstalledRawKernelSelectionListProvider implements IKernelSelection
const items = await this.kernelFinder.listKernelSpecs(resource);
return items
.filter((item) => (item.language || '').toLowerCase() === PYTHON_LANGUAGE.toLowerCase())
.filter((item) => {
// If we have a default kernel name and a non-absolute path just hide the item
// Otherwise we end up showing a bunch of "Python 3 - python" default items for
// other interpreters
const match = detectDefaultKernelName(item.name);
if (match) {
return path.isAbsolute(item.path);
}
return true;
})
.map((item) => getQuickPickItemForKernelSpec(item, this.pathUtils));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/datascience/jupyter/kernels/kernelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
IKernelDependencyService,
KernelInterpreterDependencyResponse
} from '../../types';
import { detectDefaultKernelName } from './helpers';
import { JupyterKernelSpec } from './jupyterKernelSpec';
import { LiveKernelModel } from './types';

Expand Down Expand Up @@ -202,8 +203,7 @@ export class KernelService {
}

// Check if kernel is `Python2` or `Python3` or a similar generic kernel.
const regEx = NamedRegexp('python\\s*(?<version>(\\d+))', 'g');
const match = regEx.exec(kernelSpec.name.toLowerCase());
const match = detectDefaultKernelName(kernelSpec.name);
if (match && match.groups()) {
// 3. Look for interpreter with same major version

Expand Down