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

Remove unnecessary nullables #9766

Merged
merged 4 commits into from
Apr 25, 2022
Merged

Remove unnecessary nullables #9766

merged 4 commits into from
Apr 25, 2022

Conversation

DonJayamanne
Copy link
Contributor

@DonJayamanne DonJayamanne commented Apr 25, 2022

Part of #9167

Basically we have a lot of unnecessary nullables, thereby simplifying the code
This PR finally fixes this comment

image

@@ -182,6 +182,7 @@ export class JupyterExecutionBase implements IJupyterExecution {
}
throw lastTryError;
}
throw new Error('Max number of attempts reached');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function was the root cause, we have an explicit return undefined and an implicit at the end of the while loop.
That causes return value to have undefined and bubbles all the way the to the top.
This is an unlikely scenario (cancellation is handled higher up, hence we don't need to swallow cancellations and disposing classes here)

if (!this.connection) {
return undefined;
throw new Error('Not connected');
Copy link
Contributor Author

@DonJayamanne DonJayamanne Apr 25, 2022

Choose a reason for hiding this comment

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

THis is impossible, when the class is contructed, the first things that called is the connect method that initializes the property this.connection. Ideally we shoudl remove such code that initializes the class and then calls methods.
I've found this in a number of places and replaced them with factories, after that we dont end up with nullables (removing the nullabels makes the code simpler, e.g. in this case, IJupyterConnection can never be null in the any code path, but due to the way the class is written we don't know hence end up returning undefined and have to handle this case everywhere its used)

Copy link
Contributor Author

@DonJayamanne DonJayamanne Apr 25, 2022

Choose a reason for hiding this comment

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

This class is now instantiated via a factory, hence connection is never nullable and the method getConnectionInfo is now a property

@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry
@inject(IDisposableRegistry) private readonly disposables: IDisposableRegistry,
public connection: IJupyterConnection,
private sessionManager: JupyterSessionManager
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The connection property is accessed using getConnection and we have some code that waits for this to be initialized.
Basically as soon as this class is contructed, the first method invoked is connect and its impossible for connection to be undefined, however due to the way the class was constructed it can be undefined in types & we have code all over the place that treats it as such.

Refactored this class to be created via a factory so that it can never be nullable, as a result the code paths are much simpler now.
No more nullables.

}
} catch (e) {
traceError(`Error during shutdown: `, e);
}
}

private waitForConnect(): Promise<IJupyterConnection | undefined> {
return this.connectPromise.promise;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Funky code now a thing of the past due to nullables being removed.

@DonJayamanne DonJayamanne marked this pull request as ready for review April 25, 2022 19:45
@DonJayamanne DonJayamanne requested a review from a team as a code owner April 25, 2022 19:45
@@ -34,27 +34,35 @@ import { JupyterNotebook } from '../jupyterNotebook';
import { noop } from '../../../../platform/common/utils/misc';
import { Cancellation } from '../../../../platform/common/cancellation';
import { getDisplayPath } from '../../../../platform/common/platform/fs-paths';
import { INotebookServer, IJupyterSessionManagerFactory } from '../../types';
import { INotebookServer } from '../../types';
import { Uri } from 'vscode';
/* eslint-disable @typescript-eslint/no-explicit-any */

@injectable()
Copy link
Contributor

Choose a reason for hiding this comment

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

You should probably remove this now then. Otherwise I think the tests will fail saying it's not in the container.

@codecov-commenter
Copy link

Codecov Report

Merging #9766 (fdf0743) into main (2b726d6) will increase coverage by 0%.
The diff coverage is n/a.

❗ Current head fdf0743 differs from pull request most recent head 1006723. Consider uploading reports for the commit 1006723 to get more accurate results

@@         Coverage Diff          @@
##           main   #9766   +/-   ##
====================================
  Coverage    63%     63%           
====================================
  Files       203     203           
  Lines      9816    9816           
  Branches   1556    1556           
====================================
+ Hits       6215    6221    +6     
+ Misses     3104    3096    -8     
- Partials    497     499    +2     
Impacted Files Coverage Δ
...rc/platform/common/dataScienceSurveyBanner.node.ts 70% <0%> (+5%) ⬆️

@@ -152,7 +151,7 @@ export class JupyterExecutionBase implements IJupyterExecution {
} else if (connection) {
// If this is occurring during shutdown, don't worry about it.
Copy link
Contributor

@amunger amunger Apr 25, 2022

Choose a reason for hiding this comment

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

update comment? (Or just remove)

@DonJayamanne DonJayamanne merged commit fea8190 into main Apr 25, 2022
@DonJayamanne DonJayamanne deleted the part1Of9167 branch April 25, 2022 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants