Skip to content

Commit fc93657

Browse files
committed
improve types according to test/test_repository_bare.py
1 parent 4dd179d commit fc93657

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

pygit2/_pygit2.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ class Repository:
766766
def _from_c(cls, ptr: 'GitRepositoryC', owned: bool) -> 'Repository': ...
767767
def __iter__(self) -> Iterator[Oid]: ...
768768
def __getitem__(self, key: str | Oid) -> Object: ...
769+
def __contains__(self, name: _OidArg) -> bool: ...
769770
def add_worktree(
770771
self, name: str, path: str | Path, ref: Reference = ...
771772
) -> Worktree: ...
@@ -976,6 +977,7 @@ class Repository:
976977
def raw_listall_references(self) -> list[bytes]: ...
977978
@property
978979
def raw_message(self) -> bytes: ...
980+
def read(self, oid: _OidArg) -> tuple[int, bytes]: ...
979981
def remove_message(self) -> None: ...
980982
def references_iterator_init(self) -> Iterator[Reference]: ...
981983
def references_iterator_next(
@@ -992,6 +994,7 @@ class Repository:
992994
def revert_commit(
993995
self, revert_commit: Commit, our_commit: Commit, mainline: int = 0
994996
) -> Index: ...
997+
def set_head(self, target: _OidArg) -> None: ...
995998
def set_ident(self, name: str, email: str) -> None: ...
996999
def set_odb(self, odb: Odb) -> None: ...
9971000
def set_refdb(self, refdb: Refdb) -> None: ...
@@ -1033,6 +1036,7 @@ class Repository:
10331036
def walk(
10341037
self, oid: _OidArg | None, sort_mode: SortMode = SortMode.NONE
10351038
) -> Walker: ...
1039+
def write(self, type: int, data: bytes) -> Oid: ...
10361040
def write_archive(
10371041
self,
10381042
treeish: str | Tree | Object | Oid,
@@ -1145,7 +1149,7 @@ class Worktree:
11451149
def discover_repository(
11461150
path: str, across_fs: bool = False, ceiling_dirs: str = ...
11471151
) -> str | None: ...
1148-
def hash(data: bytes) -> Oid: ...
1152+
def hash(data: bytes | str) -> Oid: ...
11491153
def hashfile(path: str) -> Oid: ...
11501154
def init_file_backend(path: str, flags: int = 0) -> object: ...
11511155
@overload

test/test_repository_bare.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import pathlib
2929
import sys
3030
import tempfile
31+
from pathlib import Path
3132

3233
import pytest
3334

3435
import pygit2
36+
from pygit2 import Branch, Commit, Oid, Repository
3537
from pygit2.enums import FileMode, ObjectType
3638

3739
from . import utils
@@ -43,23 +45,23 @@
4345
BLOB_OID = pygit2.Oid(raw=BLOB_RAW)
4446

4547

46-
def test_is_empty(barerepo):
48+
def test_is_empty(barerepo: Repository) -> None:
4749
assert not barerepo.is_empty
4850

4951

50-
def test_is_bare(barerepo):
52+
def test_is_bare(barerepo: Repository) -> None:
5153
assert barerepo.is_bare
5254

5355

54-
def test_head(barerepo):
56+
def test_head(barerepo: Repository) -> None:
5557
head = barerepo.head
5658
assert HEAD_SHA == head.target
5759
assert type(head) is pygit2.Reference
5860
assert not barerepo.head_is_unborn
5961
assert not barerepo.head_is_detached
6062

6163

62-
def test_set_head(barerepo):
64+
def test_set_head(barerepo: Repository) -> None:
6365
# Test setting a detached HEAD.
6466
barerepo.set_head(pygit2.Oid(hex=PARENT_SHA))
6567
assert barerepo.head.target == PARENT_SHA
@@ -69,9 +71,9 @@ def test_set_head(barerepo):
6971
assert barerepo.head.target == HEAD_SHA
7072

7173

72-
def test_read(barerepo):
74+
def test_read(barerepo: Repository) -> None:
7375
with pytest.raises(TypeError):
74-
barerepo.read(123)
76+
barerepo.read(123) # type: ignore
7577
utils.assertRaisesWithArg(KeyError, '1' * 40, barerepo.read, '1' * 40)
7678

7779
ab = barerepo.read(BLOB_OID)
@@ -87,7 +89,7 @@ def test_read(barerepo):
8789
assert (ObjectType.BLOB, b'a contents\n') == a3
8890

8991

90-
def test_write(barerepo):
92+
def test_write(barerepo: Repository) -> None:
9193
data = b'hello world'
9294
# invalid object type
9395
with pytest.raises(ValueError):
@@ -97,55 +99,57 @@ def test_write(barerepo):
9799
assert type(oid) is pygit2.Oid
98100

99101

100-
def test_contains(barerepo):
102+
def test_contains(barerepo: Repository) -> None:
101103
with pytest.raises(TypeError):
102-
123 in barerepo
104+
123 in barerepo # type: ignore
103105
assert BLOB_OID in barerepo
104106
assert BLOB_HEX in barerepo
105107
assert BLOB_HEX[:10] in barerepo
106108
assert ('a' * 40) not in barerepo
107109
assert ('a' * 20) not in barerepo
108110

109111

110-
def test_iterable(barerepo):
112+
def test_iterable(barerepo: Repository) -> None:
111113
oid = pygit2.Oid(hex=BLOB_HEX)
112114
assert oid in [obj for obj in barerepo]
113115

114116

115-
def test_lookup_blob(barerepo):
117+
def test_lookup_blob(barerepo: Repository) -> None:
116118
with pytest.raises(TypeError):
117-
barerepo[123]
119+
barerepo[123] # type: ignore
118120
assert barerepo[BLOB_OID].id == BLOB_HEX
119121
a = barerepo[BLOB_HEX]
120122
assert b'a contents\n' == a.read_raw()
121123
assert BLOB_HEX == a.id
122-
assert ObjectType.BLOB == a.type
124+
assert int(ObjectType.BLOB) == a.type
123125

124126

125-
def test_lookup_blob_prefix(barerepo):
127+
def test_lookup_blob_prefix(barerepo: Repository) -> None:
126128
a = barerepo[BLOB_HEX[:5]]
127129
assert b'a contents\n' == a.read_raw()
128130
assert BLOB_HEX == a.id
129-
assert ObjectType.BLOB == a.type
131+
assert int(ObjectType.BLOB) == a.type
130132

131133

132-
def test_lookup_commit(barerepo):
134+
def test_lookup_commit(barerepo: Repository) -> None:
133135
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
134136
commit = barerepo[commit_sha]
135137
assert commit_sha == commit.id
136-
assert ObjectType.COMMIT == commit.type
138+
assert int(ObjectType.COMMIT) == commit.type
139+
assert isinstance(commit, Commit)
137140
assert commit.message == (
138141
'Second test data commit.\n\nThis commit has some additional text.\n'
139142
)
140143

141144

142-
def test_lookup_commit_prefix(barerepo):
145+
def test_lookup_commit_prefix(barerepo: Repository) -> None:
143146
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
144147
commit_sha_prefix = commit_sha[:7]
145148
too_short_prefix = commit_sha[:3]
146149
commit = barerepo[commit_sha_prefix]
147150
assert commit_sha == commit.id
148-
assert ObjectType.COMMIT == commit.type
151+
assert int(ObjectType.COMMIT) == commit.type
152+
assert isinstance(commit, Commit)
149153
assert (
150154
'Second test data commit.\n\n'
151155
'This commit has some additional text.\n' == commit.message
@@ -154,14 +158,14 @@ def test_lookup_commit_prefix(barerepo):
154158
barerepo.__getitem__(too_short_prefix)
155159

156160

157-
def test_expand_id(barerepo):
161+
def test_expand_id(barerepo: Repository) -> None:
158162
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
159163
expanded = barerepo.expand_id(commit_sha[:7])
160164
assert commit_sha == expanded
161165

162166

163167
@utils.requires_refcount
164-
def test_lookup_commit_refcount(barerepo):
168+
def test_lookup_commit_refcount(barerepo: Repository) -> None:
165169
start = sys.getrefcount(barerepo)
166170
commit_sha = '5fe808e8953c12735680c257f56600cb0de44b10'
167171
commit = barerepo[commit_sha]
@@ -170,30 +174,30 @@ def test_lookup_commit_refcount(barerepo):
170174
assert start == end
171175

172176

173-
def test_get_path(barerepo_path):
177+
def test_get_path(barerepo_path: tuple[Repository, Path]) -> None:
174178
barerepo, path = barerepo_path
175179

176180
directory = pathlib.Path(barerepo.path).resolve()
177181
assert directory == path.resolve()
178182

179183

180-
def test_get_workdir(barerepo):
184+
def test_get_workdir(barerepo: Repository) -> None:
181185
assert barerepo.workdir is None
182186

183187

184-
def test_revparse_single(barerepo):
188+
def test_revparse_single(barerepo: Repository) -> None:
185189
parent = barerepo.revparse_single('HEAD^')
186190
assert parent.id == PARENT_SHA
187191

188192

189-
def test_hash(barerepo):
193+
def test_hash(barerepo: Repository) -> None:
190194
data = 'foobarbaz'
191195
hashed_sha1 = pygit2.hash(data)
192196
written_sha1 = barerepo.create_blob(data)
193197
assert hashed_sha1 == written_sha1
194198

195199

196-
def test_hashfile(barerepo):
200+
def test_hashfile(barerepo: Repository) -> None:
197201
data = 'bazbarfoo'
198202
handle, tempfile_path = tempfile.mkstemp()
199203
with os.fdopen(handle, 'w') as fh:
@@ -204,8 +208,8 @@ def test_hashfile(barerepo):
204208
assert hashed_sha1 == written_sha1
205209

206210

207-
def test_conflicts_in_bare_repository(barerepo):
208-
def create_conflict_file(repo, branch, content):
211+
def test_conflicts_in_bare_repository(barerepo: Repository) -> None:
212+
def create_conflict_file(repo: Repository, branch: Branch, content: str) -> Oid:
209213
oid = repo.create_blob(content.encode('utf-8'))
210214
tb = repo.TreeBuilder()
211215
tb.insert('conflict', oid, FileMode.BLOB)
@@ -218,9 +222,13 @@ def create_conflict_file(repo, branch, content):
218222
assert commit is not None
219223
return commit
220224

221-
b1 = barerepo.create_branch('b1', barerepo.head.peel())
225+
head_peeled = barerepo.head.peel()
226+
assert isinstance(head_peeled, Commit)
227+
b1 = barerepo.create_branch('b1', head_peeled)
222228
c1 = create_conflict_file(barerepo, b1, 'ASCII - abc')
223-
b2 = barerepo.create_branch('b2', barerepo.head.peel())
229+
head_peeled = barerepo.head.peel()
230+
assert isinstance(head_peeled, Commit)
231+
b2 = barerepo.create_branch('b2', head_peeled)
224232
c2 = create_conflict_file(barerepo, b2, 'Unicode - äüö')
225233

226234
index = barerepo.merge_commits(c1, c2)

0 commit comments

Comments
 (0)