summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYobmod <yobmod@gmail.com>2021-05-20 21:34:33 +0100
committerYobmod <yobmod@gmail.com>2021-05-20 21:34:33 +0100
commitc51f93823d46f0882b49822ce6f9e668228e5b8d (patch)
tree8f435f0565b3834aded4cff60e841f28618038bc
parent76bcd7081265f1d72fcc3101bfda62c67d8a7f32 (diff)
downloadgitpython-c51f93823d46f0882b49822ce6f9e668228e5b8d.tar.gz
Add types to objects _serialize() and _deserialize()
-rw-r--r--git/objects/commit.py20
-rw-r--r--git/objects/tree.py16
-rw-r--r--git/objects/util.py8
3 files changed, 29 insertions, 15 deletions
diff --git a/git/objects/commit.py b/git/objects/commit.py
index 228e897e..26db6e36 100644
--- a/git/objects/commit.py
+++ b/git/objects/commit.py
@@ -4,7 +4,6 @@
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
-from typing import Tuple, Union
from gitdb import IStream
from git.util import (
hex_to_bin,
@@ -37,6 +36,11 @@ import os
from io import BytesIO
import logging
+from typing import List, Tuple, Union, TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from git.repo import Repo
+
log = logging.getLogger('git.objects.commit')
log.addHandler(logging.NullHandler())
@@ -71,7 +75,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None,
committer=None, committed_date=None, committer_tz_offset=None,
- message=None, parents: Union[Tuple['Commit', ...], None] = None,
+ message=None, parents: Union[Tuple['Commit', ...], List['Commit'], None] = None,
encoding=None, gpgsig=None):
"""Instantiate a new Commit. All keyword arguments taking None as default will
be implicitly set on first query.
@@ -135,11 +139,11 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
self.gpgsig = gpgsig
@classmethod
- def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore
- return commit.parents
+ def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore ## cos overriding super
+ return tuple(commit.parents)
@classmethod
- def _calculate_sha_(cls, repo, commit):
+ def _calculate_sha_(cls, repo: 'Repo', commit: 'Commit') -> bytes:
'''Calculate the sha of a commit.
:param repo: Repo object the commit should be part of
@@ -432,7 +436,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
#{ Serializable Implementation
- def _serialize(self, stream):
+ def _serialize(self, stream: BytesIO) -> 'Commit':
write = stream.write
write(("tree %s\n" % self.tree).encode('ascii'))
for p in self.parents:
@@ -473,7 +477,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
# END handle encoding
return self
- def _deserialize(self, stream):
+ def _deserialize(self, stream: BytesIO) -> 'Commit':
""":param from_rev_list: if true, the stream format is coming from the rev-list command
Otherwise it is assumed to be a plain data stream from our object"""
readline = stream.readline
@@ -513,7 +517,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
buf = enc.strip()
while buf:
if buf[0:10] == b"encoding ":
- self.encoding = buf[buf.find(' ') + 1:].decode(
+ self.encoding = buf[buf.find(b' ') + 1:].decode(
self.encoding, 'ignore')
elif buf[0:7] == b"gpgsig ":
sig = buf[buf.find(b' ') + 1:] + b"\n"
diff --git a/git/objects/tree.py b/git/objects/tree.py
index 65c9be4c..29b2a684 100644
--- a/git/objects/tree.py
+++ b/git/objects/tree.py
@@ -3,7 +3,6 @@
#
# This module is part of GitPython and is released under
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
-from typing import Iterable, Iterator, Tuple, Union, cast
from git.util import join_path
import git.diff as diff
from git.util import to_bin_sha
@@ -18,6 +17,17 @@ from .fun import (
tree_to_stream
)
+
+# typing -------------------------------------------------
+
+from typing import Iterable, Iterator, Tuple, Union, cast, TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from io import BytesIO
+
+#--------------------------------------------------------
+
+
cmp = lambda a, b: (a > b) - (a < b)
__all__ = ("TreeModifier", "Tree")
@@ -321,7 +331,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
def __reversed__(self):
return reversed(self._iter_convert_to_object(self._cache))
- def _serialize(self, stream):
+ def _serialize(self, stream: 'BytesIO') -> 'Tree':
"""Serialize this tree into the stream. Please note that we will assume
our tree data to be in a sorted state. If this is not the case, serialization
will not generate a correct tree representation as these are assumed to be sorted
@@ -329,7 +339,7 @@ class Tree(IndexObject, diff.Diffable, util.Traversable, util.Serializable):
tree_to_stream(self._cache, stream.write)
return self
- def _deserialize(self, stream):
+ def _deserialize(self, stream: 'BytesIO') -> 'Tree':
self._cache = tree_entries_from_data(stream.read())
return self
diff --git a/git/objects/util.py b/git/objects/util.py
index 106bab0e..b94e9f12 100644
--- a/git/objects/util.py
+++ b/git/objects/util.py
@@ -5,7 +5,6 @@
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
"""Module for general utility functions"""
-
from git.util import (
IterableList,
Actor
@@ -20,9 +19,10 @@ import calendar
from datetime import datetime, timedelta, tzinfo
# typing ------------------------------------------------------------
-from typing import Any, Callable, Deque, IO, Iterator, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast, overload
+from typing import (Any, Callable, Deque, IO, Iterator, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast, overload)
if TYPE_CHECKING:
+ from io import BytesIO
from .submodule.base import Submodule
from .commit import Commit
from .blob import Blob
@@ -412,14 +412,14 @@ class Serializable(object):
"""Defines methods to serialize and deserialize objects from and into a data stream"""
__slots__ = ()
- def _serialize(self, stream):
+ def _serialize(self, stream: 'BytesIO') -> 'Serializable':
"""Serialize the data of this object into the given data stream
:note: a serialized object would ``_deserialize`` into the same object
:param stream: a file-like object
:return: self"""
raise NotImplementedError("To be implemented in subclass")
- def _deserialize(self, stream):
+ def _deserialize(self, stream: 'BytesIO') -> 'Serializable':
"""Deserialize all information regarding this object from the stream
:param stream: a file-like object
:return: self"""