Skip to content

Commit

Permalink
Add mypy chack
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Nov 25, 2024
1 parent f8f4595 commit 49ad2fe
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ jobs:
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
runs-on: ubuntu-latest
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: 2 additions & 0 deletions python/jupyterlab-chat/jupyterlab_chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
warnings.warn("Importing 'jupyterlab_chat' outside a proper installation.")
__version__ = "dev"

from .ychat import YChat


def _jupyter_labextension_paths():
return [{
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
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

0 comments on commit 49ad2fe

Please sign in to comment.