Skip to content

BUG: fix SpanningTreeIterator AttributeError when using next() directly - #8584

Merged
rossbar merged 4 commits into
networkx:mainfrom
OliCheesman:fix/spanning-tree-iterator-8563
Apr 6, 2026
Merged

rossbar merged 4 commits into
networkx:mainfrom
OliCheesman:fix/spanning-tree-iterator-8563

Conversation

@OliCheesman

Copy link
Copy Markdown
Contributor

Fixes #8563

The partition_queue was only being initialised inside __iter__, which meant calling next() directly on SpanningTreeIterator (without a for-loop, which calls __iter__ implicitly) raised an AttributeError.
The fix moves the queue initialisation into __init__ so the iterator is fully operational immediately after construction. A regression test has been added to TestSpanningTreeIterator.

@OliCheesman

Copy link
Copy Markdown
Contributor Author

Hi, I'm a first time contributor and don't have permission to add labels. Could a maintainer add the appropriate label so the checks pass? Thanks!

@rossbar

rossbar commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

The first order of business is to undo the changes to unrelated files.

@Peiffap

Peiffap commented Mar 25, 2026

Copy link
Copy Markdown
Member

Thanks @OliCheesman!
The fix itself makes sense to me, but like Ross said, please undo the autoformatting changes to other files.
I'm updating the label as IMO this is more of a bug fix than an enhancement. Feel free to revert if you disagree Ross.

@OliCheesman
OliCheesman force-pushed the fix/spanning-tree-iterator-8563 branch from 361be23 to 9e19db3 Compare March 25, 2026 18:07
@OliCheesman
OliCheesman deleted the fix/spanning-tree-iterator-8563 branch March 25, 2026 18:11
@OliCheesman OliCheesman reopened this Mar 25, 2026
@OliCheesman OliCheesman reopened this Mar 25, 2026
@OliCheesman
OliCheesman force-pushed the fix/spanning-tree-iterator-8563 branch from b5f6fd3 to 4069a13 Compare March 25, 2026 19:32
@OliCheesman

Copy link
Copy Markdown
Contributor Author

Hi again. All checks have passed. Waiting for review.

@OliCheesman

Copy link
Copy Markdown
Contributor Author

@rossbar please could you look through my changes. The two changes I made originally are present and I reformatted to make sure all code fits the formatting requirements

@OliCheesman

Copy link
Copy Markdown
Contributor Author

Thanks @dschult!

@OliCheesman

Copy link
Copy Markdown
Contributor Author

Is it possible to get this reviewed? Many thanks

@amcandio

amcandio commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

This approach breaks the laziness property of the iterator. That is a nice property to have so you can initialize and pay the generator cost only when needed (for example, for online systems).

I'd suggest setting the value as None during init and add a check+initialization on the next call

@OliCheesman

Copy link
Copy Markdown
Contributor Author

Changes made as suggested @amcandio

@amcandio amcandio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@OliCheesman
OliCheesman requested a review from dschult March 29, 2026 15:07
@OliCheesman

Copy link
Copy Markdown
Contributor Author

Is it possible to get these changes checked and merged if not problems are found? Thanks

Comment thread networkx/algorithms/tree/mst.py Resolved
@OliCheesman
OliCheesman force-pushed the fix/spanning-tree-iterator-8563 branch from 1657683 to d2f7118 Compare April 5, 2026 08:18
@OliCheesman

Copy link
Copy Markdown
Contributor Author

Can i have some help here please? I have made the changes required but after running the pre-commit, files I do not wish to change seem to get changed. Only formatting changes like removing whitespace. This was pointed out earlier and I tried to fix it. Could anyone provide guidance on how I should proceed? Thanks

fill: none;
stroke: #9ecae1;
stroke-width: .5px;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop deltas that are not related to the change

@OliCheesman
OliCheesman force-pushed the fix/spanning-tree-iterator-8563 branch from 8153d19 to 463975c Compare April 6, 2026 10:29

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @OliCheesman and @amcandio !

@rossbar
rossbar merged commit 46a639a into networkx:main Apr 6, 2026
76 of 77 checks passed
@dschult dschult added this to the 3.7 milestone Apr 6, 2026
mcbosch pushed a commit to mcbosch/networkx that referenced this pull request Apr 7, 2026
…ly (networkx#8584)

Enable next(spanning_tree_iterator_instance) by moving priority queue
initialization from __iter__ to __next__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

BUG: SpanningTreeIterator raises on next

5 participants