diff --git a/python/CHANGELOG.rst b/python/CHANGELOG.rst index 3a851c3e87..94ad670759 100644 --- a/python/CHANGELOG.rst +++ b/python/CHANGELOG.rst @@ -1,11 +1,14 @@ -------------------- -[0.5.X] - 2023-XX-XX +[0.5.6] - 2023-XX-XX -------------------- **Features** - Add ``TreeSequence.impute_unknown_mutations_time`` method to return an array of mutation times based on the times of associated nodes (:user:`duncanMR`, :pr:`2760`, :issue:`2758`) +- Add ``asdict`` to all dataclasses. These are returned when you access a row or + other tree sequence object. (:user:`benjeffery`, :pr:`2759`, :issue:`2719`) + -------------------- [0.5.5] - 2023-05-17 -------------------- diff --git a/python/tests/test_tables.py b/python/tests/test_tables.py index b6d207e2e0..265fee48b2 100644 --- a/python/tests/test_tables.py +++ b/python/tests/test_tables.py @@ -140,6 +140,14 @@ def table_5row(self, test_rows): table_5row.add_row(**row) return table_5row + def test_asdict(self, table, test_rows): + for table_row, test_row in zip(table, test_rows): + for k, v in table_row.asdict().items(): + if isinstance(v, np.ndarray): + assert np.array_equal(v, test_row[k]) + else: + assert v == test_row[k] + def test_max_rows_increment(self): for bad_value in [-1, -(2**10)]: with pytest.raises(ValueError): diff --git a/python/tskit/util.py b/python/tskit/util.py index 72f08499d6..28e9876b5a 100644 --- a/python/tskit/util.py +++ b/python/tskit/util.py @@ -47,6 +47,13 @@ def replace(self, **kwargs): """ return dataclasses.replace(self, **kwargs) + def asdict(self, **kwargs): + """ + Return a new dict which maps field names to their corresponding values + in this dataclass. + """ + return dataclasses.asdict(self, **kwargs) + def canonical_json(obj): """