Skip to content

Use None sentinel in Boruvka MST and un-ignore ty not-iterable rule - #15575

Open
iosayin wants to merge 1 commit into
TheAlgorithms:masterfrom
iosayin:fix/ty-not-iterable-boruvka
Open

iosayin wants to merge 1 commit into
TheAlgorithms:masterfrom
iosayin:fix/ty-not-iterable-boruvka

Conversation

@iosayin

@iosayin iosayin commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Describe your change

Part of #15187: un-ignores the ty not-iterable rule.

Graph.boruvka_mst in graphs/minimum_spanning_tree_boruvka.py used -1 in cheap_edge to mean "no cheapest edge yet", while the real values are [head, tail, weight] lists. Because an int and a list share the same dict, the later head, tail, weight = head_tail_weight produced the only not-iterable diagnostic left in the repo:

graphs/minimum_spanning_tree_boruvka.py:190:42 error[not-iterable] Object of type `int & ~Literal[-1]` is not iterable

Changes:

  • cheap_edge is now dict[int, list[int] | None], built with dict.fromkeys(...). None is the sentinel and checks use is None / is not None.
  • The two copy-pasted "update cheapest edge for this component" blocks are now one small loop over (set1, set2) that reads the value into a local, so ty can narrow it without a not-subscriptable error.
  • Removed rules.not-iterable = "ignore" from [tool.ty] in pyproject.toml.

Behaviour is unchanged. The existing doctest passes as before. I also compared the MST total weight and edge count against a Kruskal reference on 3,000 random connected graphs, with and without distinct_weight(), before and after the change, and found no differences.

Verification (Python 3.15):

  • python -m doctest -v graphs/minimum_spanning_tree_boruvka.py: passed
  • ruff check / ruff format --check on the file: clean
  • ty check --exclude-scripts with the rule enabled: 0 not-iterable diagnostics, same total count as on master with the rule ignored
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

…erable

`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 TheAlgorithms#15187

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant