Skip to content
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

Strict nulls for everything except logconsole #7657

Merged
merged 16 commits into from Dec 23, 2019
9 changes: 5 additions & 4 deletions packages/cells/src/widget.ts
Expand Up @@ -42,7 +42,8 @@ import {
JSONValue,
PromiseDelegate,
JSONObject,
UUID
UUID,
PartialJSONValue
} from '@lumino/coreutils';

import { some, filter, toArray } from '@lumino/algorithm';
Expand Down Expand Up @@ -511,8 +512,8 @@ export class Cell extends Widget {
* Handle changes in the metadata.
*/
protected onMetadataChanged(
model: IObservableMap<JSONValue>,
args: IObservableMap.IChangedArgs<JSONValue>
model: IObservableJSON,
args: IObservableMap.IChangedArgs<PartialJSONValue | undefined>
): void {
switch (args.key) {
case 'jupyter':
Expand Down Expand Up @@ -965,7 +966,7 @@ export class CodeCell extends Cell {
* Handle changes in the metadata.
*/
protected onMetadataChanged(
model: IObservableMap<JSONValue>,
model: IObservableJSON,
args: IObservableMap.IChangedArgs<JSONValue>
): void {
if (this._savingMetadata) {
Expand Down
19 changes: 12 additions & 7 deletions packages/coreutils/src/statedb.ts
@@ -1,7 +1,7 @@
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import { ReadonlyJSONValue } from '@lumino/coreutils';
import { ReadonlyPartialJSONValue } from '@lumino/coreutils';

import { ISignal, Signal } from '@lumino/signaling';

Expand All @@ -12,8 +12,9 @@ import { IStateDB } from './tokens';
/**
* The default concrete implementation of a state database.
*/
export class StateDB<T extends ReadonlyJSONValue = ReadonlyJSONValue>
implements IStateDB<T> {
export class StateDB<
T extends ReadonlyPartialJSONValue = ReadonlyPartialJSONValue
> implements IStateDB<T> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Here I switch the StateDB to use partial types as well, as that seems to be needed / more accurate. On the other hand, it is harder to tighten up this types later if we incorrectly relax them now. Thoughts? CC @afshin

/**
* Create a new state database.
*
Expand Down Expand Up @@ -248,7 +249,9 @@ export namespace StateDB {
/**
* A data transformation that can be applied to a state database.
*/
export type DataTransform<T extends ReadonlyJSONValue = ReadonlyJSONValue> = {
export type DataTransform<
T extends ReadonlyPartialJSONValue = ReadonlyPartialJSONValue
> = {
/*
* The change operation being applied.
*/
Expand All @@ -263,12 +266,14 @@ export namespace StateDB {
/**
* Database content map
*/
export type Content<T> = { [id: string]: T };
export type Content<T> = { [id: string]: T | undefined };

/**
* The instantiation options for a state database.
*/
export interface IOptions<T extends ReadonlyJSONValue = ReadonlyJSONValue> {
export interface IOptions<
T extends ReadonlyPartialJSONValue = ReadonlyPartialJSONValue
> {
/**
* Optional string key/value connector. Defaults to in-memory connector.
*/
Expand Down Expand Up @@ -334,5 +339,5 @@ namespace Private {
/**
* An envelope around a JSON value stored in the state database.
*/
export type Envelope = { readonly v: ReadonlyJSONValue };
export type Envelope = { readonly v: ReadonlyPartialJSONValue };
}
14 changes: 7 additions & 7 deletions packages/coreutils/src/tokens.ts
Expand Up @@ -2,12 +2,11 @@
// Distributed under the terms of the Modified BSD License.

import {
JSONObject,
JSONValue,
ReadonlyJSONObject,
ReadonlyJSONValue,
PartialJSONObject,
Token
Token,
PartialJSONValue,
ReadonlyPartialJSONObject,
ReadonlyPartialJSONValue
} from '@lumino/coreutils';

import { IDisposable } from '@lumino/disposable';
Expand Down Expand Up @@ -470,8 +469,9 @@ export const IStateDB = new Token<IStateDB>('@jupyterlab/coreutils:IStateDB');
/**
* The description of a state database.
*/
export interface IStateDB<T extends ReadonlyJSONValue = ReadonlyJSONValue>
extends IDataConnector<T> {
export interface IStateDB<
T extends ReadonlyPartialJSONValue = ReadonlyPartialJSONValue
> extends IDataConnector<T> {
/**
* Return a serialized copy of the state database's entire contents.
*
Expand Down
50 changes: 32 additions & 18 deletions packages/notebook/src/notebooktools.ts
Expand Up @@ -3,7 +3,10 @@

import { ArrayExt, each, chain } from '@lumino/algorithm';

import { JSONObject, JSONValue } from '@lumino/coreutils';
import {
ReadonlyPartialJSONValue,
ReadonlyPartialJSONObject
} from '@lumino/coreutils';

import { ConflatableMessage, Message, MessageLoop } from '@lumino/messaging';

Expand Down Expand Up @@ -209,8 +212,8 @@ export class NotebookTools extends Widget implements INotebookTools {
* Handle a change in the active cell metadata.
*/
private _onActiveNotebookPanelMetadataChanged(
sender: IObservableMap<JSONValue>,
args: IObservableMap.IChangedArgs<JSONValue>
sender: IObservableMap<ReadonlyPartialJSONValue | undefined>,
args: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue>
): void {
let message = new ObservableJSON.ChangeMessage(
'activenotebookpanel-metadata-changed',
Expand All @@ -225,8 +228,8 @@ export class NotebookTools extends Widget implements INotebookTools {
* Handle a change in the notebook model metadata.
*/
private _onActiveCellMetadataChanged(
sender: IObservableMap<JSONValue>,
args: IObservableMap.IChangedArgs<JSONValue>
sender: IObservableMap<ReadonlyPartialJSONValue | undefined>,
args: IObservableMap.IChangedArgs<ReadonlyPartialJSONValue>
): void {
let message = new ObservableJSON.ChangeMessage(
'activecell-metadata-changed',
Expand Down Expand Up @@ -742,7 +745,10 @@ export namespace NotebookTools {
/**
* Set the value for the data.
*/
private _setValue = (cell: Cell, value: JSONValue) => {
private _setValue = (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => {
if (value === this._default) {
cell.model.metadata.delete(this.key);
} else {
Expand All @@ -752,9 +758,12 @@ export namespace NotebookTools {

private _changeGuard = false;
private _validCellTypes: string[];
private _getter: (cell: Cell) => JSONValue;
private _setter: (cell: Cell, value: JSONValue) => void;
private _default: JSONValue;
private _getter: (cell: Cell) => ReadonlyPartialJSONValue | undefined;
private _setter: (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => void;
private _default: ReadonlyPartialJSONValue | undefined;
}

/**
Expand All @@ -777,7 +786,7 @@ export namespace NotebookTools {
* If a value equals the default, choosing it may erase the key from the
* metadata.
*/
optionsMap: { [key: string]: JSONValue };
optionsMap: ReadonlyPartialJSONObject;

/**
* The optional title of the selector - defaults to capitalized `key`.
Expand All @@ -796,7 +805,7 @@ export namespace NotebookTools {
*
* @returns The appropriate value for the selector.
*/
getter?: (cell: Cell) => JSONValue;
getter?: (cell: Cell) => ReadonlyPartialJSONValue | undefined;

/**
* An optional value setter for the selector.
Expand All @@ -809,12 +818,15 @@ export namespace NotebookTools {
* The setter should set the appropriate metadata value given the value of
* the selector.
*/
setter?: (cell: Cell, value: JSONValue) => void;
setter?: (
cell: Cell,
value: ReadonlyPartialJSONValue | undefined
) => void;

/**
* Default value for default setters and getters if value is not found.
*/
default?: JSONValue;
default?: ReadonlyPartialJSONValue;
}
}

Expand All @@ -834,8 +846,10 @@ export namespace NotebookTools {
Notes: 'notes'
},
getter: cell => {
let value = cell.model.metadata.get('slideshow');
return value && (value as JSONObject)['slide_type'];
let value = cell.model.metadata.get('slideshow') as
| ReadonlyPartialJSONObject
| undefined;
return value && value['slide_type'];
},
setter: (cell, value) => {
let data = cell.model.metadata.get('slideshow') || Object.create(null);
Expand All @@ -859,9 +873,9 @@ export namespace NotebookTools {
/**
* Create an nbconvert selector.
*/
export function createNBConvertSelector(optionsMap: {
[key: string]: JSONValue;
}): KeySelector {
export function createNBConvertSelector(
optionsMap: ReadonlyPartialJSONObject
): KeySelector {
return new KeySelector({
key: 'raw_mimetype',
title: 'Raw NBConvert Format',
Expand Down