Skip to content

Commit

Permalink
Typing (#106)
Browse files Browse the repository at this point in the history
* Move ui-tests directory to root

* Add mypy chack

* Fix workflow, licence and import
  • Loading branch information
brichet authored Nov 25, 2024
1 parent 1761ea8 commit 2961450
Show file tree
Hide file tree
Showing 42 changed files with 49 additions and 27 deletions.
29 changes: 24 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ jobs:
python -m pip install "jupyterlab>=4.0.0,<5" jupyterlab_chat*.whl
- name: Install dependencies
working-directory: python/jupyterlab-chat/ui-tests
working-directory: ui-tests
env:
YARN_ENABLE_IMMUTABLE_INSTALLS: 0
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
Expand All @@ -135,10 +135,10 @@ jobs:

- name: Install browser
run: jlpm playwright install chromium
working-directory: python/jupyterlab-chat/ui-tests
working-directory: ui-tests

- name: Execute integration tests
working-directory: python/jupyterlab-chat/ui-tests
working-directory: ui-tests
run: |
jlpm playwright test --retries=2
Expand All @@ -148,8 +148,27 @@ jobs:
with:
name: jupyterlab_chat-playwright-tests
path: |
python/jupyterlab-chat/ui-tests/test-results
python/jupyterlab-chat/ui-tests/playwright-report
ui-tests/test-results
ui-tests/playwright-report
typing-tests:
name: Typing test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Base Setup
uses: jupyterlab/maintainer-tools/.github/actions/base-setup@v1

- name: Install extension dependencies and build the extension
run: ./scripts/dev_install.sh

- name: Run mypy
run: |
set -eux
mypy --version
mypy python/jupyterlab-chat
check_links:
name: Check Links
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/update-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
# Playwright knows how to start JupyterLab server
start_server_script: 'null'
test_folder: python/jupyterlab-chat/ui-tests
test_folder: ui-tests
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ packages/jupyter-chat/junit.xml
packages/jupyterlab-chat/junit.xml

# Integration tests
python/jupyterlab-chat/ui-tests/test-results/
python/jupyterlab-chat/ui-tests/playwright-report/
ui-tests/test-results/
ui-tests/playwright-report/

# Created by https://www.gitignore.io/api/python
# Edit at https://www.gitignore.io/?templates=python
Expand Down
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ header:
Distributed under the terms of the Modified BSD License.
paths-ignore:
- '**/*.typed'
- '**/*.ipynb'
- '**/*.json'
- '**/*.md'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ jlpm test

### Integration tests

The integration tests are located in *python/jupyterlab-chat/ui-tests*.
The integration tests are located in *ui-tests*.

They make use of [playwright](https://playwright.dev/).

The following commands run them:

```bash
cd ./python/jupyterlab-chat/ui-tests
cd ./ui-tests

# Install the tests dependencies
jlpm install
Expand Down
3 changes: 1 addition & 2 deletions packages/jupyter-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@
"coverage",
"**/*.d.ts",
"tests",
"**/__tests__",
"ui-tests"
"**/__tests__"
],
"eslintConfig": {
"extends": [
Expand Down
3 changes: 1 addition & 2 deletions packages/jupyterlab-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@
"coverage",
"**/*.d.ts",
"tests",
"**/__tests__",
"ui-tests"
"**/__tests__"
],
"eslintConfig": {
"extends": [
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ before-bump-version = [

[tool.check-wheel-contents]
ignore = ["W002"]

[tool.mypy]
check_untyped_defs = true
2 changes: 1 addition & 1 deletion python/jupyterlab-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jlpm test
This extension uses [Playwright](https://playwright.dev/docs/intro) for the integration tests (aka user level tests).
More precisely, the JupyterLab helper [Galata](https://github.com/jupyterlab/jupyterlab/tree/master/galata) is used to handle testing the extension in JupyterLab.

More information are provided within the [ui-tests](./ui-tests/README.md) README.
More information are provided within the [ui-tests](../../ui-tests/README.md) README.

### Packaging the extension

Expand Down
Empty file.
21 changes: 11 additions & 10 deletions python/jupyterlab-chat/jupyterlab_chat/ychat.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_users(self) -> dict[str, dict[str, str]]:
Returns the users of the document.
:return: Document's users.
"""
return self._yusers.to_py()
return self._yusers.to_py() or {}

def set_user(self, user: dict[str, str]) -> None:
"""
Expand All @@ -95,7 +95,7 @@ def get_messages(self) -> list[dict]:
Returns the messages of the document.
:return: Document's messages.
"""
return self._ymessages.to_py()
return self._ymessages.to_py() or []

def add_message(self, message: dict) -> int:
"""
Expand All @@ -119,19 +119,20 @@ def update_message(self, message: dict, index: int, append: bool = False):
message["body"] = initial_message["body"] + message["body"]
self._ymessages.insert(index, message)

def set_message(self, message: dict, index: int | None = None, append: bool = False):
def set_message(self, message: dict, index: int | None = None, append: bool = False) -> int:
"""
Update or append a message.
"""

initial_message: dict | None = None
if index is not None and 0 <= index < len(self._ymessages):
initial_message = self._ymessages[index]
initial_message = self.get_messages()[index]
else:
return self.add_message(message)

if not initial_message["id"] == message["id"]:
initial_message, index = self.get_message(message["id"])
if initial_message is None:
if index is None:
return self.add_message(message)

self.update_message(message, index, append)
Expand All @@ -147,7 +148,7 @@ def get_metadata(self) -> dict[str, dict]:
"""
Returns the metadata of the document.
"""
return self._ymetadata.to_py()
return self._ymetadata.to_py() or {}

def set_metadata(self, name: str, metadata: dict):
"""
Expand All @@ -164,7 +165,7 @@ async def create_id(self) -> str:
self.set_id(id)
return id

def get_id(self) -> str:
def get_id(self) -> str | None:
"""
Returns the ID of the document.
"""
Expand Down Expand Up @@ -255,7 +256,7 @@ def _timestamp_new_messages(self, event: ArrayEvent) -> None:
index = 0
inserted_count = -1
deleted_count = -1
for value in event.delta:
for value in event.delta: # type:ignore[attr-defined]
if "retain" in value.keys():
index = value["retain"]
elif "insert" in value.keys():
Expand All @@ -268,7 +269,7 @@ def _timestamp_new_messages(self, event: ArrayEvent) -> None:
return

for idx in range(index, index + inserted_count):
message = self._ymessages[idx]
message = self.get_messages()[idx]
if message and message.get("raw_time", True):
self.create_task(self._set_timestamp(idx, timestamp))

Expand All @@ -279,7 +280,7 @@ async def _set_timestamp(self, msg_idx: int, timestamp: float):
with self._ydoc.transaction():
# Remove the message from the list and modify the timestamp
try:
message = self._ymessages[msg_idx]
message = self.get_messages()[msg_idx]
except IndexError:
return

Expand Down
3 changes: 1 addition & 2 deletions python/jupyterlab-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@
"dist",
"coverage",
"**/*.d.ts",
"tests",
"ui-tests"
"tests"
],
"eslintConfig": {
"extends": [
Expand Down
1 change: 1 addition & 0 deletions python/jupyterlab-chat/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dynamic = ["version", "description", "authors", "urls", "keywords"]
[project.optional-dependencies]
test = [
"coverage",
"mypy",
"pytest",
"pytest-asyncio",
"pytest-cov",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 2961450

Please sign in to comment.