diff --git a/src/caosdb/common/models.py b/src/caosdb/common/models.py index 4a6dbd6828fddd2863ed5daa3aea7e7678d4aa57..cca611a4cd470a8e5303fd2418a40ba35e447e01 100644 --- a/src/caosdb/common/models.py +++ b/src/caosdb/common/models.py @@ -71,6 +71,8 @@ FIX = "FIX" ALL = "ALL" NONE = "NONE" +TA_add_properties = Union[Literal['ALL', 'FIX', 'NONE'], None] + class Entity(object): @@ -341,9 +343,9 @@ class Entity(object): return self - def add_property(self, property: Union[int, str, Type[Entity], None] = None, - value: Union[str, int, Type[Entity], None] = None, - **kwargs) -> Type[Entity]: # @ReservedAssignment + def add_property(self, property: Union[int, str, Entity, None] = None, + value: Union[str, int, Entity, None] = None, + **kwargs) -> 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 @@ -354,14 +356,14 @@ class Entity(object): Parameters ---------- - property : int, str, Type[Entity], optional + property : int, str, Entity, optional An identifying parameter (name, id or abstract property), by default None - value : str, int, Type[Entity], optional + value : str, int, Entity, optional The value of the new property, by default None Returns ------- - Type[Entity] + Entity The entity passed Raises @@ -423,7 +425,7 @@ class Entity(object): 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) -> Type[Entity]: + 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. @@ -441,7 +443,7 @@ class Entity(object): Returns ------- - Type[Entity] + Entity The entity passed """ @@ -453,13 +455,13 @@ class Entity(object): return self - def add_parent(self, parent: Union[Type[Entity], int, str, None] = None, - **kwargs) -> Type[Entity]: # @ReservedAssignment + def add_parent(self, parent: Union[Entity, int, str, None] = None, + **kwargs) -> Entity: # @ReservedAssignment """Add a parent to this entity. Parameters ---------- - parent :Type[Entity], int, str, optional + parent :Entity, int, str, optional The parent entity, either specified by the Entity object itself, or its id or its name, by default None **kwargs : dict, optional @@ -485,7 +487,7 @@ class Entity(object): Returns ------- - Type[Entity] + Entity The entity passed Raises @@ -520,7 +522,7 @@ class Entity(object): return self - def has_parent(self, parent: Union[Type[Entity], str, int], + 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. @@ -532,7 +534,7 @@ class Entity(object): Parameters ---------- - parent : Type[Entity], str, int + parent : Entity, str, int Check for this parent recursive : bool, optional Whether to check recursively, by default True @@ -565,22 +567,22 @@ class Entity(object): return name_result and id_result - def get_parents(self) -> Type[list]: + def get_parents(self) -> list: """Get all parents of this entity. Returns ------- - Type[list] + list Parents """ return self.parents - def get_parents_recursively(self) -> Type[list]: + def get_parents_recursively(self) -> list: """Get all ancestors of this entity. Returns ------- - Type[list] + list list of Entities """ all_parents = _Parents() @@ -588,7 +590,7 @@ class Entity(object): return all_parents - def _get_parent_recursively(self, all_parents: Type[list]) -> None: + def _get_parent_recursively(self, all_parents: list) -> None: """Get all ancestors with a little helper. Important: As a side effect of this method, the ancestors are @@ -596,7 +598,7 @@ class Entity(object): Parameters ---------- - all_parents : Type[list] + all_parents : list The added parents so far """ for parent in self.parents: @@ -606,19 +608,19 @@ class Entity(object): all_parents.append(w_parent) w_parent._get_parent_recursively(all_parents) - def get_parent(self, key: Union[Type[Entity], int, str]) -> Type[Entity]: + def get_parent(self, key: Union[Entity, int, str]) -> Optional[Entity]: """Return the first parent matching the key or None if no match exists. Parameters ---------- - key : Type[Entity], int, str + key : Entity, int, str The id, Entity, or name of the parent that should be returned. If an Entity is given, its id or its name is used to find a matching parent. Returns ------- - Type[Entity] + Entity The first parent of this entity that matches the given id, entity, or name. """ @@ -645,22 +647,22 @@ class Entity(object): return None - def get_properties(self) -> Type[list]: + def get_properties(self) -> list: """Get all properties of this entity. Returns ------- - Type[list] + list _Properties(list) """ return self.properties - def get_property(self, pattern: Union[Type[Entity], str, int]) -> Property: + def get_property(self, pattern: Union[Entity, str, int]) -> Optional[Property]: """Return the first matching property or None. Parameters ---------- - pattern : Type[Entity], str, int + pattern : Entity, str, int The name or id to look for (case-insensitive) or an Entity where the name or id is used to match the properites of this instance. @@ -811,7 +813,7 @@ class Entity(object): return row - def get_messages(self) -> list: + def get_messages(self) -> dict: """Get all messages of this entity. Returns @@ -821,7 +823,7 @@ class Entity(object): """ return self.messages - def get_warnings(self) -> list: + def get_warnings(self) -> dict: """Get all warning messages of this entity. Returns @@ -856,13 +858,13 @@ class Entity(object): return ret - def get_errors_deep(self, roots: Type[Entity] = None) -> list: + def get_errors_deep(self, roots: Entity = None) -> list: """Get all error messages of this entity and all sub-entities / parents / properties. Parameters ---------- - roots : Type[Entity], optional + roots : Entity, optional Used to find sub-entities / parents / properties recursivly, by default None @@ -900,7 +902,7 @@ class Entity(object): return False def to_xml(self, xml: Optional[etree._Element] = None, - add_properties: Literal['ALL', 'FIX', 'NONE'] = ALL, + add_properties: TA_add_properties = ALL, local_serialization: bool = False) -> etree._Element: """Generate an xml representation of this entity. If the parameter `xml` is given, all attributes, parents, properties, and messages of this @@ -1130,11 +1132,11 @@ class Entity(object): self.acl = Entity(name=self.name, id=self.id).retrieve( flags={"ACL": None}).acl - def update_acl(self) -> Type[Entity]: + def update_acl(self) -> Entity: """Update acl Returns ------- - Type[Entity] + Entity The updated Entity Raises @@ -1617,7 +1619,7 @@ class QueryTemplate(): Returns ------- Container - + """ return Container().append(self).delete( raise_exception_on_error=raise_exception_on_error)[0] @@ -1724,14 +1726,17 @@ class Parent(Entity): def affiliation(self, affiliation): self.__affiliation = affiliation - def __init__(self, id=None, name=None, description=None, inheritance=None): # @ReservedAssignment + def __init__(self, id: Optional[int] = None, name: Optional[str] = None, + description: Optional[str] = None, + inheritance: Optional[str] = None) -> None: # @ReservedAssignment Entity.__init__(self, id=id, name=name, description=description) if inheritance is not None: self.set_flag("inheritance", inheritance) self.__affiliation = None - def to_xml(self, xml=None, add_properties=None): + def to_xml(self, xml: Optional[etree._Element] = None, + add_properties: TA_add_properties = None) -> etree._Element: if xml is None: xml = etree.Element("Parent") @@ -1750,7 +1755,9 @@ class Property(Entity): """CaosDB's Property object.""" - def add_property(self, property=None, value=None, **kwargs): # @ReservedAssignment + def add_property(self, property: Union[int, str, Entity, None] = None, + value: Union[str, int, Entity, None] = None, + **kwargs) -> Entity: # @ReservedAssignment copy_kwargs = kwargs.copy() if 'importance' not in copy_kwargs: @@ -1764,23 +1771,26 @@ class Property(Entity): return super(Property, self).add_property( property=property, value=value, **copy_kwargs) - def add_parent(self, parent=None, **kwargs): + def add_parent(self, parent: Union[Entity, str, int, None] = None, **kwargs) -> Entity: """Add a parent Entity to this Property. Parameters ---------- - parent : Entity or int or str or None, optional - The parent entity + parent : Entity, str, int, optional + The parent entity, by default None **kwargs : dict, optional Additional keyword arguments specifying the parent Entity by id or name, and specifying the inheritance level. See :py:meth:`Entity.add_parent` for more information. Note that by default, `inheritance` is set to ``fix``. + Returns + ------- + Entity + See Also -------- Entity.add_parent - """ copy_kwargs = kwargs.copy()