Skip to content

Commit 5ad2487

Browse files
committed
improve types according to test/test_commit.py
1 parent 4dcfc88 commit 5ad2487

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

pygit2/_pygit2.pyi

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ from typing import (
99
Iterator,
1010
Literal,
1111
Optional,
12+
Sequence,
1213
Type,
1314
TypedDict,
1415
TypeVar,
@@ -740,6 +741,16 @@ class Repository:
740741
def _from_c(cls, ptr: 'GitRepositoryC', owned: bool) -> 'Repository': ...
741742
def __getitem__(self, key: str | Oid) -> Object: ...
742743
def add_worktree(self, name: str, path: str, ref: Reference = ...) -> Worktree: ...
744+
def amend_commit(
745+
self,
746+
commit: Commit | Oid | str,
747+
refname: Reference | str | None,
748+
author: Signature | None = None,
749+
committer: Signature | None = None,
750+
message: str | None = None,
751+
tree: Tree | Oid | str | None = None,
752+
encoding: str = 'UTF-8',
753+
) -> Oid: ...
743754
def applies(
744755
self,
745756
diff: Diff,
@@ -782,7 +793,7 @@ class Repository:
782793
committer: Signature,
783794
message: str | bytes,
784795
tree: _OidArg,
785-
parents: list[_OidArg],
796+
parents: Sequence[_OidArg],
786797
encoding: str = ...,
787798
) -> Oid: ...
788799
def create_commit_string(

test/test_commit.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import pytest
3131

32-
from pygit2 import GitError, Oid, Signature
32+
from pygit2 import Commit, GitError, Oid, Repository, Signature, Tree
3333
from pygit2.enums import ObjectType
3434

3535
from . import utils
@@ -41,7 +41,7 @@
4141

4242

4343
@utils.requires_refcount
44-
def test_commit_refcount(barerepo):
44+
def test_commit_refcount(barerepo: Repository) -> None:
4545
commit = barerepo[COMMIT_SHA]
4646
start = sys.getrefcount(commit)
4747
tree = commit.tree
@@ -50,8 +50,9 @@ def test_commit_refcount(barerepo):
5050
assert start == end
5151

5252

53-
def test_read_commit(barerepo):
53+
def test_read_commit(barerepo: Repository) -> None:
5454
commit = barerepo[COMMIT_SHA]
55+
assert isinstance(commit, Commit)
5556
assert COMMIT_SHA == commit.id
5657
parents = commit.parents
5758
assert 1 == len(parents)
@@ -71,7 +72,7 @@ def test_read_commit(barerepo):
7172
assert '967fce8df97cc71722d3c2a5930ef3e6f1d27b12' == commit.tree.id
7273

7374

74-
def test_new_commit(barerepo):
75+
def test_new_commit(barerepo: Repository) -> None:
7576
repo = barerepo
7677
message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n'
7778
committer = Signature('John Doe', '[email protected]', 12346, 0)
@@ -88,8 +89,9 @@ def test_new_commit(barerepo):
8889

8990
sha = repo.create_commit(None, author, committer, message, tree_prefix, parents)
9091
commit = repo[sha]
92+
assert isinstance(commit, Commit)
9193

92-
assert ObjectType.COMMIT == commit.type
94+
assert ObjectType.COMMIT.value == commit.type
9395
assert '98286caaab3f1fde5bf52c8369b2b0423bad743b' == commit.id
9496
assert commit.message_encoding is None
9597
assert message == commit.message
@@ -103,7 +105,7 @@ def test_new_commit(barerepo):
103105
assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
104106

105107

106-
def test_new_commit_encoding(barerepo):
108+
def test_new_commit_encoding(barerepo: Repository) -> None:
107109
repo = barerepo
108110
encoding = 'iso-8859-1'
109111
message = 'New commit.\n\nMessage with non-ascii chars: ééé.\n'
@@ -117,8 +119,9 @@ def test_new_commit_encoding(barerepo):
117119
None, author, committer, message, tree_prefix, parents, encoding
118120
)
119121
commit = repo[sha]
122+
assert isinstance(commit, Commit)
120123

121-
assert ObjectType.COMMIT == commit.type
124+
assert ObjectType.COMMIT.value == commit.type
122125
assert 'iso-8859-1' == commit.message_encoding
123126
assert message.encode(encoding) == commit.raw_message
124127
assert 12346 == commit.commit_time
@@ -131,7 +134,7 @@ def test_new_commit_encoding(barerepo):
131134
assert Oid(hex=COMMIT_SHA) == commit.parent_ids[0]
132135

133136

134-
def test_modify_commit(barerepo):
137+
def test_modify_commit(barerepo: Repository) -> None:
135138
message = 'New commit.\n\nMessage.\n'
136139
committer = ('John Doe', '[email protected]', 12346)
137140
author = ('Jane Doe', '[email protected]', 12345)
@@ -150,9 +153,10 @@ def test_modify_commit(barerepo):
150153
setattr(commit, 'parents', None)
151154

152155

153-
def test_amend_commit_metadata(barerepo):
156+
def test_amend_commit_metadata(barerepo: Repository) -> None:
154157
repo = barerepo
155158
commit = repo[COMMIT_SHA_TO_AMEND]
159+
assert isinstance(commit, Commit)
156160
assert commit.id == repo.head.target
157161

158162
encoding = 'iso-8859-1'
@@ -173,9 +177,10 @@ def test_amend_commit_metadata(barerepo):
173177
encoding=encoding,
174178
)
175179
amended_commit = repo[amended_oid]
180+
assert isinstance(amended_commit, Commit)
176181

177182
assert repo.head.target == amended_oid
178-
assert ObjectType.COMMIT == amended_commit.type
183+
assert ObjectType.COMMIT.value == amended_commit.type
179184
assert amended_committer == amended_commit.committer
180185
assert amended_author == amended_commit.author
181186
assert amended_message.encode(encoding) == amended_commit.raw_message
@@ -184,31 +189,35 @@ def test_amend_commit_metadata(barerepo):
184189
assert commit.tree == amended_commit.tree # we didn't touch the tree
185190

186191

187-
def test_amend_commit_tree(barerepo):
192+
def test_amend_commit_tree(barerepo: Repository) -> None:
188193
repo = barerepo
189194
commit = repo[COMMIT_SHA_TO_AMEND]
195+
assert isinstance(commit, Commit)
190196
assert commit.id == repo.head.target
191197

192198
tree = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12'
193199
tree_prefix = tree[:5]
194200

195201
amended_oid = repo.amend_commit(commit, 'HEAD', tree=tree_prefix)
196202
amended_commit = repo[amended_oid]
203+
assert isinstance(amended_commit, Commit)
204+
assert isinstance(commit, Commit)
197205

198206
assert repo.head.target == amended_oid
199-
assert ObjectType.COMMIT == amended_commit.type
207+
assert ObjectType.COMMIT.value == amended_commit.type
200208
assert commit.message == amended_commit.message
201209
assert commit.author == amended_commit.author
202210
assert commit.committer == amended_commit.committer
203211
assert commit.tree_id != amended_commit.tree_id
204212
assert Oid(hex=tree) == amended_commit.tree_id
205213

206214

207-
def test_amend_commit_not_tip_of_branch(barerepo):
215+
def test_amend_commit_not_tip_of_branch(barerepo: Repository) -> None:
208216
repo = barerepo
209217

210218
# This commit isn't at the tip of the branch.
211219
commit = repo['5fe808e8953c12735680c257f56600cb0de44b10']
220+
assert isinstance(commit, Commit)
212221
assert commit.id != repo.head.target
213222

214223
# Can't update HEAD to the rewritten commit because it's not the tip of the branch.
@@ -219,50 +228,52 @@ def test_amend_commit_not_tip_of_branch(barerepo):
219228
repo.amend_commit(commit, None, message='this will work')
220229

221230

222-
def test_amend_commit_no_op(barerepo):
231+
def test_amend_commit_no_op(barerepo: Repository) -> None:
223232
repo = barerepo
224233
commit = repo[COMMIT_SHA_TO_AMEND]
234+
assert isinstance(commit, Commit)
225235
assert commit.id == repo.head.target
226236

227237
amended_oid = repo.amend_commit(commit, None)
228238
assert amended_oid == commit.id
229239

230240

231-
def test_amend_commit_argument_types(barerepo):
241+
def test_amend_commit_argument_types(barerepo: Repository) -> None:
232242
repo = barerepo
233243

234244
some_tree = repo['967fce8df97cc71722d3c2a5930ef3e6f1d27b12']
235245
commit = repo[COMMIT_SHA_TO_AMEND]
236246
alt_commit1 = Oid(hex=COMMIT_SHA_TO_AMEND)
237247
alt_commit2 = COMMIT_SHA_TO_AMEND
238248
alt_tree = some_tree
249+
assert isinstance(alt_tree, Tree)
239250
alt_refname = (
240251
repo.head
241252
) # try this one last, because it'll change the commit at the tip
242253

243254
# Pass bad values/types for the commit
244255
with pytest.raises(ValueError):
245-
repo.amend_commit(None, None)
256+
repo.amend_commit(None, None) # type: ignore
246257
with pytest.raises(TypeError):
247-
repo.amend_commit(some_tree, None)
258+
repo.amend_commit(some_tree, None) # type: ignore
248259

249260
# Pass bad types for signatures
250261
with pytest.raises(TypeError):
251-
repo.amend_commit(commit, None, author='Toto')
262+
repo.amend_commit(commit, None, author='Toto') # type: ignore
252263
with pytest.raises(TypeError):
253-
repo.amend_commit(commit, None, committer='Toto')
264+
repo.amend_commit(commit, None, committer='Toto') # type: ignore
254265

255266
# Pass bad refnames
256267
with pytest.raises(ValueError):
257-
repo.amend_commit(commit, 'this-ref-doesnt-exist')
268+
repo.amend_commit(commit, 'this-ref-doesnt-exist') # type: ignore
258269
with pytest.raises(TypeError):
259-
repo.amend_commit(commit, repo)
270+
repo.amend_commit(commit, repo) # type: ignore
260271

261272
# Pass bad trees
262273
with pytest.raises(ValueError):
263-
repo.amend_commit(commit, None, tree="can't parse this")
274+
repo.amend_commit(commit, None, tree="can't parse this") # type: ignore
264275
with pytest.raises(KeyError):
265-
repo.amend_commit(commit, None, tree='baaaaad')
276+
repo.amend_commit(commit, None, tree='baaaaad') # type: ignore
266277

267278
# Pass an Oid for the commit
268279
amended_oid = repo.amend_commit(alt_commit1, None, message='Hello')
@@ -273,7 +284,8 @@ def test_amend_commit_argument_types(barerepo):
273284
# Pass a str for the commit
274285
amended_oid = repo.amend_commit(alt_commit2, None, message='Hello', tree=alt_tree)
275286
amended_commit = repo[amended_oid]
276-
assert ObjectType.COMMIT == amended_commit.type
287+
assert isinstance(amended_commit, Commit)
288+
assert ObjectType.COMMIT.value == amended_commit.type
277289
assert amended_oid != COMMIT_SHA_TO_AMEND
278290
assert repo[COMMIT_SHA_TO_AMEND].tree != amended_commit.tree
279291
assert alt_tree.id == amended_commit.tree_id

0 commit comments

Comments
 (0)