diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 7034d70e8cb6e024e45ee34ddff4b0f36147a00d..0616c9be978dc8ad7d70d152a9e66b876adb2c76 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -85,8 +85,9 @@ class Entity(object): by the user to control several server-side plug-ins. """ - def __init__(self, name=None, id=None, description=None, # @ReservedAssignment - datatype=None, value=None, **kwargs): + def __init__(self, name: Optional[str] = None, id: Optional[int] = None, + description: Optional[str] = None, + datatype: Optional[str] = None, value=None, **kwargs): # @ReservedAssignment self.__role = kwargs["role"] if "role" in kwargs else None self._checksum = None self._size = None @@ -94,7 +95,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 @@ -193,7 +194,7 @@ class Entity(object): return self.__description return self._wrapped_entity.description - + @description.setter def description(self, new_description): self.__description = new_description @@ -343,8 +344,12 @@ class Entity(object): return self - def add_property(self, property=None, value=None, id=None, name=None, description=None, datatype=None, - unit=None, importance=None, inheritance=None): # @ReservedAssignment + def add_property(self, property: Union[int, str, Entity, None] = None, + value: Union[str, int, Entity, None] = None, + id: Optional[int] = None, name: Optional[str] = None, + description: Optional[str] = None, datatype: Optional[str] = None, + unit: Optional[str] = None, importance: Optional[str] = None, + inheritance: Optional[str] = None) -> Entity: # @ReservedAssignment """Add a property to this entity. The first parameter is meant to identify the property entity. So the method expects an instance of @@ -355,23 +360,23 @@ class Entity(object): Parameters ---------- - property : int, str, Property, optional + property : Union[int, str, Entity, None], optional An identifying parameter, by default None - value : int, str, Property, optional + value : Union[str, int, Entity, None], optional The value of the new property, by default None - id : int, optional + id : Optional[int], optional Id of the property, by default None - name : str, optional + name : Optional[str], optional Name of the property, by default None - description : str, optional + description : Optional[str], optional Description of the property, by default None - datatype : str, optional + datatype : Optional[str], optional Datatype of the property, by default None - unit : str, optional + unit : Optional[str], optional Unit of the property, by default None - importance :str, optional + importance : Optional[str], optional Importance of the property, by default None - inheritance : str, optional + inheritance : Optional[str], optional Inheritance of the property, by default None Returns @@ -380,13 +385,15 @@ class Entity(object): Raises ------ + ValueError + Unusual Property role UserWarning If the first parameter is None then id or name must be defined and not be None. UserWarning If the first parameter is an integer then it is interpreted as the id and id must be undefined or None. UserWarning - If the first parameter is not None and neither an instance of Entity nor an integer it is + If the first parameter is not None and neither an instance of Entity nor an integer it is interpreted as the name and name must be undefined or None. """ @@ -433,17 +440,29 @@ class Entity(object): return self - def add_message(self, msg=None, type=None, code=None, # @ReservedAssignment - description=None, body=None): + def add_message(self, msg: Optional[Message] = None, type: Union[str, int, None] = None, + code: Union[str, int, None] = None, # @ReservedAssignment + description: Optional[str] = None, + body: Optional[str] = None) -> Entity: """Add a message (msg) to this entity. If and only if no msg is given this method will created a new message from the parameters type, code, description, and body. - @param msg: The message to be added to this entity. - @param type: The type of the message to be added. - @param code: The code of the message to be added. - @param description: The description of the message to be added. - @param body: The body of the message to be added. + Parameters + ---------- + msg : Message, optional + The message to be added to this entity, by default None + type : str, int, optional + The type of the message to be added, by default None + code : str, int, optional + The code of the message to be added, by default None + body : str, optional + The body of the message to be added, by default None + + Returns + ------- + Entity + The entity passed """ if msg is not None: @@ -454,38 +473,44 @@ class Entity(object): return self - def add_parent(self, parent=None, id=None, name=None, inheritance=None): # @ReservedAssignment + def add_parent(self, parent: Union[Entity, int, str, None] = None, + id: Optional[int] = None, name: Optional[str] = None, + inheritance: Optional[str] = None) -> Entity: # @ReservedAssignment """Add a parent to this entity. Parameters ---------- - parent : Entity or int or str or None + parent : Union[Entity, int, str, None], optional The parent entity, either specified by the Entity object - itself, or its id or its name. Default is None. - id : int + itself, or its id or its name, by default None + id : Optional[int], optional Integer id of the parent entity. Ignored if `parent` - is not None. - name : str + is not None, by default None + name : Optional[str], optional Name of the parent entity. Ignored if `parent is not - none`. - inheritance : str + none`, by default None + inheritance : Optional[str], optional One of ``obligatory``, ``recommended``, ``suggested``, or ``fix``. Specifies the minimum importance which parent properties need to have to be inherited by this entity. If no `inheritance` is given, no properties will be inherited by the child. This parameter is case-insensitive. - Note that the behaviour is currently not yet specified when assigning parents to + Note that the behaviour is currently not yet specified when assigning parents to Records, it only works for inheritance of RecordTypes (and Properties). For more information, it is recommended to look into the - :ref:`data insertion tutorial<tutorial-inheritance-properties>`. + :ref:`data insertion tutorial<tutorial-inheritance-properties>`, by default None + + Returns + ------- + Entity + The entity passed Raises ------ UserWarning If neither a `parent` parameter, nor the `id`, nor `name` parameter is passed to this method. - """ pid = id @@ -510,8 +535,9 @@ class Entity(object): return self - def has_parent(self, parent, recursive=True, - check_name=True, check_id=False): + def has_parent(self, parent: Union[Entity, str, int], + recursive: bool = True, check_name: bool = True, + check_id: bool = False) -> bool: """Checks if this entity has a given parent. If 'check_name' and 'check_id' are both False, test for identity @@ -519,11 +545,21 @@ class Entity(object): check. Note that, if checked, name or ID should not be None, lest the check fail. - @param parent: Check for this parent. - @param recursive: Whether to check recursively. - @param check_name: Whether to use the name for ancestry check. - @param check_id: Whether to use the ID for ancestry check. - @return: True if 'parent' is a true parent, False otherwise. + Parameters + ---------- + parent : Entity, str, int + Check for this parent + recursive : bool, optional + Whether to check recursively, by default True + check_name : bool, optional + Whether to use the name for ancestry check, by default True + check_id : bool, optional + Whether to use the ID for ancestry check, by default False + + Returns + ------- + bool + True if 'parent' is a true parent, False otherwise """ if recursive: @@ -534,14 +570,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