Skip to content

Commit

Permalink
If type identifier for an environment is not registered, infer type f…
Browse files Browse the repository at this point in the history
…rom locator (microsoft/vscode-python#23083)

For microsoft/vscode-python#22810

...so in case of Hatch, as an [explicit Hatch identifier is not
available](microsoft/vscode-python#22779 (comment)),
we infer an environment is Hatch if we get it from the Hatch locator:
microsoft/vscode-python#22779.
  • Loading branch information
Kartik Raj authored and wesm committed Apr 8, 2024
1 parent f76905d commit 6b377d9
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions extensions/positron-python/src/client/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export type KnownEnvironmentTools =
| 'Venv'
| 'VirtualEnvWrapper'
| 'Pyenv'
| 'Hatch'
| 'Unknown';

/**
Expand Down
2 changes: 2 additions & 0 deletions extensions/positron-python/src/client/environmentApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ function convertKind(kind: PythonEnvKind): EnvironmentTools | undefined {
return 'Pipenv';
case PythonEnvKind.Poetry:
return 'Poetry';
case PythonEnvKind.Hatch:
return 'Hatch';
case PythonEnvKind.VirtualEnvWrapper:
return 'VirtualEnvWrapper';
case PythonEnvKind.VirtualEnv:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ function getPrioritizedEnvironmentType(): EnvironmentType[] {
EnvironmentType.Poetry,
EnvironmentType.Pipenv,
EnvironmentType.VirtualEnvWrapper,
EnvironmentType.Hatch,
EnvironmentType.Venv,
EnvironmentType.VirtualEnv,
EnvironmentType.ActiveState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export namespace EnvGroups {
export const Pyenv = 'Pyenv';
export const Venv = 'Venv';
export const Poetry = 'Poetry';
export const Hatch = 'Hatch';
export const VirtualEnvWrapper = 'VirtualEnvWrapper';
export const ActiveState = 'ActiveState';
export const Recommended = Common.recommended;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { cloneDeep } from 'lodash';
import { Event, EventEmitter } from 'vscode';
import { identifyEnvironment } from '../../../common/environmentIdentifier';
import { isIdentifierRegistered, identifyEnvironment } from '../../../common/environmentIdentifier';
import { IEnvironmentInfoService } from '../../info/environmentInfoService';
import { PythonEnvInfo, PythonEnvKind } from '../../info';
import { getEnvPath, setEnvDisplayString } from '../../info/env';
Expand Down Expand Up @@ -156,6 +156,10 @@ async function setKind(env: BasicEnvInfo, environmentKinds: Map<string, PythonEn
const { path } = getEnvPath(env.executablePath, env.envPath);
let kind = environmentKinds.get(path);
if (!kind) {
if (!isIdentifierRegistered(env.kind)) {
// If identifier is not registered, skip setting env kind.
return;
}
kind = await identifyEnvironment(path);
environmentKinds.set(path, kind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import {
import { isMicrosoftStoreEnvironment } from './environmentManagers/microsoftStoreEnv';
import { isActiveStateEnvironment } from './environmentManagers/activestate';

const notImplemented = () => Promise.resolve(false);

function getIdentifiers(): Map<PythonEnvKind, (path: string) => Promise<boolean>> {
const notImplemented = () => Promise.resolve(false);
const defaultTrue = () => Promise.resolve(true);
const identifier: Map<PythonEnvKind, (path: string) => Promise<boolean>> = new Map();
Object.values(PythonEnvKind).forEach((k) => {
Expand All @@ -39,6 +40,15 @@ function getIdentifiers(): Map<PythonEnvKind, (path: string) => Promise<boolean>
return identifier;
}

export function isIdentifierRegistered(kind: PythonEnvKind): boolean {
const identifiers = getIdentifiers();
const identifier = identifiers.get(kind);
if (identifier === notImplemented) {
return false;
}
return true;
}

/**
* Returns environment type.
* @param {string} path : Absolute path to the python interpreter binary or path to environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export enum EnvironmentType {
Venv = 'Venv',
MicrosoftStore = 'MicrosoftStore',
Poetry = 'Poetry',
Hatch = 'Hatch',
VirtualEnvWrapper = 'VirtualEnvWrapper',
ActiveState = 'ActiveState',
Global = 'Global',
Expand All @@ -28,6 +29,7 @@ export enum EnvironmentType {
export const virtualEnvTypes = [
EnvironmentType.Poetry,
EnvironmentType.Pipenv,
EnvironmentType.Hatch,
EnvironmentType.Venv,
EnvironmentType.VirtualEnvWrapper,
EnvironmentType.Conda,
Expand Down Expand Up @@ -115,6 +117,9 @@ export function getEnvironmentTypeName(environmentType: EnvironmentType): string
case EnvironmentType.Poetry: {
return 'Poetry';
}
case EnvironmentType.Hatch: {
return 'Hatch';
}
case EnvironmentType.VirtualEnvWrapper: {
return 'virtualenvwrapper';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const convertedKinds = new Map(
[PythonEnvKind.VirtualEnv]: EnvironmentType.VirtualEnv,
[PythonEnvKind.Pipenv]: EnvironmentType.Pipenv,
[PythonEnvKind.Poetry]: EnvironmentType.Poetry,
[PythonEnvKind.Hatch]: EnvironmentType.Hatch,
[PythonEnvKind.Venv]: EnvironmentType.Venv,
[PythonEnvKind.VirtualEnvWrapper]: EnvironmentType.VirtualEnvWrapper,
[PythonEnvKind.ActiveState]: EnvironmentType.ActiveState,
Expand Down

0 comments on commit 6b377d9

Please sign in to comment.