From c53a3575f54cc17f1fa381bef5a3d1feab8e8534 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 13 Sep 2024 10:50:08 +0200 Subject: [PATCH] Check types, add code coverage --- .github/workflows/test.yml | 13 ++++++++++--- README.md | 1 + pyproject.toml | 8 ++++++++ src/ypywidgets/__init__.py | 2 +- src/ypywidgets/comm.py | 2 +- src/ypywidgets/py.typed | 0 src/ypywidgets/widget.py | 8 ++++---- tests/conftest.py | 2 +- 8 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 src/ypywidgets/py.typed diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ced9aa4..91084a0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,9 +34,16 @@ jobs: run: python3 -m pip install --upgrade pip - name: Install ypywidgets in dev mode - run: | - pip install .[dev] + run: pip install .[dev] + + - name: Check types + run: mypy src - name: Run tests + run: pytest ./tests -v --color=yes + + - name: Run code coverage + if: ${{ (matrix.python-version == '3.12') && (matrix.os == 'ubuntu-latest') }} run: | - pytest ./tests -v --color=yes + coverage run -m pytest tests + coverage report --fail-under=100 diff --git a/README.md b/README.md index 21c09e0..c5dff32 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ [![Build Status](https://github.com/QuantStack/ypywidgets/workflows/test/badge.svg)](https://github.com/QuantStack/ypywidgets/actions) +[![Code Coverage](https://img.shields.io/badge/coverage-100%25-green)](https://img.shields.io/badge/coverage-100%25-green) # ypywidgets: Y-based Jupyter widgets for Python diff --git a/pyproject.toml b/pyproject.toml index 8920855..7866a42 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ dependencies = [ "comm >=0.1.4,<1", "pycrdt >=0.9.0,<0.10.0", "reacttrs >=0.2.1,<0.3.0", + "coverage >=7.0.0,<8.0.0", ] [project.urls] @@ -28,6 +29,7 @@ Homepage = "https://github.com/davidbrochart/ypywidgets" [project.optional-dependencies] dev = [ + "mypy", "pytest", "pytest-asyncio", ] @@ -35,3 +37,9 @@ dev = [ [tool.hatch.build.targets.wheel] ignore-vcs = true packages = ["src/ypywidgets"] + +[tool.coverage.run] +source = ["src/ypywidgets", "tests"] + +[tool.coverage.report] +show_missing = true diff --git a/src/ypywidgets/__init__.py b/src/ypywidgets/__init__.py index e2c4762..ca0425b 100644 --- a/src/ypywidgets/__init__.py +++ b/src/ypywidgets/__init__.py @@ -6,5 +6,5 @@ try: __version__ = importlib.metadata.version("ypywidgets") -except importlib.metadata.PackageNotFoundError: +except importlib.metadata.PackageNotFoundError: # pragma: no cover __version__ = "unknown" diff --git a/src/ypywidgets/comm.py b/src/ypywidgets/comm.py index 13901c8..e3e5b65 100644 --- a/src/ypywidgets/comm.py +++ b/src/ypywidgets/comm.py @@ -76,7 +76,7 @@ def __init__( self._comm = create_widget_comm(comm_data, comm_metadata, comm_id) CommProvider(self.ydoc, self._comm) - def _repr_mimebundle_(self, **kwargs): + def _repr_mimebundle_(self, **kwargs): # pragma: nocover plaintext = repr(self) if len(plaintext) > 110: plaintext = plaintext[:110] + '…' diff --git a/src/ypywidgets/py.typed b/src/ypywidgets/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/src/ypywidgets/widget.py b/src/ypywidgets/widget.py index 247d1e9..79157d1 100644 --- a/src/ypywidgets/widget.py +++ b/src/ypywidgets/widget.py @@ -1,6 +1,6 @@ from __future__ import annotations -from pycrdt import Doc, Map, Text +from pycrdt import Doc, MapEvent, Map, Text class Widget: @@ -9,17 +9,17 @@ class Widget: ydoc: Doc def __init__(self, ydoc: Doc | None = None) -> None: - if self._initialized: + if self._initialized: # pragma: nocover return self._initialized = True self.ydoc = Doc() if ydoc is None else ydoc self.ydoc["_attrs"] = self._attrs = Map() self.ydoc["_model_name"] = Text() self._attrs.observe(self._set_attr) - for k, v in self._attrs.items(): + for k, v in self._attrs.items(): # pragma: nocover setattr(self, k, v) - def _set_attr(self, event): + def _set_attr(self, event: MapEvent): for k, v in event.keys.items(): new_value = v["newValue"] if getattr(self, k) != new_value: diff --git a/tests/conftest.py b/tests/conftest.py index f1761d5..b7a0ea5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -96,5 +96,5 @@ async def get_widget(self, timeout=0.1): if self.widget: return self.widget await asyncio.sleep(0) - if time.monotonic() - t > timeout: + if time.monotonic() - t > timeout: # pragma: nocover raise TimeoutError("Timeout waiting for widget")