Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
e23a9087
Commit
e23a9087
authored
1 year ago
by
Joscha Schmiedt
Browse files
Options
Downloads
Patches
Plain Diff
Add type hints to versioning.py
parent
3477e4f4
No related branches found
No related tags found
2 merge requests
!143
Release 0.15.0
,
!135
Add and fix more type hints
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/linkahead/common/versioning.py
+30
-12
30 additions, 12 deletions
src/linkahead/common/versioning.py
with
30 additions
and
12 deletions
src/linkahead/common/versioning.py
+
30
−
12
View file @
e23a9087
...
@@ -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
(
"
complete
H
istory
"
)
,
is_complete_history
=
is_
complete
_h
istory
,
username
=
xml
.
get
(
"
username
"
),
realm
=
xml
.
get
(
"
realm
"
),
username
=
xml
.
get
(
"
username
"
),
realm
=
xml
.
get
(
"
realm
"
),
predecessors
=
predecessors
,
successors
=
successors
)
predecessors
=
predecessors
,
successors
=
successors
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment