From e2a944c428f774d01d5d5a295056926ece296fef Mon Sep 17 00:00:00 2001 From: David Brochart Date: Thu, 12 Dec 2024 09:41:04 +0100 Subject: [PATCH] Add tests --- .github/workflows/test.yml | 2 +- pyproject.toml | 3 +++ python/pycrdt/_base.py | 1 + tests/test_types.py | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 tests/test_types.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a1e3418..ffef389 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -57,7 +57,7 @@ jobs: run: pip install -e ".[test]" - name: Check types - run: mypy python + run: mypy --check-untyped-defs python - name: Run tests if: ${{ !((matrix.python-version == '3.13') && (matrix.os == 'ubuntu')) }} diff --git a/pyproject.toml b/pyproject.toml index a6ffcbe..bed93b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ dependencies = [ [project.optional-dependencies] test = [ "pytest >=7.4.2,<8", + "pytest-mypy-testing", "anyio", "trio >=0.25.1,<0.27", "pydantic >=2.5.2,<3", @@ -59,11 +60,13 @@ python-source = "python" module-name = "pycrdt._pycrdt" [tool.ruff] +exclude = ["tests/test_types.py"] line-length = 100 lint.select = ["F", "E", "W", "I001"] [tool.coverage.run] source = ["python", "tests"] +omit = ["tests/test_types.py"] [tool.coverage.report] show_missing = true diff --git a/python/pycrdt/_base.py b/python/pycrdt/_base.py index 3e1c5b0..327c09f 100644 --- a/python/pycrdt/_base.py +++ b/python/pycrdt/_base.py @@ -222,6 +222,7 @@ def __init__(self, event: Any, doc: Doc): def __str__(self): str_list = [] + slot: Any for slot in self.__slots__: val = str(getattr(self, slot)) str_list.append(f"{slot}: {val}") diff --git a/tests/test_types.py b/tests/test_types.py new file mode 100644 index 0000000..a273043 --- /dev/null +++ b/tests/test_types.py @@ -0,0 +1,20 @@ +import pytest +from pycrdt import Array, Doc, Map + + +@pytest.mark.mypy_testing +def mypy_test_array(): + doc = Doc() + array0: Array[int] = doc.get("array0", type=Array) + array0.append(0) + array0.append("foo") # E: Argument 1 to "append" of "Array" has incompatible type "str"; expected "int" [arg-type] + + +@pytest.mark.mypy_testing +def mypy_test_map(): + doc = Doc() + map0: Map[bool] = doc.get("map0", type=Map) + map0["foo"] = True + map0["foo"] = "bar" # E: Incompatible types in assignment (expression has type "str", target has type "bool") + v0: str = map0.pop("foo") # E: Incompatible types in assignment (expression has type "bool", variable has type "str") + v1: bool = map0.pop("foo")