Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#310 - Add short_name property to type #311

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cassis/typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_typesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down