Skip to content

Commit d67dfb4

Browse files
authored
Merge pull request #1091 from benjeffery/default-metadata
Make default metadata schema dependant
2 parents 24f3d0e + b36c5bc commit d67dfb4

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.2.0
3+
rev: v3.4.0
44
hooks:
55
- id: check-merge-conflict
66
- id: debug-statements
77
- id: mixed-line-ending
88
- id: check-case-conflict
99
- id: check-yaml
1010
- repo: https://github.com/benjeffery/pre-commit-clang-format
11-
rev: master
11+
rev: c21a74d089aaeb86c2c19df371c7e7bf40c07207
1212
hooks:
1313
- id: clang-format
1414
exclude: dev-tools|examples
1515
verbose: true
1616
- repo: https://github.com/asottile/reorder_python_imports
17-
rev: v2.3.5
17+
rev: v2.3.6
1818
hooks:
1919
- id: reorder-python-imports
2020
args: [--application-directories=python,
2121
--unclassifiable-application-module=_tskit]
2222
- repo: https://github.com/asottile/pyupgrade
23-
rev: v2.7.2
23+
rev: v2.7.4
2424
hooks:
2525
- id: pyupgrade
2626
args: [--py3-plus, --py36-plus]
@@ -33,13 +33,13 @@ repos:
3333
# loop that throws an error before black excludes it
3434
args: [python/tskit, python/setup.py, python/stress_lowlevel.py]
3535
- repo: https://gitlab.com/pycqa/flake8
36-
rev: 3.8.3
36+
rev: 3.8.4
3737
hooks:
3838
- id: flake8
3939
args: [--config=python/.flake8]
4040
additional_dependencies: ["flake8-bugbear==20.1.4", "flake8-builtins==1.5.2"]
4141
- repo: https://github.com/asottile/blacken-docs
42-
rev: v1.8.0
42+
rev: v1.9.1
4343
hooks:
4444
- id: blacken-docs
4545
args: [--skip-errors]

python/CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
the shortest possible branch lengths instead of a fixed epsilon of
1212
1e-10. (:user:`jeromekelleher`, :issue:`1089`, :pr:`1090`)
1313

14+
- Default value metadata in ``add_row`` functions is now schema-dependant, so that
15+
``metadata={}`` is no longer needed as an argument when a schema is present
16+
(:user:`benjeffery`, :issue:`1084`).
17+
18+
1419
--------------------
1520
[0.3.4] - 2020-12-02
1621
--------------------

python/tests/test_tables.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,21 @@ def test_default_metadata_schema(self):
760760
}
761761
)
762762

763+
def test_default_metadata_add_row(self):
764+
row_data = self.input_data_for_add_row()
765+
del row_data["metadata"]
766+
767+
table = self.table_class()
768+
table.add_row(**row_data)
769+
assert table[0].metadata == b""
770+
assert table[0].metadata == table.metadata_schema.empty_value
771+
772+
table = self.table_class()
773+
table.metadata_schema = tskit.MetadataSchema({"codec": "json"})
774+
table.add_row(**row_data)
775+
assert table[0].metadata == {}
776+
assert table[0].metadata == table.metadata_schema.empty_value
777+
763778
def test_row_round_trip_metadata_schema(self):
764779
data = self.metadata_example_data()
765780
table = self.table_class()

python/tskit/metadata.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ def __init__(self, schema: Optional[Mapping[str, Any]]) -> None:
510510
self._validate_row = validate_bytes
511511
self.encode_row = NOOPCodec({}).encode
512512
self.decode_row = NOOPCodec({}).decode
513+
self.empty_value = b""
513514
else:
514515
try:
515516
TSKITMetadataSchemaValidator.check_schema(schema)
@@ -530,6 +531,7 @@ def __init__(self, schema: Optional[Mapping[str, Any]]) -> None:
530531
self._validate_row = TSKITMetadataSchemaValidator(schema).validate
531532
self.encode_row = codec_instance.encode
532533
self.decode_row = codec_instance.decode
534+
self.empty_value = {}
533535

534536
def __repr__(self) -> str:
535537
return self._string

python/tskit/tables.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,9 +510,13 @@ def add_row(self, flags=0, location=None, metadata=None):
510510
array describing the location of this individual. If not specified
511511
or None, a zero-dimensional location is stored.
512512
:param object metadata: Any object that is valid metadata for the table's schema.
513+
Defaults to the default metadata value for the table's schema. This is
514+
typically ``{}``. For no schema, ``None``.
513515
:return: The ID of the newly added node.
514516
:rtype: int
515517
"""
518+
if metadata is None:
519+
metadata = self.metadata_schema.empty_value
516520
metadata = self.metadata_schema.validate_and_encode_row(metadata)
517521
return self.ll_table.add_row(flags=flags, location=location, metadata=metadata)
518522

@@ -718,9 +722,13 @@ def add_row(self, flags=0, time=0, population=-1, individual=-1, metadata=None):
718722
:param int individual: The ID of the individual in which the new node was born.
719723
Defaults to :data:`tskit.NULL`.
720724
:param object metadata: Any object that is valid metadata for the table's schema.
725+
Defaults to the default metadata value for the table's schema. This is
726+
typically ``{}``. For no schema, ``None``.
721727
:return: The ID of the newly added node.
722728
:rtype: int
723729
"""
730+
if metadata is None:
731+
metadata = self.metadata_schema.empty_value
724732
metadata = self.metadata_schema.validate_and_encode_row(metadata)
725733
return self.ll_table.add_row(flags, time, population, individual, metadata)
726734

@@ -913,9 +921,13 @@ def add_row(self, left, right, parent, child, metadata=None):
913921
:param int parent: The ID of parent node.
914922
:param int child: The ID of child node.
915923
:param object metadata: Any object that is valid metadata for the table's schema.
924+
Defaults to the default metadata value for the table's schema. This is
925+
typically ``{}``. For no schema, ``None``.
916926
:return: The ID of the newly added edge.
917927
:rtype: int
918928
"""
929+
if metadata is None:
930+
metadata = self.metadata_schema.empty_value
919931
metadata = self.metadata_schema.validate_and_encode_row(metadata)
920932
return self.ll_table.add_row(left, right, parent, child, metadata)
921933

@@ -1127,9 +1139,13 @@ def add_row(self, left, right, node, source, dest, time, metadata=None):
11271139
:param int dest: The ID of the destination population.
11281140
:param float time: The time of the migration event.
11291141
:param object metadata: Any object that is valid metadata for the table's schema.
1142+
Defaults to the default metadata value for the table's schema. This is
1143+
typically ``{}``. For no schema, ``None``.
11301144
:return: The ID of the newly added migration.
11311145
:rtype: int
11321146
"""
1147+
if metadata is None:
1148+
metadata = self.metadata_schema.empty_value
11331149
metadata = self.metadata_schema.validate_and_encode_row(metadata)
11341150
return self.ll_table.add_row(left, right, node, source, dest, time, metadata)
11351151

@@ -1334,9 +1350,13 @@ def add_row(self, position, ancestral_state, metadata=None):
13341350
:param float position: The position of this site in genome coordinates.
13351351
:param str ancestral_state: The state of this site at the root of the tree.
13361352
:param object metadata: Any object that is valid metadata for the table's schema.
1353+
Defaults to the default metadata value for the table's schema. This is
1354+
typically ``{}``. For no schema, ``None``.
13371355
:return: The ID of the newly added site.
13381356
:rtype: int
13391357
"""
1358+
if metadata is None:
1359+
metadata = self.metadata_schema.empty_value
13401360
metadata = self.metadata_schema.validate_and_encode_row(metadata)
13411361
return self.ll_table.add_row(position, ancestral_state, metadata)
13421362

@@ -1560,11 +1580,15 @@ def add_row(self, site, node, derived_state, parent=-1, metadata=None, time=None
15601580
:param int parent: The ID of the parent mutation. If not specified,
15611581
defaults to :attr:`NULL`.
15621582
:param object metadata: Any object that is valid metadata for the table's schema.
1583+
Defaults to the default metadata value for the table's schema. This is
1584+
typically ``{}``. For no schema, ``None``.
15631585
:return: The ID of the newly added mutation.
15641586
:param float time: The occurrence time for the new mutation. If not specified,
15651587
defaults to ``UNKNOWN_TIME``, indicating the time is unknown.
15661588
:rtype: int
15671589
"""
1590+
if metadata is None:
1591+
metadata = self.metadata_schema.empty_value
15681592
metadata = self.metadata_schema.validate_and_encode_row(metadata)
15691593
return self.ll_table.add_row(
15701594
site,
@@ -1763,9 +1787,13 @@ def add_row(self, metadata=None):
17631787
:attr:`metadata_schema<tskit.PopulationTable.metadata_schema>`.
17641788
17651789
:param object metadata: Any object that is valid metadata for the table's schema.
1790+
Defaults to the default metadata value for the table's schema. This is
1791+
typically ``{}``. For no schema, ``None``.
17661792
:return: The ID of the newly added population.
17671793
:rtype: int
17681794
"""
1795+
if metadata is None:
1796+
metadata = self.metadata_schema.empty_value
17691797
metadata = self.metadata_schema.validate_and_encode_row(metadata)
17701798
return self.ll_table.add_row(metadata=metadata)
17711799

0 commit comments

Comments
 (0)