Skip to content

Commit 05e1d29

Browse files
authored
Improve pyright compatibility by using TypeVar's default argument (#1246)
* Use TypeVar's default feature with typing-extension>=4.4 * Fix _T_Co as well * Remove tox-ini update because well it is already out of data for mypy * Fix typo in last fix
1 parent 9969f0e commit 05e1d29

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

noxfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
stubs_deps = [
2222
"mypy==1.11.2",
23-
"typing-extensions",
23+
"typing-extensions>=4.4",
2424
]
2525

2626
def install_rustworkx(session):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Enhanced the compatibility of the type annotations with `pyright` in strict
5+
mode. See `issue 1242 <https://github.com/Qiskit/rustworkx/issues/1242>`__ for
6+
more details.

rustworkx/__init__.pyi

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99
# This file contains only type annotations for PyO3 functions and classes
1010
# For implementation details, see __init__.py and src/lib.rs
1111

12+
import sys
1213
import numpy as np
1314

14-
from typing import Generic, TypeVar, Any, Callable, overload
15+
from typing import Generic, Any, Callable, overload
1516
from collections.abc import Iterator, Sequence
1617

18+
if sys.version_info >= (3, 13):
19+
from typing import TypeVar
20+
else:
21+
from typing_extensions import TypeVar
22+
1723
# Re-Exports of rust native functions in rustworkx.rustworkx
1824
# To workaround limitations in mypy around re-exporting objects from the inner
1925
# rustworkx module we need to explicitly re-export every inner function from
@@ -270,8 +276,8 @@ from .rustworkx import AllPairsMultiplePathMapping as AllPairsMultiplePathMappin
270276
from .rustworkx import PyGraph as PyGraph
271277
from .rustworkx import PyDiGraph as PyDiGraph
272278

273-
_S = TypeVar("_S")
274-
_T = TypeVar("_T")
279+
_S = TypeVar("_S", default=Any)
280+
_T = TypeVar("_T", default=Any)
275281
_BFSVisitor = TypeVar("_BFSVisitor", bound=visit.BFSVisitor)
276282
_DFSVisitor = TypeVar("_DFSVisitor", bound=visit.DFSVisitor)
277283
_DijkstraVisitor = TypeVar("_DijkstraVisitor", bound=visit.DijkstraVisitor)

rustworkx/rustworkx.pyi

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from .visit import BFSVisitor, DFSVisitor, DijkstraVisitor
1313
from typing import (
14-
TypeVar,
1514
Callable,
1615
final,
1716
Any,
@@ -35,9 +34,15 @@ from rustworkx import generators # noqa
3534
from typing_extensions import Self
3635

3736
import numpy as np
37+
import sys
3838

39-
_S = TypeVar("_S")
40-
_T = TypeVar("_T")
39+
if sys.version_info >= (3, 13):
40+
from typing import TypeVar
41+
else:
42+
from typing_extensions import TypeVar
43+
44+
_S = TypeVar("_S", default=Any)
45+
_T = TypeVar("_T", default=Any)
4146

4247
class DAGHasCycle(Exception): ...
4348
class DAGWouldCycle(Exception): ...
@@ -1059,7 +1064,7 @@ def dominance_frontiers(graph: PyDiGraph[_S, _T], start_node: int, /) -> dict[in
10591064

10601065
# Iterators
10611066

1062-
_T_co = TypeVar("_T_co", covariant=True)
1067+
_T_co = TypeVar("_T_co", covariant=True, default=Any)
10631068

10641069
class _RustworkxCustomVecIter(Generic[_T_co], Sequence[_T_co], ABC):
10651070
def __init__(self) -> None: ...

0 commit comments

Comments
 (0)