From 4eebf8bd04fc29b944d77a758daa0b88f1e86efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0lker=20O=C4=9Fulcan=20Say=C4=B1n?= <47698920+iosayin@users.noreply.github.com> Date: Wed, 14 Oct 2026 13:46:16 +0300 Subject: [PATCH] fix(graphs): use None sentinel in Boruvka MST and un-ignore ty not-iterable `Graph.boruvka_mst` stored `-1` in `cheap_edge` as a "no edge yet" marker next to `[head, tail, weight]` lists. Mixing an int sentinel with list values makes the later unpacking `head, tail, weight = ...` fail type checking (`int & ~Literal[-1]` is not iterable), which was the only `not-iterable` diagnostic left in the repository. Use `None` as the sentinel with an explicit `list[int] | None` value type and narrow through a local variable. Behaviour is unchanged. With that diagnostic gone, remove `rules.not-iterable = "ignore"` from `[tool.ty]` in pyproject.toml. Part of #15187 --- graphs/minimum_spanning_tree_boruvka.py | 17 ++++++++--------- pyproject.toml | 1 - 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/graphs/minimum_spanning_tree_boruvka.py b/graphs/minimum_spanning_tree_boruvka.py index 8ef8f6b7c827..fcbb4a7ff366 100644 --- a/graphs/minimum_spanning_tree_boruvka.py +++ b/graphs/minimum_spanning_tree_boruvka.py @@ -167,9 +167,9 @@ def boruvka_mst(graph) -> "Graph": union_find = Graph.UnionFind() mst_edges = [] while num_components > 1: - cheap_edge = {} - for vertex in graph.get_vertices(): - cheap_edge[vertex] = -1 + cheap_edge: dict[int, list[int] | None] = dict.fromkeys( + graph.get_vertices() + ) edges = graph.get_edges() for edge in edges: @@ -180,13 +180,12 @@ def boruvka_mst(graph) -> "Graph": set1 = union_find.find(head) set2 = union_find.find(tail) if set1 != set2: - if cheap_edge[set1] == -1 or cheap_edge[set1][2] > weight: - cheap_edge[set1] = [head, tail, weight] - - if cheap_edge[set2] == -1 or cheap_edge[set2][2] > weight: - cheap_edge[set2] = [head, tail, weight] + for component in (set1, set2): + current = cheap_edge[component] + if current is None or current[2] > weight: + cheap_edge[component] = [head, tail, weight] for head_tail_weight in cheap_edge.values(): - if head_tail_weight != -1: + if head_tail_weight is not None: head, tail, weight = head_tail_weight if union_find.find(head) != union_find.find(tail): union_find.union(head, tail) diff --git a/pyproject.toml b/pyproject.toml index 52bd60b9d3aa..03c5d234303f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -321,7 +321,6 @@ rules.deprecated = "ignore" rules.invalid-argument-type = "ignore" rules.invalid-return-type = "ignore" rules.no-matching-overload = "ignore" -rules.not-iterable = "ignore" rules.not-subscriptable = "ignore" rules.parameter-already-assigned = "ignore" rules.unresolved-attribute = "ignore"