From bd6600dba75330b1a529cb1beda61afb35ec262c Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sat, 16 Nov 2024 13:58:26 -0500 Subject: [PATCH 1/4] Use TypeVar's default feature with typing-extension>=4.4 --- noxfile.py | 2 +- .../pyright-compatibility-f78cafca4a95696d.yaml | 6 ++++++ rustworkx/__init__.pyi | 12 +++++++++--- rustworkx/rustworkx.pyi | 11 ++++++++--- tox.ini | 2 +- 5 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/pyright-compatibility-f78cafca4a95696d.yaml diff --git a/noxfile.py b/noxfile.py index d7d5229cb2..11f8292c97 100644 --- a/noxfile.py +++ b/noxfile.py @@ -19,7 +19,7 @@ stubs_deps = [ "mypy==1.11.2", - "typing-extensions", + "typing-extensions>=4.4", ] def install_rustworkx(session): diff --git a/releasenotes/notes/pyright-compatibility-f78cafca4a95696d.yaml b/releasenotes/notes/pyright-compatibility-f78cafca4a95696d.yaml new file mode 100644 index 0000000000..d6e4403d9b --- /dev/null +++ b/releasenotes/notes/pyright-compatibility-f78cafca4a95696d.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Enhanced the compatibility of the type annotations with `pyright` in strict + mode. See `issue 1242 `__ for + more details. \ No newline at end of file diff --git a/rustworkx/__init__.pyi b/rustworkx/__init__.pyi index 63f5f4be4b..1624af0386 100644 --- a/rustworkx/__init__.pyi +++ b/rustworkx/__init__.pyi @@ -9,11 +9,17 @@ # This file contains only type annotations for PyO3 functions and classes # For implementation details, see __init__.py and src/lib.rs +import sys import numpy as np -from typing import Generic, TypeVar, Any, Callable, overload +from typing import Generic, Any, Callable, overload from collections.abc import Iterator, Sequence +if sys.version_info >= (3, 13): + from typing import TypeVar +else: + from typing_extensions import TypeVar + # Re-Exports of rust native functions in rustworkx.rustworkx # To workaround limitations in mypy around re-exporting objects from the inner # rustworkx module we need to explicitly re-export every inner function from @@ -264,8 +270,8 @@ from .rustworkx import AllPairsMultiplePathMapping as AllPairsMultiplePathMappin from .rustworkx import PyGraph as PyGraph from .rustworkx import PyDiGraph as PyDiGraph -_S = TypeVar("_S") -_T = TypeVar("_T") +_S = TypeVar("_S", default=Any) +_T = TypeVar("_T", default=Any) _BFSVisitor = TypeVar("_BFSVisitor", bound=visit.BFSVisitor) _DFSVisitor = TypeVar("_DFSVisitor", bound=visit.DFSVisitor) _DijkstraVisitor = TypeVar("_DijkstraVisitor", bound=visit.DijkstraVisitor) diff --git a/rustworkx/rustworkx.pyi b/rustworkx/rustworkx.pyi index 6e690a2c2c..c9c8e24f59 100644 --- a/rustworkx/rustworkx.pyi +++ b/rustworkx/rustworkx.pyi @@ -11,7 +11,6 @@ from .visit import BFSVisitor, DFSVisitor, DijkstraVisitor from typing import ( - TypeVar, Callable, final, Any, @@ -35,9 +34,15 @@ from rustworkx import generators # noqa from typing_extensions import Self import numpy as np +import sys -_S = TypeVar("_S") -_T = TypeVar("_T") +if sys.version_info >= (3, 13): + from typing import TypeVar +else: + from typing_extensions import TypeVar + +_S = TypeVar("_S", default=Any) +_T = TypeVar("_T", default=Any) class DAGHasCycle(Exception): ... class DAGWouldCycle(Exception): ... diff --git a/tox.ini b/tox.ini index 712139c017..8cf7a04dc5 100644 --- a/tox.ini +++ b/tox.ini @@ -84,7 +84,7 @@ commands = basepython = python3 deps = mypy==1.8.0 - typing-extensions + typing-extensions>=4.4 extras = mpl graphviz From 17fa54a2781adbe2e4807e7d36d61330f85aec13 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sat, 16 Nov 2024 14:15:29 -0500 Subject: [PATCH 2/4] Fix _T_Co as well --- rustworkx/rustworkx.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustworkx/rustworkx.pyi b/rustworkx/rustworkx.pyi index c9c8e24f59..b264524a38 100644 --- a/rustworkx/rustworkx.pyi +++ b/rustworkx/rustworkx.pyi @@ -1039,7 +1039,7 @@ def graph_union( # Iterators -_T_co = TypeVar("_T_co", covariant=True) +_T_co = TypeVar("_T_co", covariant=True, default=Any) class _RustworkxCustomVecIter(Generic[_T_co], Sequence[_T_co], ABC): def __init__(self) -> None: ... From 7b1a20e54ab593d025dd9f25f39c0f19202e710d Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 17 Nov 2024 09:29:39 -0500 Subject: [PATCH 3/4] Remove tox-ini update because well it is already out of data for mypy --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 8cf7a04dc5..8c2e4c79e4 100644 --- a/tox.ini +++ b/tox.ini @@ -84,7 +84,7 @@ commands = basepython = python3 deps = mypy==1.8.0 - typing-extensions>=4.4 + typing-extension extras = mpl graphviz From b180c09e0b74f08b12fd9efaa9fa48ef233e6603 Mon Sep 17 00:00:00 2001 From: Ivan Carvalho Date: Sun, 17 Nov 2024 09:32:24 -0500 Subject: [PATCH 4/4] Fix typo in last fix --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 8c2e4c79e4..712139c017 100644 --- a/tox.ini +++ b/tox.ini @@ -84,7 +84,7 @@ commands = basepython = python3 deps = mypy==1.8.0 - typing-extension + typing-extensions extras = mpl graphviz