Skip to content

Commit cc49b84

Browse files
hyanwongbenjeffery
authored andcommitted
str(null_schema) returns a meaningful string
1 parent 4430763 commit cc49b84

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎python/CHANGELOG.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
in the presence of non-ancestral material (very rare).
2525
(:user:`petrelharp`, :issue:`2983`, :pr:`1623`)
2626

27+
- Printing ``tskit.MetadataSchema(schema=None)`` now shows ``"Null_schema"`` rather
28+
than ``None``, to avoid confusion (:user:`hyanwong`, :pr:`2720`)
29+
2730
**Features**
2831

2932
- Add ``TreeSequence.extend_haplotypes`` method that extends ancestral haplotypes
@@ -110,8 +113,7 @@
110113
- Add ``__repr__`` for variants to return a string representation of the raw data
111114
without spewing megabytes of text (:user:`chriscrsmith`, :pr:`2695`, :issue:`2694`)
112115

113-
- Add ``keep_rows`` method to table classes to support efficient in-place
114-
table subsetting (:user:`jeromekelleher`, :pr:`2700`)
116+
**Breaking Changes**
115117

116118
**Bugfixes**
117119

‎python/tests/test_metadata.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ def test_schema_str(self):
322322
"required": ["one", "two"],
323323
"additionalProperties": False,
324324
}
325-
assert str(metadata.MetadataSchema(schema)) == pprint.pformat(schema)
325+
assert (
326+
str(metadata.MetadataSchema(schema))
327+
== f"tskit.MetadataSchema(\n{pprint.pformat(schema)}\n)"
328+
)
326329

327330
def test_register_codec(self):
328331
class TestCodec(metadata.AbstractMetadataCodec):

‎python/tests/test_tables.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ def test_metadata_schema(self, table_5row):
13961396
table2.metadata_schema = tskit.MetadataSchema({"codec": "json"})
13971397
with pytest.raises(
13981398
AssertionError,
1399-
match=f"{type(table_5row).__name__} metadata schemas differ:",
1399+
match=f"{type(table_5row).__name__} metadata schemas differ: ",
14001400
):
14011401
table_5row.assert_equals(table2)
14021402
table_5row.assert_equals(table2, ignore_metadata=True)
@@ -4089,7 +4089,7 @@ def test_metadata_schema(self, t1, t2):
40894089
t2.metadata_schema = tskit.MetadataSchema(None)
40904090
with pytest.raises(
40914091
AssertionError,
4092-
match=re.escape("Metadata schemas differ"),
4092+
match=re.escape("Metadata schemas differ:"),
40934093
):
40944094
t1.assert_equals(t2)
40954095
t1.assert_equals(t2, ignore_metadata=True)
@@ -4193,7 +4193,7 @@ def test_ignore_reference_sequence(self, t1, t2):
41934193
t2.reference_sequence.clear()
41944194
with pytest.raises(
41954195
AssertionError,
4196-
match=re.escape("Metadata schemas differ"),
4196+
match=re.escape("Metadata schemas differ: "),
41974197
):
41984198
t1.assert_equals(t2)
41994199
t1.assert_equals(t2, ignore_reference_sequence=True)

‎python/tskit/metadata.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,14 @@ def __repr__(self) -> str:
664664
return self._string
665665

666666
def __str__(self) -> str:
667-
return pprint.pformat(self._schema)
667+
if isinstance(self._schema, collections.OrderedDict):
668+
s = pprint.pformat(dict(self._schema))
669+
else:
670+
s = pprint.pformat(self._schema)
671+
if "\n" in s:
672+
return f"tskit.MetadataSchema(\n{s}\n)"
673+
else:
674+
return f"tskit.MetadataSchema({s})"
668675

669676
def __eq__(self, other) -> bool:
670677
return self._string == other._string

0 commit comments

Comments
 (0)