Skip to content

Commit c51f938

Browse files
committed
Add types to objects _serialize() and _deserialize()
1 parent 76bcd70 commit c51f938

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

git/objects/commit.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66

7-
from typing import Tuple, Union
87
from gitdb import IStream
98
from git.util import (
109
hex_to_bin,
@@ -37,6 +36,11 @@
3736
from io import BytesIO
3837
import logging
3938

39+
from typing import List, Tuple, Union, TYPE_CHECKING
40+
41+
if TYPE_CHECKING:
42+
from git.repo import Repo
43+
4044
log = logging.getLogger('git.objects.commit')
4145
log.addHandler(logging.NullHandler())
4246

@@ -71,7 +75,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
7175

7276
def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None,
7377
committer=None, committed_date=None, committer_tz_offset=None,
74-
message=None, parents: Union[Tuple['Commit', ...], None] = None,
78+
message=None, parents: Union[Tuple['Commit', ...], List['Commit'], None] = None,
7579
encoding=None, gpgsig=None):
7680
"""Instantiate a new Commit. All keyword arguments taking None as default will
7781
be implicitly set on first query.
@@ -135,11 +139,11 @@ def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, aut
135139
self.gpgsig = gpgsig
136140

137141
@classmethod
138-
def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore
139-
return commit.parents
142+
def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore ## cos overriding super
143+
return tuple(commit.parents)
140144

141145
@classmethod
142-
def _calculate_sha_(cls, repo, commit):
146+
def _calculate_sha_(cls, repo: 'Repo', commit: 'Commit') -> bytes:
143147
'''Calculate the sha of a commit.
144148
145149
:param repo: Repo object the commit should be part of
@@ -432,7 +436,7 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False,
432436

433437
#{ Serializable Implementation
434438

435-
def _serialize(self, stream):
439+
def _serialize(self, stream: BytesIO) -> 'Commit':
436440
write = stream.write
437441
write(("tree %s\n" % self.tree).encode('ascii'))
438442
for p in self.parents:
@@ -473,7 +477,7 @@ def _serialize(self, stream):
473477
# END handle encoding
474478
return self
475479

476-
def _deserialize(self, stream):
480+
def _deserialize(self, stream: BytesIO) -> 'Commit':
477481
""":param from_rev_list: if true, the stream format is coming from the rev-list command
478482
Otherwise it is assumed to be a plain data stream from our object"""
479483
readline = stream.readline
@@ -513,7 +517,7 @@ def _deserialize(self, stream):
513517
buf = enc.strip()
514518
while buf:
515519
if buf[0:10] == b"encoding ":
516-
self.encoding = buf[buf.find(' ') + 1:].decode(
520+
self.encoding = buf[buf.find(b' ') + 1:].decode(
517521
self.encoding, 'ignore')
518522
elif buf[0:7] == b"gpgsig ":
519523
sig = buf[buf.find(b' ') + 1:] + b"\n"

git/objects/tree.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
6-
from typing import Iterable, Iterator, Tuple, Union, cast
76
from git.util import join_path
87
import git.diff as diff
98
from git.util import to_bin_sha
@@ -18,6 +17,17 @@
1817
tree_to_stream
1918
)
2019

20+
21+
# typing -------------------------------------------------
22+
23+
from typing import Iterable, Iterator, Tuple, Union, cast, TYPE_CHECKING
24+
25+
if TYPE_CHECKING:
26+
from io import BytesIO
27+
28+
#--------------------------------------------------------
29+
30+
2131
cmp = lambda a, b: (a > b) - (a < b)
2232

2333
__all__ = ("TreeModifier", "Tree")
@@ -321,15 +331,15 @@ def __contains__(self, item):
321331
def __reversed__(self):
322332
return reversed(self._iter_convert_to_object(self._cache))
323333

324-
def _serialize(self, stream):
334+
def _serialize(self, stream: 'BytesIO') -> 'Tree':
325335
"""Serialize this tree into the stream. Please note that we will assume
326336
our tree data to be in a sorted state. If this is not the case, serialization
327337
will not generate a correct tree representation as these are assumed to be sorted
328338
by algorithms"""
329339
tree_to_stream(self._cache, stream.write)
330340
return self
331341

332-
def _deserialize(self, stream):
342+
def _deserialize(self, stream: 'BytesIO') -> 'Tree':
333343
self._cache = tree_entries_from_data(stream.read())
334344
return self
335345

git/objects/util.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
"""Module for general utility functions"""
77

8-
98
from git.util import (
109
IterableList,
1110
Actor
@@ -20,9 +19,10 @@
2019
from datetime import datetime, timedelta, tzinfo
2120

2221
# typing ------------------------------------------------------------
23-
from typing import Any, Callable, Deque, IO, Iterator, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast, overload
22+
from typing import (Any, Callable, Deque, IO, Iterator, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast, overload)
2423

2524
if TYPE_CHECKING:
25+
from io import BytesIO
2626
from .submodule.base import Submodule
2727
from .commit import Commit
2828
from .blob import Blob
@@ -412,14 +412,14 @@ class Serializable(object):
412412
"""Defines methods to serialize and deserialize objects from and into a data stream"""
413413
__slots__ = ()
414414

415-
def _serialize(self, stream):
415+
def _serialize(self, stream: 'BytesIO') -> 'Serializable':
416416
"""Serialize the data of this object into the given data stream
417417
:note: a serialized object would ``_deserialize`` into the same object
418418
:param stream: a file-like object
419419
:return: self"""
420420
raise NotImplementedError("To be implemented in subclass")
421421

422-
def _deserialize(self, stream):
422+
def _deserialize(self, stream: 'BytesIO') -> 'Serializable':
423423
"""Deserialize all information regarding this object from the stream
424424
:param stream: a file-like object
425425
:return: self"""

0 commit comments

Comments
 (0)