From edb39f57e61919f0044f5fbded72a932aa2bed59 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 19 Oct 2025 00:41:36 -0500 Subject: [PATCH 1/3] Improve types in test_graph_tools --- pytools/test/test_graph_tools.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pytools/test/test_graph_tools.py b/pytools/test/test_graph_tools.py index b8a62877..2b159af2 100644 --- a/pytools/test/test_graph_tools.py +++ b/pytools/test/test_graph_tools.py @@ -258,8 +258,8 @@ def test_prioritized_topological_sort(): from pytools.graph import compute_topological_order rng = random.Random(0) - def generate_random_graph(nnodes): - graph = {i: set() for i in range(nnodes)} + def generate_random_graph(nnodes: int): + graph: dict[int, set[int]] = {i: set() for i in range(nnodes)} for i in range(nnodes): # to avoid cycles only consider edges node_i->node_j where j > i. for j in range(i+1, nnodes): @@ -270,14 +270,15 @@ def generate_random_graph(nnodes): nnodes = rng.randint(40, 100) rev_dep_graph = generate_random_graph(nnodes) - dep_graph = {i: set() for i in range(nnodes)} + dep_graph: dict[int, set[int]] = {i: set() for i in range(nnodes)} for i in range(nnodes): for rev_dep in rev_dep_graph[i]: dep_graph[rev_dep].add(i) keys = [rng.random() for _ in range(nnodes)] - topo_order = compute_topological_order(rev_dep_graph, key=keys.__getitem__) + topo_order = compute_topological_order( + rev_dep_graph, key=keys.__getitem__) for scheduled_node in topo_order: nodes_with_no_deps = {node for node, deps in dep_graph.items() @@ -303,7 +304,7 @@ def test_as_graphviz_dot(): "B": [], "C": ["A"]} - from pytools.graph import NodeT, as_graphviz_dot + from pytools.graph import Node, NodeT, as_graphviz_dot def edge_labels(n1: NodeT, n2: NodeT) -> str: if n1 == "A" and n2 == "B": @@ -311,7 +312,7 @@ def edge_labels(n1: NodeT, n2: NodeT) -> str: return "" - def node_labels(node: NodeT) -> str: + def node_labels(node: Node) -> str: if node == "A": return "foonode" From f6596ffe318593c86a2e9f26744da694df81ccba Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 19 Oct 2025 00:42:00 -0500 Subject: [PATCH 2/3] compute_topological_order: back to CanLt[Any], with explanation --- pytools/graph.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pytools/graph.py b/pytools/graph.py index d413f464..e51c1dc1 100644 --- a/pytools/graph.py +++ b/pytools/graph.py @@ -281,6 +281,8 @@ def __lt__(self, other: _HeapEntry[NodeT]) -> bool: def compute_topological_order( graph: GraphT[NodeT], + # should not use CanLt[object] because many types can't compare against + # everything under the sun key: Callable[[NodeT], optype.CanLt[Any]] | None = None, ) -> list[NodeT]: """Compute a topological order of nodes in a directed graph. From 33f081f778e91ef67bf191c481c8b4fa4a9aa343 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sun, 19 Oct 2025 00:42:04 -0500 Subject: [PATCH 3/3] Update baseline --- .basedpyright/baseline.json | 192 +++--------------------------------- 1 file changed, 16 insertions(+), 176 deletions(-) diff --git a/.basedpyright/baseline.json b/.basedpyright/baseline.json index 60a670a5..5afafdf2 100644 --- a/.basedpyright/baseline.json +++ b/.basedpyright/baseline.json @@ -4021,6 +4021,22 @@ "lineCount": 1 } }, + { + "code": "reportUnknownVariableType", + "range": { + "startColumn": 8, + "endColumn": 14, + "lineCount": 1 + } + }, + { + "code": "reportUnknownArgumentType", + "range": { + "startColumn": 45, + "endColumn": 69, + "lineCount": 1 + } + }, { "code": "reportAny", "range": { @@ -8461,182 +8477,6 @@ "lineCount": 1 } }, - { - "code": "reportUnknownParameterType", - "range": { - "startColumn": 8, - "endColumn": 29, - "lineCount": 1 - } - }, - { - "code": "reportUnknownParameterType", - "range": { - "startColumn": 30, - "endColumn": 36, - "lineCount": 1 - } - }, - { - "code": "reportMissingParameterType", - "range": { - "startColumn": 30, - "endColumn": 36, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 8, - "endColumn": 13, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 41, - "endColumn": 47, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 23, - "endColumn": 29, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 32, - "endColumn": 38, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 34, - "endColumn": 44, - "lineCount": 1 - } - }, - { - "code": "reportUnknownMemberType", - "range": { - "startColumn": 20, - "endColumn": 32, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 15, - "endColumn": 20, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 4, - "endColumn": 17, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 4, - "endColumn": 13, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 12, - "endColumn": 19, - "lineCount": 1 - } - }, - { - "code": "reportUnknownMemberType", - "range": { - "startColumn": 12, - "endColumn": 34, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 43, - "endColumn": 56, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 45, - "endColumn": 49, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 27, - "endColumn": 31, - "lineCount": 1 - } - }, - { - "code": "reportUnknownMemberType", - "range": { - "startColumn": 8, - "endColumn": 21, - "lineCount": 1 - } - }, - { - "code": "reportUnknownVariableType", - "range": { - "startColumn": 12, - "endColumn": 16, - "lineCount": 1 - } - }, - { - "code": "reportUnknownMemberType", - "range": { - "startColumn": 12, - "endColumn": 24, - "lineCount": 1 - } - }, - { - "code": "reportUnknownArgumentType", - "range": { - "startColumn": 15, - "endColumn": 24, - "lineCount": 1 - } - }, - { - "code": "reportInvalidTypeVarUse", - "range": { - "startColumn": 26, - "endColumn": 31, - "lineCount": 1 - } - }, { "code": "reportUnknownVariableType", "range": {