diff --git a/ontopy/ontology.py b/ontopy/ontology.py index fa2a1bb9..851f6ae2 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -43,6 +43,7 @@ ReadCatalogError, _validate_installed_version, LabelDefinitionError, + AmbiguousLabelError, EntityClassDefinitionError, EMMOntoPyException, ) @@ -204,12 +205,16 @@ def __dir__(self): lst = list(self.get_entities(imported=self._dir_imported)) if self._dir_preflabel: dirset.update( - _.prefLabel.first() for _ in lst if hasattr(_, "prefLabel") + dir.prefLabel.first() + for dir in lst + if hasattr(dir, "prefLabel") ) if self._dir_label: - dirset.update(_.label.first() for _ in lst if hasattr(_, "label")) + dirset.update( + dir.label.first() for dir in lst if hasattr(dir, "label") + ) if self._dir_name: - dirset.update(_.name for _ in lst if hasattr(_, "name")) + dirset.update(dir.name for dir in lst if hasattr(dir, "name")) dirset.difference_update({None}) # get rid of possible None return sorted(dirset) @@ -339,18 +344,21 @@ def get_by_label( prefix = splitlabel[0] if prefix: - entitylist = self.get_by_label_all( + entityset = self.get_by_label_all( label, label_annotations=label_annotations, prefix=prefix, ) - if len(entitylist) == 1: - return next(iter(entitylist)) - + if len(entityset) == 1: + return entityset.pop() + if len(entityset) > 1: + raise AmbiguousLabelError( + f"Several entities have the same label {label!r} " + f"with prefix {prefix!r}." + ) raise NoSuchLabelError( - f"Either no label annotations matches for {label!r} " - f"with prefix {prefix!r} or several entities have " - "the same label and prefix." + f"No label annotations matches for {label!r} " + f"with prefix {prefix!r}." ) # Label is a full IRI @@ -418,16 +426,16 @@ def get_by_label_all(self, label, label_annotations=None, prefix=None): if label_annotations: annotations = ( - _.name if hasattr(_, "storid") else _ for _ in label_annotations + ann.name if hasattr(ann, "storid") else ann + for ann in label_annotations ) elif self._label_annotations: - annotations = (_.name for _ in self.label_annotations) + annotations = (ann.name for ann in self.label_annotations) else: annotations = None entities = set() if annotations: - # entity = self.world.search(**{next(annotations): label}) for key in annotations: entities.update(self.world.search(**{key: label})) @@ -445,7 +453,9 @@ def get_by_label_all(self, label, label_annotations=None, prefix=None): if prefix: return set( - _ for _ in entities if _.namespace.ontology.prefix == prefix + ent + for ent in entities + if ent.namespace.ontology.prefix == prefix ) return entities diff --git a/ontopy/utils.py b/ontopy/utils.py index 3961131f..7020b477 100644 --- a/ontopy/utils.py +++ b/ontopy/utils.py @@ -66,6 +66,10 @@ class NoSuchLabelError(LookupError, AttributeError, EMMOntoPyException): """Error raised when a label cannot be found.""" +class AmbiguousLabelError(LookupError, AttributeError, EMMOntoPyException): + """Error raised when a label is ambiguous.""" + + class LabelDefinitionError(EMMOntoPyException): """Error in label definition.""" diff --git a/tests/ontopy_tests/test_new_entity.py b/tests/ontopy_tests/test_new_entity.py index fa954176..99794cf4 100644 --- a/tests/ontopy_tests/test_new_entity.py +++ b/tests/ontopy_tests/test_new_entity.py @@ -21,7 +21,6 @@ def test_new_entity(testonto: "Ontology") -> None: # Test that new entity is found by both version of get_by_label assert testonto.get_by_label("FantasyClass") == testonto.FantasyClass - print(testonto.get_by_label_all("*")) assert testonto.get_by_label_all("FantasyClass") == {testonto.FantasyClass} testonto.sync_attributes()