From a451d7af282dbcf111a423f0d0d3f6375ac4f2a4 Mon Sep 17 00:00:00 2001 From: Yan Wong Date: Thu, 2 Mar 2023 15:22:21 +0000 Subject: [PATCH] str(null_schema) returns "Null_schema" not None --- python/CHANGELOG.rst | 6 ++++-- python/tests/test_tables.py | 19 +++++++++++++++---- python/tskit/metadata.py | 9 ++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/python/CHANGELOG.rst b/python/CHANGELOG.rst index 1fd56f3b01..c12283b522 100644 --- a/python/CHANGELOG.rst +++ b/python/CHANGELOG.rst @@ -24,6 +24,9 @@ in the presence of non-ancestral material (very rare). (:user:`petrelharp`, :issue:`2983`, :pr:`1623`) +- Printing ``tskit.MetadataSchema(schema=None)`` now shows ``"Null_schema"`` rather + than ``None``, to avoid confusion (:user:`hyanwong`, :pr:`2720`) + **Features** - Add ``TreeSequence.extend_haplotypes`` method that extends ancestral haplotypes @@ -103,8 +106,7 @@ - Add ``__repr__`` for variants to return a string representation of the raw data without spewing megabytes of text (:user:`chriscrsmith`, :pr:`2695`, :issue:`2694`) -- Add ``keep_rows`` method to table classes to support efficient in-place - table subsetting (:user:`jeromekelleher`, :pr:`2700`) +**Breaking Changes** **Bugfixes** diff --git a/python/tests/test_tables.py b/python/tests/test_tables.py index 7551f3250c..d283f458d8 100644 --- a/python/tests/test_tables.py +++ b/python/tests/test_tables.py @@ -1396,9 +1396,10 @@ def test_metadata_schema(self, table_5row): table2.metadata_schema = tskit.MetadataSchema({"codec": "json"}) with pytest.raises( AssertionError, - match=f"{type(table_5row).__name__} metadata schemas differ:", + match=f"{type(table_5row).__name__} metadata schemas differ: " + "self=Null_schema other=OrderedDict([('codec', 'json')])", ): - table_5row.assert_equals(table2) + table_5row.assert_equals(table2) table_5row.assert_equals(table2, ignore_metadata=True) def test_row_changes(self, table_5row, test_rows): @@ -4089,7 +4090,10 @@ def test_metadata_schema(self, t1, t2): t2.metadata_schema = tskit.MetadataSchema(None) with pytest.raises( AssertionError, - match=re.escape("Metadata schemas differ"), + match=re.escape( + "Metadata schemas differ: self=OrderedDict([('codec', 'json')]) " + "other=Null_schema" + ), ): t1.assert_equals(t2) t1.assert_equals(t2, ignore_metadata=True) @@ -4136,6 +4140,10 @@ def test_ignore_metadata(self, t1, t2, table_name): with pytest.raises( AssertionError, match=re.escape(f"{type(table).__name__} metadata schemas differ:"), + match=re.escape( + f"{type(table).__name__} metadata schemas differ: " + f"self=OrderedDict([('codec', 'json')]) other=Null_schema" + ), ): t1.assert_equals(t2) t1.assert_equals(t2, ignore_metadata=True) @@ -4193,7 +4201,10 @@ def test_ignore_reference_sequence(self, t1, t2): t2.reference_sequence.clear() with pytest.raises( AssertionError, - match=re.escape("Metadata schemas differ"), + match=re.escape( + "Metadata schemas differ: " + "self=OrderedDict([('codec', 'json')]) other=Null_schema" + ), ): t1.assert_equals(t2) t1.assert_equals(t2, ignore_reference_sequence=True) diff --git a/python/tskit/metadata.py b/python/tskit/metadata.py index f9e8c0c6c7..781daf5ee3 100644 --- a/python/tskit/metadata.py +++ b/python/tskit/metadata.py @@ -664,7 +664,14 @@ def __repr__(self) -> str: return self._string def __str__(self) -> str: - return pprint.pformat(self._schema) + if isinstance(self._schema, collections.OrderedDict): + s = pprint.pformat(dict(self._schema)) + else: + s = pprint.pformat(self._schema) + if "\n" in s: + return f"MetadataSchema(\n{s}\n)" + else: + return f"MetadataSchema({s})" def __eq__(self, other) -> bool: return self._string == other._string