Skip to content
Snippets Groups Projects
Commit e23a9087 authored by Joscha Schmiedt's avatar Joscha Schmiedt
Browse files

Add type hints to versioning.py

parent 3477e4f4
No related branches found
No related tags found
2 merge requests!143Release 0.15.0,!135Add and fix more type hints
...@@ -26,10 +26,15 @@ ...@@ -26,10 +26,15 @@
Currently this module defines nothing but a single class, `Version`. Currently this module defines nothing but a single class, `Version`.
""" """
from __future__ import absolute_import from __future__ import absolute_import, annotations
from .utils import xml2str from .utils import xml2str
from lxml import etree from lxml import etree
from typing import TYPE_CHECKING
import sys
if TYPE_CHECKING and sys.version_info > (3, 7):
from typing import Optional, List, Union, Literal
class Version(): class Version():
"""The version of an entity. """The version of an entity.
...@@ -95,9 +100,11 @@ class Version(): ...@@ -95,9 +100,11 @@ class Version():
""" """
# pylint: disable=redefined-builtin # pylint: disable=redefined-builtin
def __init__(self, id=None, date=None, username=None, realm=None, def __init__(self, id: Optional[str] = None, date: Optional[str] = None,
predecessors=None, successors=None, is_head=False, username: Optional[str] = None, realm: Optional[str] = None,
is_complete_history=False): predecessors: Optional[List[Version]] = None, successors: Optional[List[Version]] = None,
is_head: Union[bool, str] = False,
is_complete_history: Union[bool, str] = False):
"""Typically the `predecessors` or `successors` should not "link back" to an existing Version """Typically the `predecessors` or `successors` should not "link back" to an existing Version
object.""" object."""
self.id = id self.id = id
...@@ -109,7 +116,7 @@ object.""" ...@@ -109,7 +116,7 @@ object."""
self.is_head = str(is_head).lower() == "true" self.is_head = str(is_head).lower() == "true"
self.is_complete_history = str(is_complete_history).lower() == "true" self.is_complete_history = str(is_complete_history).lower() == "true"
def get_history(self): def get_history(self) -> List[Version]:
""" Returns a flat list of Version instances representing the history """ Returns a flat list of Version instances representing the history
of the entity. of the entity.
...@@ -126,7 +133,7 @@ object.""" ...@@ -126,7 +133,7 @@ object."""
------- -------
list of Version list of Version
""" """
versions = [] versions: List[Version] = []
for p in self.predecessors: for p in self.predecessors:
# assuming that predecessors don't have any successors # assuming that predecessors don't have any successors
versions = p.get_history() versions = p.get_history()
...@@ -137,7 +144,7 @@ object.""" ...@@ -137,7 +144,7 @@ object."""
versions.extend(s.get_history()) versions.extend(s.get_history())
return versions return versions
def to_xml(self, tag="Version"): def to_xml(self, tag: str = "Version") -> etree._Element:
"""Serialize this version to xml. """Serialize this version to xml.
The tag name is 'Version' per default. But since this method is called The tag name is 'Version' per default. But since this method is called
...@@ -184,7 +191,7 @@ object.""" ...@@ -184,7 +191,7 @@ object."""
return xml2str(self.to_xml()) return xml2str(self.to_xml())
@staticmethod @staticmethod
def from_xml(xml): def from_xml(xml: etree._Element) -> Version:
"""Parse a version object from a 'Version' xml element. """Parse a version object from a 'Version' xml element.
Parameters Parameters
...@@ -199,11 +206,22 @@ object.""" ...@@ -199,11 +206,22 @@ object."""
version : Version version : Version
a new version instance a new version instance
""" """
predecessors = [Version.from_xml(p) for p in xml if p.tag.lower() == "predecessor"] predecessors = [Version.from_xml(
successors = [Version.from_xml(s) for s in xml if s.tag.lower() == "successor"] p) for p in xml if p.tag.lower() == "predecessor"]
successors = [Version.from_xml(s)
for s in xml if s.tag.lower() == "successor"]
is_head = xml.get("head")
if is_head is None:
raise ValueError(f"Version head is missing from xml:{str(xml)}")
is_complete_history = xml.get("completeHistory")
if is_complete_history is None:
raise ValueError(
f"Version completeHistory is missing from xml:{str(xml)}")
return Version(id=xml.get("id"), date=xml.get("date"), return Version(id=xml.get("id"), date=xml.get("date"),
is_head=xml.get("head"), is_head=is_head,
is_complete_history=xml.get("completeHistory"), is_complete_history=is_complete_history,
username=xml.get("username"), realm=xml.get("realm"), username=xml.get("username"), realm=xml.get("realm"),
predecessors=predecessors, successors=successors) predecessors=predecessors, successors=successors)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment