Skip to content

Commit

Permalink
Deprecate Tree.num_nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
hyanwong committed Nov 30, 2021
1 parent 1e15ea0 commit 87b11a8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
4 changes: 4 additions & 0 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

**Breaking changes**

- The ``Tree.num_nodes`` method is now deprecated with a warning, because it confusingly
returns the number of nodes in the entire tree sequence, rather than in the tree.
(:user:`hyanwong`, :issue:`1966` :pr:`1968`)

- The CLI ``info`` command now gives more detailed information on the tree sequence
(:user:`benjeffery`, :pr:`1611`)

Expand Down
17 changes: 0 additions & 17 deletions python/_tskitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9486,19 +9486,6 @@ Tree_get_sample_size(Tree *self)
return ret;
}

static PyObject *
Tree_get_num_nodes(Tree *self)
{
PyObject *ret = NULL;

if (Tree_check_state(self) != 0) {
goto out;
}
ret = Py_BuildValue("n", (Py_ssize_t) self->tree->num_nodes);
out:
return ret;
}

static PyObject *
Tree_get_num_roots(Tree *self)
{
Expand Down Expand Up @@ -10478,10 +10465,6 @@ static PyMethodDef Tree_methods[] = {
.ml_meth = (PyCFunction) Tree_get_sample_size,
.ml_flags = METH_NOARGS,
.ml_doc = "Returns the number of samples in this tree." },
{ .ml_name = "get_num_nodes",
.ml_meth = (PyCFunction) Tree_get_num_nodes,
.ml_flags = METH_NOARGS,
.ml_doc = "Returns the number of nodes in this tree." },
{ .ml_name = "get_num_roots",
.ml_meth = (PyCFunction) Tree_get_num_roots,
.ml_flags = METH_NOARGS,
Expand Down
16 changes: 13 additions & 3 deletions python/tskit/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,12 +1403,22 @@ def is_descendant(self, u, v):
def num_nodes(self):
"""
Returns the number of nodes in the :class:`TreeSequence` this tree is in.
Equivalent to ``tree.tree_sequence.num_nodes``. To find the number of
nodes that are reachable from all roots use ``len(list(tree.nodes()))``.
Equivalent to ``tree.tree_sequence.num_nodes``.
.. deprecated:: 0.4
Use :attr:`Tree.tree_sequence.num_nodes` if you want the number of nodes
in the entire tree sequence, or ``len(list(tree.nodes()))`` to find the
number of nodes that are reachable from all roots in this tree.
:rtype: int
"""
return self._ll_tree.get_num_nodes()
warnings.warn(
"This property is a deprecated alias for Tree.tree_sequence.num_nodes"
"and will be removed in the future",
FutureWarning,
)
return self.tree_sequence.num_nodes

@property
def num_roots(self):
Expand Down

0 comments on commit 87b11a8

Please sign in to comment.