Skip to content

Prevent iterating over a TopologyCounter #3202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
and a window breakpoint falls within an internal missing interval.
(:user:`nspope`, :pr:`3176`, :issue:`3175`)

- Prevent iterating over a ``TopologyCounter``
(:user:`benjeffery` , :pr:`3202`, :issue:`1462`)

--------------------
[0.6.4] - 2025-05-21
--------------------
Expand Down
5 changes: 5 additions & 0 deletions python/tests/test_combinatorics.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ def verify_topologies(self, ts, sample_sets=None, expected=None):
assert actual_topologies == expected[i][sample_set_indexes]
assert actual_topologies == actual_inc_topologies

def test_no_iterate(self):
with pytest.raises(TypeError, match="not iterable"):
for _ in tskit.Tree.generate_star(3).count_topologies():
pass

def subsample_topologies(self, ts, sample_sets, sample_set_indexes):
subsample_sets = [sample_sets[i] for i in sample_set_indexes]
topologies = collections.Counter()
Expand Down
6 changes: 6 additions & 0 deletions python/tskit/combinatorics.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ def __setitem__(self, sample_set_indexes, counter):
k = TopologyCounter._to_key(sample_set_indexes)
self.topologies[k] = counter

def __iter__(self):
raise TypeError(
"TopologyCounter object is not iterable, "
"iterate over '.topologies' instead"
)

@staticmethod
def _to_key(sample_set_indexes):
if not isinstance(sample_set_indexes, collections.abc.Iterable):
Expand Down
Loading