Skip to content

Commit dcffa32

Browse files
author
Kartik Raj
committed
Clarify using NonNullable
1 parent b9ad1f9 commit dcffa32

File tree

4 files changed

+72
-44
lines changed

4 files changed

+72
-44
lines changed

src/client/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { IExtensionApi } from './apiTypes';
4646
import { buildProposedApi } from './proposedApi';
4747
import { WorkspaceService } from './common/application/workspace';
4848
import { disposeAll } from './common/utils/resourceLifecycle';
49-
import { IProposedExtensionAPI } from './proposedApiTypes';
49+
import { ProposedExtensionAPI } from './proposedApiTypes';
5050

5151
durations.codeLoadingTime = stopWatch.elapsedTime;
5252

@@ -104,7 +104,7 @@ async function activateUnsafe(
104104
context: IExtensionContext,
105105
startupStopWatch: StopWatch,
106106
startupDurations: IStartupDurations,
107-
): Promise<[IExtensionApi & IProposedExtensionAPI, Promise<void>, IServiceContainer]> {
107+
): Promise<[IExtensionApi & ProposedExtensionAPI, Promise<void>, IServiceContainer]> {
108108
// Add anything that we got from initializing logs to dispose.
109109
context.subscriptions.push(...logDispose);
110110

src/client/proposedApi.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { Architecture } from './common/utils/platform';
88
import { IInterpreterService } from './interpreter/contracts';
99
import { IServiceContainer } from './ioc/types';
1010
import {
11-
ActiveEnvironmentChangedParams,
11+
ActiveEnvironmentChangeEvent,
1212
Environment,
13-
EnvironmentsChangedParams,
14-
IProposedExtensionAPI,
13+
EnvironmentsChangedEvent,
14+
ProposedExtensionAPI,
1515
ResolvedEnvironment,
1616
UniquePath,
1717
PythonVersionInfo,
@@ -24,12 +24,12 @@ import { PythonEnvInfo, PythonEnvKind, virtualEnvKinds } from './pythonEnvironme
2424
import { getEnvPath } from './pythonEnvironments/base/info/env';
2525
import { IDiscoveryAPI, ProgressReportStage } from './pythonEnvironments/base/locator';
2626

27-
const onDidActiveInterpreterChangedEvent = new EventEmitter<ActiveEnvironmentChangedParams>();
28-
export function reportActiveInterpreterChanged(e: ActiveEnvironmentChangedParams): void {
27+
const onDidActiveInterpreterChangedEvent = new EventEmitter<ActiveEnvironmentChangeEvent>();
28+
export function reportActiveInterpreterChanged(e: ActiveEnvironmentChangeEvent): void {
2929
onDidActiveInterpreterChangedEvent.fire(e);
3030
}
3131
const onProgress = new EventEmitter<RefreshState>();
32-
const onEnvironmentsChanged = new EventEmitter<EnvironmentsChangedParams>();
32+
const onEnvironmentsChanged = new EventEmitter<EnvironmentsChangedEvent>();
3333
const environmentsReference = new Map<UniquePath, EnvironmentReference>();
3434

3535
export class EnvironmentReference implements Environment {
@@ -70,7 +70,7 @@ function getEnvReference(e: Environment) {
7070
export function buildProposedApi(
7171
discoveryApi: IDiscoveryAPI,
7272
serviceContainer: IServiceContainer,
73-
): IProposedExtensionAPI {
73+
): ProposedExtensionAPI {
7474
const interpreterPathService = serviceContainer.get<IInterpreterPathService>(IInterpreterPathService);
7575
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
7676
const disposables = serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
@@ -97,7 +97,7 @@ export function buildProposedApi(
9797
onProgress,
9898
onEnvironmentsChanged,
9999
);
100-
const proposed: IProposedExtensionAPI = {
100+
const proposed: ProposedExtensionAPI = {
101101
environment: {
102102
activeEnvironment: {
103103
async fetch(resource?: Resource) {

src/client/proposedApiTypes.ts

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import { CancellationToken, Event, Uri, WorkspaceFolder } from 'vscode';
55

66
// https://github.com/microsoft/vscode-python/wiki/Proposed-Environment-APIs
77

8-
export interface IProposedExtensionAPI {
9-
environment: IEnvironmentAPI;
8+
export interface ProposedExtensionAPI {
9+
environment: EnvironmentAPI;
1010
}
1111

12-
interface IEnvironmentAPI {
12+
interface EnvironmentAPI {
1313
/**
1414
* Carries the API to track the selected environment by the user for a workspace.
1515
*/
16-
activeEnvironment: IActiveEnvironmentAPI;
16+
activeEnvironment: ActiveEnvironmentAPI;
1717
/**
1818
* Carries the API necessary for locating environments.
1919
*/
20-
locator: IEnvironmentLocatorAPI;
20+
locator: EnvironmentLocatorAPI;
2121
/**
2222
* Returns details for the given environment, or `undefined` if the env is invalid.
2323
* @param environment : Environment whose details you need. Can also pass the full path to environment folder
@@ -26,7 +26,7 @@ interface IEnvironmentAPI {
2626
resolveEnvironment(environment: Environment | UniquePath): Promise<ResolvedEnvironment | undefined>;
2727
}
2828

29-
interface IActiveEnvironmentAPI {
29+
interface ActiveEnvironmentAPI {
3030
/**
3131
* Returns the environment selected. Uses the cache by default, otherwise fetches full information about the
3232
* environment.
@@ -45,10 +45,10 @@ interface IActiveEnvironmentAPI {
4545
/**
4646
* This event is triggered when the active environment changes.
4747
*/
48-
onDidChange: Event<ActiveEnvironmentChangedParams>;
48+
onDidChange: Event<ActiveEnvironmentChangeEvent>;
4949
}
5050

51-
interface IEnvironmentLocatorAPI {
51+
interface EnvironmentLocatorAPI {
5252
/**
5353
* Carries environments found by the extension at the time of fetching the property. Note a refresh might be
5454
* going on so this may not be the complete list. To wait on complete list use {@link refreshState()} and
@@ -59,14 +59,14 @@ interface IEnvironmentLocatorAPI {
5959
* This event is triggered when the known environment list changes, like when a environment
6060
* is found, existing environment is removed, or some details changed on an environment.
6161
*/
62-
onDidChangeEnvironments: Event<EnvironmentsChangedParams>;
62+
onDidChangeEnvironments: Event<EnvironmentsChangedEvent>;
6363
/**
64-
* Returns the last known state in the refresh, i.e whether it started, finished, or any other relevant state.
64+
* Carries the current state in the refresh, i.e whether it started, finished, or any other relevant state.
6565
*/
6666
refreshState: RefreshState;
6767
/**
68-
* Tracks refresh progress for current list of known environments, i.e when it starts, finishes or any other
69-
* relevant state.
68+
* Fires when a refresh state has been reached, i.e when it starts, finishes or any other relevant state.
69+
* Tracks refresh progress for current list of known environments.
7070
*/
7171
readonly onDidChangeRefreshState: Event<RefreshState>;
7272
/**
@@ -115,7 +115,7 @@ export type Environment = {
115115
/**
116116
* Type of the environment.
117117
*/
118-
type: EnvType;
118+
type: EnvironmentType;
119119
/**
120120
* Name to the environment if any.
121121
*/
@@ -132,7 +132,7 @@ export type Environment = {
132132
* Tools/plugins which created the environment or where it came from. First value in array corresponds
133133
* to the primary source, which never changes over time.
134134
*/
135-
source: EnvSource[];
135+
source: EnvironmentSource[];
136136
}
137137
| undefined;
138138
/**
@@ -146,23 +146,43 @@ export type Environment = {
146146
};
147147
};
148148

149+
/**
150+
* A new form of object `T` where no property can have the value of `undefined`.
151+
*/
149152
type MakeAllPropertiesNonNullable<T> = {
150153
[P in keyof T]: NonNullable<T[P]>;
151154
};
152-
type MakeNonNullable<Type, Key extends keyof Type> = Omit<Type, Key> & MakeAllPropertiesNonNullable<Pick<Type, Key>>;
153-
type ExecutableInfo = MakeNonNullable<Environment['executable'], 'sysPrefix'> &
154-
MakeNonNullable<Environment['executable'], 'bitness'>;
155+
/**
156+
* A new form of object `Type` where a property represented by `Key` cannot be `undefined`.
157+
*/
158+
type MakePropertyNonNullable<Type, Key extends keyof Type> = Omit<Type, Key> &
159+
MakeAllPropertiesNonNullable<Pick<Type, Key>>;
155160

156-
type EnvironmentInfo = NonNullable<Pick<Environment, 'environment'>['environment']>;
161+
type ExecutableInfo = MakePropertyNonNullable<Environment['executable'], 'sysPrefix'> &
162+
MakePropertyNonNullable<Environment['executable'], 'bitness'>;
157163
export type PythonVersionInfo = MakeAllPropertiesNonNullable<Environment['version']>;
158164

159165
/**
160-
* Derived form of {@link Environment} with complete information, which means certain properties can no longer be `undefined`.
166+
* Derived form of {@link Environment} where certain properties can no longer be `undefined`. Meant to represent an
167+
* {@link Environment} with complete information.
161168
*/
162169
export interface ResolvedEnvironment {
170+
/**
171+
* See {@link UniquePath} for description.
172+
*/
163173
pathID: UniquePath;
174+
/**
175+
* New form of {@link Environment.executable} object where properties `sysPrefix` and `bitness` cannot be
176+
* `undefined`.
177+
*/
164178
executable: ExecutableInfo;
165-
environment: EnvironmentInfo | undefined;
179+
/**
180+
* See {@link Environment.environment} for description.
181+
*/
182+
environment: Environment['environment'];
183+
/**
184+
* New form of {@link Environment.version} object where no properties can be `undefined`.
185+
*/
166186
version: PythonVersionInfo;
167187
}
168188

@@ -171,7 +191,7 @@ export type RefreshState = {
171191
};
172192

173193
/**
174-
* Value of the enum indicates which states comes before when a refresh takes place.
194+
* Contains state values in the order they finish during a refresh cycle.
175195
*/
176196
export enum RefreshStateValue {
177197
/**
@@ -199,7 +219,7 @@ export type Resource = Uri | WorkspaceFolder;
199219
*/
200220
export type UniquePath = string;
201221

202-
export type EnvironmentsChangedParams = {
222+
export type EnvironmentsChangedEvent = {
203223
env: Environment;
204224
/**
205225
* * "add": New environment is added.
@@ -209,7 +229,7 @@ export type EnvironmentsChangedParams = {
209229
type: 'add' | 'remove' | 'update';
210230
};
211231

212-
export type ActiveEnvironmentChangedParams = {
232+
export type ActiveEnvironmentChangeEvent = {
213233
/**
214234
* See {@link UniquePath} for description.
215235
*/
@@ -231,28 +251,36 @@ export type RefreshOptions = {
231251
};
232252

233253
/**
234-
* Tool/plugin where the environment came from. It could be {@link KnownEnvSources} or custom string which was
235-
* contributed.
254+
* Tool/plugin where the environment came from. It can be {@link KnownEnvironmentSources} or custom string which
255+
* was contributed.
236256
*/
237-
export type EnvSource = KnownEnvSources | string;
257+
export type EnvironmentSource = KnownEnvironmentSources | string;
238258
/**
239259
* Tools or plugins the Python extension is aware of.
240260
*/
241-
export type KnownEnvSources = 'Conda' | 'Pipenv' | 'Poetry' | 'VirtualEnv' | 'Venv' | 'VirtualEnvWrapper' | 'Pyenv';
261+
export type KnownEnvironmentSources =
262+
| 'Conda'
263+
| 'Pipenv'
264+
| 'Poetry'
265+
| 'VirtualEnv'
266+
| 'Venv'
267+
| 'VirtualEnvWrapper'
268+
| 'Pyenv';
242269

243270
/**
244-
* Type of the environment. It could be {@link KnownEnvTypes} or custom string which was contributed.
271+
* Type of the environment. It can be {@link KnownEnvironmentTypes} or custom string which was contributed.
245272
*/
246-
export type EnvType = KnownEnvTypes | string;
273+
export type EnvironmentType = KnownEnvironmentTypes | string;
247274
/**
248275
* Environment types the Python extension is aware of.
249276
*/
250-
export type KnownEnvTypes = 'VirtualEnv' | 'Conda' | 'Unknown';
277+
export type KnownEnvironmentTypes = 'VirtualEnv' | 'Conda' | 'Unknown';
251278

252279
/**
253280
* Carries bitness for an environment.
254281
*/
255282
export type Architecture = 'x86' | 'x64' | 'Unknown';
283+
256284
/**
257285
* The possible Python release levels.
258286
*/

src/test/proposedApi.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import {
2121
import { PythonEnvironment } from '../client/pythonEnvironments/info';
2222
import { buildEnvInfo } from '../client/pythonEnvironments/base/info/env';
2323
import {
24-
ActiveEnvironmentChangedParams,
25-
IProposedExtensionAPI,
24+
ActiveEnvironmentChangeEvent,
25+
ProposedExtensionAPI,
2626
RefreshState,
2727
RefreshStateValue,
2828
} from '../client/proposedApiTypes';
@@ -36,7 +36,7 @@ suite('Proposed Extension API', () => {
3636
let onDidExecutionEvent: Event<Uri | undefined>;
3737
let onDidChangeRefreshState: EventEmitter<ProgressNotificationEvent>;
3838

39-
let proposed: IProposedExtensionAPI;
39+
let proposed: ProposedExtensionAPI;
4040

4141
setup(() => {
4242
serviceContainer = typemoq.Mock.ofType<IServiceContainer>();
@@ -73,7 +73,7 @@ suite('Proposed Extension API', () => {
7373
});
7474

7575
test('Provide an event to track when active environment details change', async () => {
76-
const events: ActiveEnvironmentChangedParams[] = [];
76+
const events: ActiveEnvironmentChangeEvent[] = [];
7777
proposed.environment.activeEnvironment.onDidChange((e) => {
7878
events.push(e);
7979
});

0 commit comments

Comments
 (0)