diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index cca611a4cd470a8e5303fd2418a40ba35e447e01..fb16918241e30c57c8439ad316c89f441808bef6 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -59,17 +59,17 @@ from caosdb.exceptions import (AmbiguousEntityError, AuthorizationError, TransactionError, UniqueNamesError, UnqualifiedParentsError, UnqualifiedPropertiesError) -from lxml import etree +from lxml import etree # type: ignore _ENTITY_URI_SEGMENT = "Entity" # importances/inheritance -OBLIGATORY = "OBLIGATORY" -SUGGESTED = "SUGGESTED" -RECOMMENDED = "RECOMMENDED" -FIX = "FIX" -ALL = "ALL" -NONE = "NONE" +OBLIGATORY: Literal["OBLIGATORY"] = "OBLIGATORY" +SUGGESTED: Literal["SUGGESTED"] = "SUGGESTED" +RECOMMENDED: Literal["RECOMMENDED"] = "RECOMMENDED" +FIX: Literal["FIX"] = "FIX" +ALL: Literal["ALL"] = "ALL" +NONE: Literal["NONE"] = "NONE" TA_add_properties = Union[Literal['ALL', 'FIX', 'NONE'], None] @@ -97,7 +97,7 @@ class Entity(object): self._wrapped_entity = None self._version = None self._cuid = None - self._flags = dict() + self._flags: dict = dict() self.__value = None self.__datatype = None self.datatype = datatype @@ -187,6 +187,10 @@ class Entity(object): self.__value = _parse_value(new_type, self.__value) self.__datatype = new_type + @property + def checksum(self): + return self._checksum + @property def description(self): if self.__description is not None or self._wrapped_entity is None: @@ -194,10 +198,6 @@ class Entity(object): return self._wrapped_entity.description - @property - def checksum(self): - return self._checksum - @description.setter def description(self, new_description): self.__description = new_description @@ -556,14 +556,20 @@ class Entity(object): if not (check_name or check_id): return parent in parents - name_result = ( - not check_name or - (parent.name is not None and - parent.name in [pp.name for pp in parents])) - id_result = ( - not check_id or - (parent.id is not None and - parent.id in [pp.id for pp in parents])) + parent_name = getattr(parent, "name", None) + parent_id = getattr(parent, "id", None) + name_result: bool = False + id_result: bool = False + if parent_name: + name_result = ( + not check_name or + (parent_name is not None and + parent_name in [pp.name for pp in parents])) + if parent_id: + id_result = ( + not check_id or + (parent_id is not None and + parent_id in [pp.id for pp in parents])) return name_result and id_result @@ -677,17 +683,18 @@ class Entity(object): Wrong input type. """ # entity given - - if (hasattr(pattern, "name") or hasattr(pattern, "id")): + pattern_name = getattr(pattern, "name", None) + pattern_id = getattr(pattern, "id", None) + if pattern_name or pattern_id: # only return if a result was found, otherwise use id - if (hasattr(pattern, "name") and pattern.name is not None - and self.get_property(pattern.name) is not None): + if (pattern_name is not None + and self.get_property(pattern_name) is not None): - return self.get_property(pattern.name) + return self.get_property(pattern_name) - if hasattr(pattern, "id") and pattern.id is not None: - return self.get_property(pattern.id) + if pattern_id is not None: + return self.get_property(pattern_id) # int given elif isinstance(pattern, int): @@ -770,7 +777,7 @@ class Entity(object): # if we saved a special selector before, apply it if special_selector is None: - return prop.value + return prop.value # type: ignore[union-attr] else: return getattr(ref, special_selector.lower()) @@ -802,7 +809,7 @@ class Entity(object): tuple A row-like representation of the entity's properties. """ - row = tuple() + row: tuple = tuple() for selector in selectors: val = self._get_value_for_selector(selector) @@ -839,7 +846,7 @@ class Entity(object): return ret - def get_errors(self) -> list: + def get_errors(self) -> dict: """Get all error messages of this entity. Returns @@ -858,7 +865,7 @@ class Entity(object): return ret - def get_errors_deep(self, roots: Entity = None) -> list: + def get_errors_deep(self, roots: Union[Entity, list, None] = None) -> list: """Get all error messages of this entity and all sub-entities / parents / properties. @@ -1043,7 +1050,7 @@ class Entity(object): return xml @staticmethod - def _from_xml(entity: Entity, elem: etree._Element) -> Entity: + def _from_xml(entity: Any, elem: etree._Element) -> Entity: """Parse a single string representation of an xml element to an entity. Parameters @@ -1074,7 +1081,8 @@ class Entity(object): entity.unit = elem.get("unit") entity.file = elem.get("file") - if hasattr(entity, "affiliation"): + entity_affiliation = getattr(entity, "affiliation", None) + if entity_affiliation: entity.affiliation = elem.get("affiliation") vals = list() @@ -1659,8 +1667,9 @@ class QueryTemplate(): @staticmethod def _from_xml(xml: etree._Element): if xml.tag.lower() == "querytemplate": - q = QueryTemplate(name=xml.get("name"), + q: Any = QueryTemplate(name=xml.get("name"), description=xml.get("description"), query=None) + # TODO: Add correct type hint. Any is just a fix. 17.08.2021, AK for e in xml: if e.tag.lower() == "query": @@ -1736,7 +1745,7 @@ class Parent(Entity): self.__affiliation = None def to_xml(self, xml: Optional[etree._Element] = None, - add_properties: TA_add_properties = None) -> etree._Element: + add_properties: TA_add_properties = None, *args, **kwargs) -> etree._Element: if xml is None: xml = etree.Element("Parent") @@ -1787,7 +1796,7 @@ class Property(Entity): Returns ------- Entity - + See Also -------- Entity.add_parent