Skip to content

Commit 2d2ff03

Browse files
authored
Merge pull request #1279 from Yobmod/main
Finish typing object, improve verious other types.
2 parents 703280b + 5d7b8ba commit 2d2ff03

File tree

16 files changed

+255
-148
lines changed

16 files changed

+255
-148
lines changed

git/index/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def write_tree(self) -> Tree:
568568
# note: additional deserialization could be saved if write_tree_from_cache
569569
# would return sorted tree entries
570570
root_tree = Tree(self.repo, binsha, path='')
571-
root_tree._cache = tree_items
571+
root_tree._cache = tree_items # type: ignore
572572
return root_tree
573573

574574
def _process_diff_args(self, args: List[Union[str, diff.Diffable, object]]

git/index/fun.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
from typing import (Dict, IO, List, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast)
5555

56-
from git.types import PathLike
56+
from git.types import PathLike, TypeGuard
5757

5858
if TYPE_CHECKING:
5959
from .base import IndexFile
@@ -185,11 +185,17 @@ def read_header(stream: IO[bytes]) -> Tuple[int, int]:
185185
def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, int]:
186186
""":return: Key suitable to be used for the index.entries dictionary
187187
:param entry: One instance of type BaseIndexEntry or the path and the stage"""
188+
189+
def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
190+
return isinstance(entry, tuple) and len(entry) == 2
191+
188192
if len(entry) == 1:
189-
entry_first = cast(BaseIndexEntry, entry[0]) # type: BaseIndexEntry
193+
entry_first = entry[0]
194+
assert isinstance(entry_first, BaseIndexEntry)
190195
return (entry_first.path, entry_first.stage)
191196
else:
192-
entry = cast(Tuple[PathLike, int], tuple(entry))
197+
# entry = tuple(entry)
198+
assert is_entry_tuple(entry)
193199
return entry
194200
# END handle entry
195201

@@ -293,7 +299,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
293299
# finally create the tree
294300
sio = BytesIO()
295301
tree_to_stream(tree_items, sio.write) # converts bytes of each item[0] to str
296-
tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items) # type: List[Tuple[str, int, str]]
302+
tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items)
297303
sio.seek(0)
298304

299305
istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))

git/objects/commit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from git.util import (
99
hex_to_bin,
1010
Actor,
11-
Iterable,
11+
IterableObj,
1212
Stats,
1313
finalize_process
1414
)
@@ -47,7 +47,7 @@
4747
__all__ = ('Commit', )
4848

4949

50-
class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
50+
class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):
5151

5252
"""Wraps a git Commit object.
5353

git/objects/fun.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
"""Module with functions which are supposed to be as fast as possible"""
22
from stat import S_ISDIR
3+
34
from git.compat import (
45
safe_decode,
56
defenc
67
)
78

9+
# typing ----------------------------------------------
10+
11+
from typing import List, Tuple
12+
13+
14+
# ---------------------------------------------------
15+
16+
817
__all__ = ('tree_to_stream', 'tree_entries_from_data', 'traverse_trees_recursive',
918
'traverse_tree_recursive')
1019

@@ -38,7 +47,7 @@ def tree_to_stream(entries, write):
3847
# END for each item
3948

4049

41-
def tree_entries_from_data(data):
50+
def tree_entries_from_data(data: bytes) -> List[Tuple[bytes, int, str]]:
4251
"""Reads the binary representation of a tree and returns tuples of Tree items
4352
:param data: data block with tree data (as bytes)
4453
:return: list(tuple(binsha, mode, tree_relative_path), ...)"""
@@ -72,8 +81,8 @@ def tree_entries_from_data(data):
7281

7382
# default encoding for strings in git is utf8
7483
# Only use the respective unicode object if the byte stream was encoded
75-
name = data[ns:i]
76-
name = safe_decode(name)
84+
name_bytes = data[ns:i]
85+
name = safe_decode(name_bytes)
7786

7887
# byte is NULL, get next 20
7988
i += 1

git/objects/submodule/base.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import os
55
import stat
6-
from typing import List
76
from unittest import SkipTest
87
import uuid
98

@@ -27,12 +26,13 @@
2726
from git.objects.base import IndexObject, Object
2827
from git.objects.util import Traversable
2928
from git.util import (
30-
Iterable,
29+
IterableObj,
3130
join_path_native,
3231
to_native_path_linux,
3332
RemoteProgress,
3433
rmtree,
35-
unbare_repo
34+
unbare_repo,
35+
IterableList
3636
)
3737
from git.util import HIDE_WINDOWS_KNOWN_ERRORS
3838

@@ -47,6 +47,11 @@
4747
)
4848

4949

50+
# typing ----------------------------------------------------------------------
51+
52+
53+
# -----------------------------------------------------------------------------
54+
5055
__all__ = ["Submodule", "UpdateProgress"]
5156

5257

@@ -74,7 +79,7 @@ class UpdateProgress(RemoteProgress):
7479
# IndexObject comes via util module, its a 'hacky' fix thanks to pythons import
7580
# mechanism which cause plenty of trouble of the only reason for packages and
7681
# modules is refactoring - subpackages shouldn't depend on parent packages
77-
class Submodule(IndexObject, Iterable, Traversable):
82+
class Submodule(IndexObject, IterableObj, Traversable):
7883

7984
"""Implements access to a git submodule. They are special in that their sha
8085
represents a commit in the submodule's repository which is to be checked out
@@ -136,12 +141,12 @@ def _set_cache_(self, attr):
136141
# END handle attribute name
137142

138143
@classmethod
139-
def _get_intermediate_items(cls, item: 'Submodule') -> List['Submodule']: # type: ignore
144+
def _get_intermediate_items(cls, item: 'Submodule') -> IterableList['Submodule']:
140145
""":return: all the submodules of our module repository"""
141146
try:
142147
return cls.list_items(item.module())
143148
except InvalidGitRepositoryError:
144-
return []
149+
return IterableList('')
145150
# END handle intermediate items
146151

147152
@classmethod
@@ -1153,7 +1158,7 @@ def name(self):
11531158
"""
11541159
return self._name
11551160

1156-
def config_reader(self):
1161+
def config_reader(self) -> SectionConstraint:
11571162
"""
11581163
:return: ConfigReader instance which allows you to qurey the configuration values
11591164
of this submodule, as provided by the .gitmodules file
@@ -1163,7 +1168,7 @@ def config_reader(self):
11631168
:raise IOError: If the .gitmodules file/blob could not be read"""
11641169
return self._config_parser_constrained(read_only=True)
11651170

1166-
def children(self):
1171+
def children(self) -> IterableList['Submodule']:
11671172
"""
11681173
:return: IterableList(Submodule, ...) an iterable list of submodules instances
11691174
which are children of this submodule or 0 if the submodule is not checked out"""

git/objects/submodule/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
from io import BytesIO
55
import weakref
66

7+
from typing import TYPE_CHECKING
8+
9+
if TYPE_CHECKING:
10+
from .base import Submodule
11+
712
__all__ = ('sm_section', 'sm_name', 'mkhead', 'find_first_remote_branch',
813
'SubmoduleConfigParser')
914

@@ -60,12 +65,12 @@ def __init__(self, *args, **kwargs):
6065
super(SubmoduleConfigParser, self).__init__(*args, **kwargs)
6166

6267
#{ Interface
63-
def set_submodule(self, submodule):
68+
def set_submodule(self, submodule: 'Submodule') -> None:
6469
"""Set this instance's submodule. It must be called before
6570
the first write operation begins"""
6671
self._smref = weakref.ref(submodule)
6772

68-
def flush_to_index(self):
73+
def flush_to_index(self) -> None:
6974
"""Flush changes in our configuration file to the index"""
7075
assert self._smref is not None
7176
# should always have a file here

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy