From ee4b9e7f96c220d393f93637051e3c5d87132db0 Mon Sep 17 00:00:00 2001 From: Oscar Benjamin Date: Wed, 6 Nov 2024 17:23:14 +0000 Subject: [PATCH 1/4] Use pyright for type-checking --- checks.sh | 1 + quicktest.sh | 1 + src/protosym/core/sym.py | 4 ++-- tests/core/test_sym.py | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/checks.sh b/checks.sh index 0f7ac0b..83bebc6 100755 --- a/checks.sh +++ b/checks.sh @@ -4,6 +4,7 @@ set -o errexit pre-commit run --all-files mypy --python-version=3.12 src tests +pyright --pythonversion 3.12 src tests sphinx-build -b html docs docs/_build python -m xdoctest --quiet protosym pytest --cov=protosym diff --git a/quicktest.sh b/quicktest.sh index e905aaa..e4e0a58 100755 --- a/quicktest.sh +++ b/quicktest.sh @@ -16,6 +16,7 @@ fi hatch run pre-commit:run hatch run types:mypy-check +hatch run types:pyright-check hatch run docs:build hatch run test:doctest hatch run test:coverage diff --git a/src/protosym/core/sym.py b/src/protosym/core/sym.py index eed2500..a8d6893 100644 --- a/src/protosym/core/sym.py +++ b/src/protosym/core/sym.py @@ -10,7 +10,7 @@ from __future__ import annotations -from typing import Any, Callable, Generic, Sequence, TypeVar, overload +from typing import Any, Callable, Generic, Self, Sequence, TypeVar, overload from weakref import WeakValueDictionary as _WeakDict from protosym.core.atom import AtomType @@ -130,7 +130,7 @@ class Sym: rep: Tree - def __new__(cls, tree_expr: Tree) -> Sym: + def __new__(cls, tree_expr: Tree) -> Self: """Create a new Sym wrapping `tree_expr`. If an equivalent Sym instance already exists then the same object will diff --git a/tests/core/test_sym.py b/tests/core/test_sym.py index 61aa35e..3ecdf0c 100644 --- a/tests/core/test_sym.py +++ b/tests/core/test_sym.py @@ -122,7 +122,7 @@ def test_Sym_evaluator() -> None: to_str[Integer[a]] = PyFunc1[int, str](str)(a) to_str[AtomRule[a]] = AtomFunc(str)(a) to_str[cos(a)] = PyOp1(lambda s: f"cos({s})")(a) - to_str[Add(star(a))] = PyOpN(" + ".join)(a) + to_str[Add(star(a))] = PyOpN(str(" + ").join)(a) to_str[HeadRule(a, b)] = HeadOp(lambda f, a: f"{f}({', '.join(a)})")(a, b) assert to_str(cos(one)) == "cos(1)" From e826d864bfd6ee07d2494d6ef246abf2f771601b Mon Sep 17 00:00:00 2001 From: Oscar Benjamin Date: Wed, 6 Nov 2024 17:25:40 +0000 Subject: [PATCH 2/4] Add pyright in CI --- requirements-all.txt | 1 + requirements-lint.txt | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-all.txt b/requirements-all.txt index be5c3cb..68361fb 100644 --- a/requirements-all.txt +++ b/requirements-all.txt @@ -2,3 +2,4 @@ -r requirements-test.txt -r requirements-docs.txt mypy +pyright diff --git a/requirements-lint.txt b/requirements-lint.txt index 4723573..520a0da 100644 --- a/requirements-lint.txt +++ b/requirements-lint.txt @@ -1,4 +1,3 @@ ruff -black pre-commit pre-commit-hooks From 595422d06faadf71a15cc4bf54dd1086baf701ce Mon Sep 17 00:00:00 2001 From: Oscar Benjamin Date: Wed, 6 Nov 2024 17:39:17 +0000 Subject: [PATCH 3/4] Add pyright:ignore for sympy call --- tests/test_simplecas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_simplecas.py b/tests/test_simplecas.py index 2b83009..afbe626 100644 --- a/tests/test_simplecas.py +++ b/tests/test_simplecas.py @@ -186,7 +186,7 @@ def test_simplecas_to_sympy() -> None: (cos(x), cosx_sym), (cos(x) ** 2 + sin(x) ** 2, cosx_sym**2 + sinx_sym**2), # pyright: ignore (cos(x) * sin(x), cosx_sym * sinx_sym), # pyright: ignore - (f(x), f_sym(x_sym)), + (f(x), f_sym(x_sym)), # pyright: ignore ] for expr, sympy_expr in test_cases: # XXX: Converting to SymPy and back does not in general round-trip From 3e3997025e69139ba6e8a402d02834736d7f9d0c Mon Sep 17 00:00:00 2001 From: Oscar Benjamin Date: Wed, 6 Nov 2024 17:57:06 +0000 Subject: [PATCH 4/4] Only import Self if type checking --- src/protosym/core/sym.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/protosym/core/sym.py b/src/protosym/core/sym.py index a8d6893..6253994 100644 --- a/src/protosym/core/sym.py +++ b/src/protosym/core/sym.py @@ -10,7 +10,12 @@ from __future__ import annotations -from typing import Any, Callable, Generic, Self, Sequence, TypeVar, overload +from typing import TYPE_CHECKING as _TYPE_CHECKING +from typing import Any, Callable, Generic, Sequence, TypeVar, overload + +if _TYPE_CHECKING: + from typing_extensions import Self + from weakref import WeakValueDictionary as _WeakDict from protosym.core.atom import AtomType