Skip to content

Commit 822e05e

Browse files
diningPhilosopher64prabhakk-mw
authored andcommitted
Python & js linting.Updated tests
1 parent 38c8472 commit 822e05e

File tree

7 files changed

+29
-55
lines changed

7 files changed

+29
-55
lines changed

src/jupyter_matlab_kernel/kernels/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .jsp_kernel import MATLABKernelUsingJSP
44
from .mpm_kernel import MATLABKernelUsingMPM
5-
from .kernel_factory import KernelFactory
5+
from .kernel_factory import KernelFactory

src/jupyter_matlab_kernel/kernels/jsp_kernel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def start_matlab_proxy(logger=_logger):
8989
pass
9090

9191
# Use parent process id of the kernel to filter Jupyter Server from the list.
92-
jupyter_server_pid = base._get_parent_pid(logger)
92+
jupyter_server_pid = base._get_parent_pid()
9393
logger.debug(f"Resolved jupyter server pid: {jupyter_server_pid}")
9494

9595
nb_server = dict()

src/jupyter_matlab_kernel/kernels/labextension_comm/communication.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2025 The MathWorks, Inc.
22

33
from ipykernel.comm import Comm
4+
import time
45

56

67
class LabExtensionCommunication:
@@ -27,7 +28,14 @@ def comm_open(self, stream, ident, msg):
2728

2829
async def comm_msg(self, stream, ident, msg):
2930
"""Handler to execute when labextension sends a message with 'comm_msg' type."""
30-
pass
31+
# As per jupyter messaging protocol https://jupyter-client.readthedocs.io/en/latest/messaging.html#custom-messages
32+
# 'content' will be present in msg, 'comm_id' and 'data' will be present in content.
33+
payload = msg["content"]["data"]
34+
action_type, action_data = payload["action"], payload["data"]
35+
36+
self.log.debug(
37+
f"Received action_type:{action_type} with data:{action_data} from the lab extension"
38+
)
3139

3240
def comm_close(self, stream, ident, msg):
3341
"""Handler to execute when labextension sends a message with 'comm_close' type."""

src/jupyter_matlab_kernel/mwi_comm_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ async def _send_jupyter_request_to_matlab(self, request_type, inputs, http_clien
388388
)
389389

390390
# The 'else' condition is an artifact and is present here incase we ever want to test
391-
# eval execution.
391+
# eval execution.
392392
else:
393393
user_mcode = inputs[2]
394394
# Construct a string which can be evaluated in MATLAB. For example

src/jupyter_matlab_labextension/src/plugins/matlabCommunication.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,22 @@ implements
128128

129129
// Handle comm close
130130
comm.onClose = (msg) => {
131-
console.debug('Comm closed:', msg);
131+
console.debug(`Received data:${msg} for comm close event.`);
132+
console.log(`Comm with ID:${comm.commId} closed.`);
132133
};
133134

134135
this._comms.set(panel.id, comm);
135-
136-
// Clean up when notebook is disposed
137-
panel.disposed.connect(() => {
138-
this._comms.delete(panel.id);
139-
});
140136
})
141137
.catch((error) => {
142138
console.error('Notebook panel was not ready', error);
143139
});
144140

145141
return new DisposableDelegate(() => {
146-
this._comms.get(panel.id)?.close();
147-
this._comms.delete(panel.id);
142+
const comm = this._comms.get(panel.id);
143+
if (comm && !comm.isDisposed) {
144+
comm.close();
145+
this._comms.delete(panel.id);
146+
}
148147
});
149148
}
150149

src/jupyter_matlab_labextension/src/utils/notebook.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ export class NotebookInfo {
9393
*/
9494
async update (panel: NotebookPanel | null): Promise<void> {
9595
if (panel) {
96-
this._panel = panel;
97-
this._isMatlabNotebook =
98-
panel.context.model.metadata.kernelspec?.language === 'matlab';
99-
const context = panel.context;
10096
// Wait for session context to be ready
10197
if (!panel.sessionContext.isReady) {
10298
await panel.sessionContext.ready;
10399
}
104-
100+
this._panel = panel;
101+
this._isMatlabNotebook = panel.sessionContext.kernelDisplayName === 'MATLAB Kernel';
102+
const context = panel.context;
105103
this._isBusy = panel.sessionContext.session?.kernel?.status === 'busy';
106104
this._notebookName = context.path;
107105
} else {

tests/unit/jupyter_matlab_kernel/test_labextension_communication.py

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_comm_open_creates_comm(
8686

8787
@pytest.mark.asyncio
8888
async def test_comm_msg_with_valid_comm(
89-
labext_comm, mocker, mock_stream, mock_ident, mock_comm
89+
labext_comm, mock_stream, mock_ident, mock_comm
9090
):
9191
"""Test that comm_msg processes messages when comm is available."""
9292
# Arrange
@@ -111,38 +111,7 @@ async def test_comm_msg_with_valid_comm(
111111
)
112112

113113

114-
@pytest.mark.asyncio
115-
async def test_comm_msg_without_comm_raises_exception(
116-
labext_comm, mocker, mock_stream, mock_ident
117-
):
118-
"""Test that comm_msg raises exception when no comm is available."""
119-
# Ensure comms is empty
120-
labext_comm.comms = {}
121-
comm_id = "test-comm-id"
122-
123-
# Prepare test data
124-
test_action = "test-action"
125-
test_data = {"key": "value"}
126-
msg = {
127-
"content": {
128-
"comm_id": comm_id,
129-
"data": {"action": test_action, "data": test_data},
130-
}
131-
}
132-
133-
# Call the method and expect exception
134-
with pytest.raises(Exception, match="No Communcation channel available"):
135-
await labext_comm.comm_msg(mock_stream, mock_ident, msg)
136-
137-
# Verify error logging
138-
labext_comm.log.error.assert_called_once_with(
139-
"Received comm_msg but no communication channel is available"
140-
)
141-
142-
143-
def test_comm_close_with_valid_comm_id(
144-
labext_comm, mocker, mock_stream, mock_ident, mock_comm
145-
):
114+
def test_comm_close_with_valid_comm_id(labext_comm, mock_stream, mock_ident, mock_comm):
146115
"""Test that comm_close closes the correct communication channel."""
147116
# Arrange
148117
# Set up a mock comm with matching ID
@@ -164,7 +133,7 @@ def test_comm_close_with_valid_comm_id(
164133

165134

166135
def test_comm_close_with_non_matching_comm_id(
167-
labext_comm, mocker, mock_stream, mock_ident, mock_comm
136+
labext_comm, mock_stream, mock_ident, mock_comm
168137
):
169138
"""Test that comm_close warns when trying to close unknown comm."""
170139
# Arrange
@@ -184,12 +153,12 @@ def test_comm_close_with_non_matching_comm_id(
184153
assert labext_comm.comms[different_comm_id] is mock_comm
185154

186155
# Verify warning logging
187-
labext_comm.log.warning.assert_called_once_with(
156+
labext_comm.log.debug.assert_called_once_with(
188157
f"Attempted to close unknown comm_id: {comm_id}"
189158
)
190159

191160

192-
def test_comm_close_with_no_comm(labext_comm, mocker, mock_stream, mock_ident):
161+
def test_comm_close_with_no_comm(labext_comm, mock_stream, mock_ident):
193162
"""Test that comm_close warns when no comm exists."""
194163
# Arrange
195164
# Ensure comms is empty
@@ -205,14 +174,14 @@ def test_comm_close_with_no_comm(labext_comm, mocker, mock_stream, mock_ident):
205174
assert labext_comm.comms == {}
206175

207176
# Verify warning logging
208-
labext_comm.log.warning.assert_called_once_with(
177+
labext_comm.log.debug.assert_called_once_with(
209178
f"Attempted to close unknown comm_id: {test_comm_id}"
210179
)
211180

212181

213182
@pytest.mark.asyncio
214183
async def test_comm_msg_extracts_data_correctly(
215-
labext_comm, mocker, mock_stream, mock_ident, mock_comm
184+
labext_comm, mock_stream, mock_ident, mock_comm
216185
):
217186
"""Test that comm_msg correctly extracts action and data from message."""
218187
# Arrange

0 commit comments

Comments
 (0)