Skip to content

Commit 3118b53

Browse files
committed
Tweaks for docs and an extra test
1 parent 4ec80ea commit 3118b53

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

python/tests/test_tables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,21 @@ def test_metadata_vector_errors(self):
11641164
with pytest.raises(KeyError):
11651165
_ = table.metadata_vector("x")
11661166

1167+
table.clear()
1168+
metadata_list = [
1169+
{"a": {"c": 5}, "u": [1, 2]},
1170+
{"a": {"b": 6}},
1171+
]
1172+
for md in metadata_list:
1173+
table.add_row(
1174+
**{
1175+
**self.input_data_for_add_row(),
1176+
"metadata": md,
1177+
}
1178+
)
1179+
with pytest.raises(KeyError):
1180+
_ = table.metadata_vector(["a", "x"])
1181+
11671182
def test_metadata_vector_nodefault(self):
11681183
table = self.table_class()
11691184
ms = tskit.MetadataSchema({"codec": "json"})

python/tskit/tables.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@
4949

5050
dataclass_options = {"frozen": True}
5151

52-
# we'll need this because python can't tell if kwargs are passed in not
53-
DEFAULT = object()
52+
53+
# Needed for cases where `None` can be an appropriate kwarg value,
54+
# we override the meta so that it looks good in the docs.
55+
class NotSetMeta(type):
56+
def __repr__(cls):
57+
return "Not set"
58+
59+
60+
class NOTSET(metaclass=NotSetMeta):
61+
pass
5462

5563

5664
@metadata.lazy_decode
@@ -590,7 +598,7 @@ def _update_metadata_schema_cache_from_ll(self) -> None:
590598
self.ll_table.metadata_schema
591599
)
592600

593-
def metadata_vector(self, key, *, dtype=None, default_value=DEFAULT):
601+
def metadata_vector(self, key, *, dtype=None, default_value=NOTSET):
594602
"""
595603
Returns a numpy array of metadata values obtained by extracting ``key``
596604
from each metadata entry, and using ``default_value`` if the key is
@@ -602,10 +610,11 @@ def metadata_vector(self, key, *, dtype=None, default_value=DEFAULT):
602610
:param str dtype: The dtype of the result (can usually be omitted).
603611
:param object default_value: The value to be inserted if the metadata key
604612
is not present. Note that for numeric columns, a default value of None
605-
will result in a non-numeric array.
613+
will result in a non-numeric array. The default behaviour is to raise
614+
``KeyError`` on missing entries.
606615
"""
607616

608-
if default_value == DEFAULT:
617+
if default_value == NOTSET:
609618

610619
def getter(d, k):
611620
return d[k]

0 commit comments

Comments
 (0)