Skip to content

Commit f419fba

Browse files
committed
Manipulate version numbers as tuples.
Amend the SSSOMSchemaView#get_minimum_version() method to return a (major, minor) tuple, rather than a SssomVersionEnum object. The SssomVersionObject (which is automatically generated from the LinkML schema) is cumbersome to use, for at least two reasons: 1) obtaining the actual value of the enum requires accessing two levels of attributes (SssomVersionObject.code.text); 2) SssomVersionEnum values cannot be meaningfully compared (e.g. to check that a given version number is higher than another given version), we must (a) obtain the text value, (b) split that value over the middle dot, (c) convert the strings to integers, (d) put the integers into a tuple. OK, this can be done in one line of code, but this is cumbersome all the same, and it's best if that kind of things is not left to client code.
1 parent 28a2354 commit f419fba

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/sssom/constants.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
import uuid
55
from enum import Enum
66
from functools import cached_property, lru_cache
7-
from typing import Any, Dict, List, Literal, Set, TextIO, Union
7+
from typing import Any, Dict, List, Literal, Optional, Set, TextIO, Tuple, Union
88

99
import importlib_resources
1010
import yaml
1111
from linkml_runtime.utils.schema_as_dict import schema_as_dict
1212
from linkml_runtime.utils.schemaview import SchemaView
13-
from sssom_schema.datamodel.sssom_schema import SssomVersionEnum
1413

1514
HERE = pathlib.Path(__file__).parent.resolve()
1615

@@ -284,24 +283,30 @@ def propagatable_slots(self) -> List[str]:
284283
slots.append(slot_name)
285284
return slots
286285

287-
def get_minimum_version(self, slot_name: str, class_name: str = "mapping") -> SssomVersionEnum:
286+
def get_minimum_version(
287+
self, slot_name: str, class_name: str = "mapping"
288+
) -> Optional[Tuple[int, int]]:
288289
"""Get the minimum version of SSSOM required for a given slot.
289290
290291
:param slot_name: The queried slot.
291292
:param class_name: The class the slot belongs to. This is needed
292293
because a slot may have been added to a class
293294
in a later version than the version in which
294295
it was first introduced in the schema.
295-
:return: A SssomVersionEnum value representing the earliest
296-
version of SSSOM that defines the given slot in the
297-
given class. May be None if the requested slot name
298-
is not a valid slot name.
296+
:return: A tuple containing the major and minor numbers of the
297+
earliest version of SSSOM that defines the given slot
298+
in the given class. May be None if the requested slot
299+
name is not a valid slot name.
299300
"""
300301
try:
301302
slot = self.view.induced_slot(slot_name, class_name)
302-
return SssomVersionEnum(slot.annotations.added_in.value)
303+
version = [int(s) for s in slot.annotations.added_in.value.split(".")]
304+
if len(version) != 2:
305+
# Should never happen, schema is incorrect
306+
return None
307+
return (version[0], version[1])
303308
except AttributeError: # No added_in annotation, defaults to 1.0
304-
return SssomVersionEnum("1.0")
309+
return (1, 0)
305310
except ValueError: # No such slot
306311
return None
307312

src/sssom/util.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,18 +501,18 @@ def _to_string(row: dict[str, Any], side: str) -> str:
501501
def get_compatible_version(self) -> str:
502502
"""Get the minimum version of SSSOM this set is compatible with."""
503503
schema = SSSOMSchemaView()
504-
versions: Set[str] = set()
504+
versions: Set[Tuple[int, int]] = set()
505505

506506
# First get the minimum versions required by the slots present
507507
# in the set; this is entirely provided by the SSSOM model.
508508
for slot in self.metadata.keys():
509509
version = schema.get_minimum_version(slot, "mapping set")
510510
if version is not None:
511-
versions.add(str(version))
511+
versions.add(version)
512512
for slot in self.df.columns:
513513
version = schema.get_minimum_version(slot, "mapping")
514514
if version is not None:
515-
versions.add(str(version))
515+
versions.add(version)
516516

517517
# Then take care of enum values; we cannot use the SSSOM model
518518
# for that (enum values are not tagged with an "added_in"
@@ -531,16 +531,13 @@ def get_compatible_version(self) -> str:
531531
and "composed entity expression" in self.df[OBJECT_TYPE].values
532532
)
533533
):
534-
versions.add("1.1")
534+
versions.add((1, 1))
535535

536536
if MAPPING_CARDINALITY in self.df.columns and "0:0" in self.df[MAPPING_CARDINALITY].values:
537-
versions.add("1.1")
537+
versions.add((1, 1))
538538

539-
# Get the highest of the accumulated versions. We do a numerical
540-
# sort, so that version 1.10 (if we ever get that far in the 1.x
541-
# branch) does not get sorted before version 1.9.
542-
def _version_to_compare_key(version):
543-
return tuple(int(s) for s in version.split("."))
539+
# Get the highest of the accumulated versions.
540+
return ".".join([str(i) for i in max(versions)])
544541

545542
return max(versions, key=_version_to_compare_key)
546543

0 commit comments

Comments
 (0)