Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc51f938

Browse files
committed
Add types to objects _serialize() and _deserialize()
1 parent76bcd70 commitc51f938

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

‎git/objects/commit.py‎

Lines changed: 12 additions & 8 deletions
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-
fromtypingimportTuple,Union
87
fromgitdbimportIStream
98
fromgit.utilimport (
109
hex_to_bin,
@@ -37,6 +36,11 @@
3736
fromioimportBytesIO
3837
importlogging
3938

39+
fromtypingimportList,Tuple,Union,TYPE_CHECKING
40+
41+
ifTYPE_CHECKING:
42+
fromgit.repoimportRepo
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-
returncommit.parents
142+
def_get_intermediate_items(cls,commit:'Commit')->Tuple['Commit', ...]:# type: ignore ## cos overriding super
143+
returntuple(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
forpinself.parents:
@@ -473,7 +477,7 @@ def _serialize(self, stream):
473477
# END handle encoding
474478
returnself
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
whilebuf:
515519
ifbuf[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
elifbuf[0:7]==b"gpgsig ":
519523
sig=buf[buf.find(b' ')+1:]+b"\n"

‎git/objects/tree.py‎

Lines changed: 13 additions & 3 deletions
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-
fromtypingimportIterable,Iterator,Tuple,Union,cast
76
fromgit.utilimportjoin_path
87
importgit.diffasdiff
98
fromgit.utilimportto_bin_sha
@@ -18,6 +17,17 @@
1817
tree_to_stream
1918
)
2019

20+
21+
# typing -------------------------------------------------
22+
23+
fromtypingimportIterable,Iterator,Tuple,Union,cast,TYPE_CHECKING
24+
25+
ifTYPE_CHECKING:
26+
fromioimportBytesIO
27+
28+
#--------------------------------------------------------
29+
30+
2131
cmp=lambdaa,b: (a>b)- (a<b)
2232

2333
__all__= ("TreeModifier","Tree")
@@ -321,15 +331,15 @@ def __contains__(self, item):
321331
def__reversed__(self):
322332
returnreversed(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
returnself
331341

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

‎git/objects/util.py‎

Lines changed: 4 additions & 4 deletions
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
fromgit.utilimport (
109
IterableList,
1110
Actor
@@ -20,9 +19,10 @@
2019
fromdatetimeimportdatetime,timedelta,tzinfo
2120

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

2524
ifTYPE_CHECKING:
25+
fromioimportBytesIO
2626
from .submodule.baseimportSubmodule
2727
from .commitimportCommit
2828
from .blobimportBlob
@@ -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
raiseNotImplementedError("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)

[8]ページ先頭

©2009-2025 Movatter.jp