From 5402a166a4971512f9d513bf36159dead9672ae9 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 20 May 2021 20:44:53 +0100 Subject: Add types to objects _get_intermediate_items() --- git/objects/commit.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index 45e6d772..6d3f0bac 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -4,6 +4,7 @@ # 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, @@ -70,7 +71,8 @@ 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=None, encoding=None, gpgsig=None): + message=None, parents: Union[Tuple['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. @@ -133,7 +135,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): self.gpgsig = gpgsig @classmethod - def _get_intermediate_items(cls, commit): + def _get_intermediate_items(cls, commit: 'Commit') -> Tuple['Commit', ...]: # type: ignore return commit.parents @classmethod @@ -477,7 +479,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): readline = stream.readline self.tree = Tree(self.repo, hex_to_bin(readline().split()[1]), Tree.tree_id << 12, '') - self.parents = [] + self.parents_list = [] # List['Commit'] next_line = None while True: parent_line = readline() @@ -485,9 +487,9 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): next_line = parent_line break # END abort reading parents - self.parents.append(type(self)(self.repo, hex_to_bin(parent_line.split()[-1].decode('ascii')))) + self.parents_list.append(type(self)(self.repo, hex_to_bin(parent_line.split()[-1].decode('ascii')))) # END for each parent line - self.parents = tuple(self.parents) + self.parents = tuple(self.parents_list) # type: Tuple['Commit', ...] # we don't know actual author encoding before we have parsed it, so keep the lines around author_line = next_line -- cgit v1.2.1 From ce8cc4a6123a3ea11fc4e35416d93a8bd68cfd65 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 20 May 2021 21:06:09 +0100 Subject: Add types to commit.py spit parents into list and tuple types --- git/objects/commit.py | 1 + 1 file changed, 1 insertion(+) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index 6d3f0bac..b95f4c65 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -129,6 +129,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): self.message = message if parents is not None: self.parents = parents + self.parents_list = list(parents) if encoding is not None: self.encoding = encoding if gpgsig is not None: -- cgit v1.2.1 From 76bcd7081265f1d72fcc3101bfda62c67d8a7f32 Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 20 May 2021 21:10:33 +0100 Subject: Add types to commit.py undo --- git/objects/commit.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'git/objects/commit.py') diff --git a/git/objects/commit.py b/git/objects/commit.py index b95f4c65..228e897e 100644 --- a/git/objects/commit.py +++ b/git/objects/commit.py @@ -129,7 +129,6 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): self.message = message if parents is not None: self.parents = parents - self.parents_list = list(parents) if encoding is not None: self.encoding = encoding if gpgsig is not None: @@ -480,7 +479,7 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): readline = stream.readline self.tree = Tree(self.repo, hex_to_bin(readline().split()[1]), Tree.tree_id << 12, '') - self.parents_list = [] # List['Commit'] + self.parents = [] next_line = None while True: parent_line = readline() @@ -488,9 +487,9 @@ class Commit(base.Object, Iterable, Diffable, Traversable, Serializable): next_line = parent_line break # END abort reading parents - self.parents_list.append(type(self)(self.repo, hex_to_bin(parent_line.split()[-1].decode('ascii')))) + self.parents.append(type(self)(self.repo, hex_to_bin(parent_line.split()[-1].decode('ascii')))) # END for each parent line - self.parents = tuple(self.parents_list) # type: Tuple['Commit', ...] + self.parents = tuple(self.parents) # we don't know actual author encoding before we have parsed it, so keep the lines around author_line = next_line -- cgit v1.2.1 From c51f93823d46f0882b49822ce6f9e668228e5b8d Mon Sep 17 00:00:00 2001 From: Yobmod Date: Thu, 20 May 2021 21:34:33 +0100 Subject: Add types to objects _serialize() and _deserialize() --- git/objects/commit.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'git/objects/commit.py') 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" -- cgit v1.2.1