From f4f56ec8eed5166376f5b0a520c71cc445db9525 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Sat, 4 May 2024 12:59:09 +0200 Subject: [PATCH] #310 - Add short_name property to type - Added the property and a test --- cassis/typesystem.py | 9 +++++++-- tests/test_typesystem.py | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cassis/typesystem.py b/cassis/typesystem.py index cbc5be3..b3dc2ad 100644 --- a/cassis/typesystem.py +++ b/cassis/typesystem.py @@ -710,6 +710,11 @@ def descendants(self) -> Iterator["Type"]: for child in self._children.values(): yield from child.descendants + @property + def short_name(self) -> str: + """The short name of the type (i.e. the part after the last dot).""" + return self.name.split(".")[-1] + def subsumes(self, other_type: "Type") -> bool: """Determines if the type `other_type` is a child of `self`. @@ -838,10 +843,10 @@ def __init__(self, add_document_annotation_type: bool = True): if add_document_annotation_type: self._add_document_annotation_type() - def __iter__(self): + def __iter__(self) -> Iterator[Type]: return self.get_types() - def contains_type(self, typename: str): + def contains_type(self, typename: str) -> bool: """Checks whether this type system contains a type with name `typename`. Args: diff --git a/tests/test_typesystem.py b/tests/test_typesystem.py index cc3cfcd..36fba9e 100644 --- a/tests/test_typesystem.py +++ b/tests/test_typesystem.py @@ -511,6 +511,14 @@ def test_is_array(): assert cas.typesystem.is_array(type.name) == type.name.endswith("Array") +def test_type_name(): + cas = Cas() + + Annotation = cas.typesystem.get_type(TYPE_NAME_ANNOTATION) + assert Annotation.name == TYPE_NAME_ANNOTATION + assert Annotation.short_name == "Annotation" + + def test_get_types(): cas = Cas()