Skip to content

Commit 79a7de5

Browse files
committed
add types to several test files
1 parent 49d32f3 commit 79a7de5

File tree

7 files changed

+59
-40
lines changed

7 files changed

+59
-40
lines changed

test/test_repository_custom.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@
2424
# Boston, MA 02110-1301, USA.
2525

2626
from pathlib import Path
27+
from typing import Generator
2728

2829
import pytest
2930

3031
import pygit2
32+
from pygit2 import Repository
3133
from pygit2.enums import ObjectType
3234

3335

3436
@pytest.fixture
35-
def repo(testrepopacked):
37+
def repo(testrepopacked: Repository) -> Generator[Repository, None, None]:
3638
testrepo = testrepopacked
3739

3840
odb = pygit2.Odb()
@@ -49,14 +51,14 @@ def repo(testrepopacked):
4951
yield repo
5052

5153

52-
def test_references(repo):
54+
def test_references(repo: Repository) -> None:
5355
refs = [(ref.name, ref.target) for ref in repo.references.objects]
5456
assert sorted(refs) == [
5557
('refs/heads/i18n', '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'),
5658
('refs/heads/master', '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'),
5759
]
5860

5961

60-
def test_objects(repo):
62+
def test_objects(repo: Repository) -> None:
6163
a = repo.read('323fae03f4606ea9991df8befbb2fca795e648fa')
6264
assert (ObjectType.BLOB, b'foobar\n') == a

test/test_repository_empty.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26+
from pygit2 import Repository
2627

27-
def test_is_empty(emptyrepo):
28+
29+
def test_is_empty(emptyrepo: Repository) -> None:
2830
assert emptyrepo.is_empty
2931

3032

31-
def test_is_base(emptyrepo):
33+
def test_is_base(emptyrepo: Repository) -> None:
3234
assert not emptyrepo.is_bare
3335

3436

35-
def test_head(emptyrepo):
37+
def test_head(emptyrepo: Repository) -> None:
3638
assert emptyrepo.head_is_unborn
3739
assert not emptyrepo.head_is_detached

test/test_revparse.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@
2727

2828
from pytest import raises
2929

30-
from pygit2 import InvalidSpecError
30+
from pygit2 import InvalidSpecError, Repository
3131
from pygit2.enums import RevSpecFlag
3232

3333
HEAD_SHA = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
3434
PARENT_SHA = '5ebeeebb320790caf276b9fc8b24546d63316533' # HEAD^
3535

3636

37-
def test_revparse_single(testrepo):
37+
def test_revparse_single(testrepo: Repository) -> None:
3838
assert testrepo.revparse_single('HEAD').id == HEAD_SHA
3939
assert testrepo.revparse_single('HEAD^').id == PARENT_SHA
4040
o = testrepo.revparse_single('@{-1}')
4141
assert o.id == '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'
4242

4343

44-
def test_revparse_ext(testrepo):
44+
def test_revparse_ext(testrepo: Repository) -> None:
4545
o, r = testrepo.revparse_ext('master')
4646
assert o.id == HEAD_SHA
4747
assert r == testrepo.references['refs/heads/master']
@@ -55,37 +55,37 @@ def test_revparse_ext(testrepo):
5555
assert r == testrepo.references['refs/heads/i18n']
5656

5757

58-
def test_revparse_1(testrepo):
58+
def test_revparse_1(testrepo: Repository) -> None:
5959
s = testrepo.revparse('master')
6060
assert s.from_object.id == HEAD_SHA
6161
assert s.to_object is None
6262
assert s.flags == RevSpecFlag.SINGLE
6363

6464

65-
def test_revparse_range_1(testrepo):
65+
def test_revparse_range_1(testrepo: Repository) -> None:
6666
s = testrepo.revparse('HEAD^1..acecd5e')
6767
assert s.from_object.id == PARENT_SHA
6868
assert str(s.to_object.id).startswith('acecd5e')
6969
assert s.flags == RevSpecFlag.RANGE
7070

7171

72-
def test_revparse_range_2(testrepo):
72+
def test_revparse_range_2(testrepo: Repository) -> None:
7373
s = testrepo.revparse('HEAD...i18n')
7474
assert str(s.from_object.id).startswith('2be5719')
7575
assert str(s.to_object.id).startswith('5470a67')
7676
assert s.flags == RevSpecFlag.RANGE | RevSpecFlag.MERGE_BASE
7777
assert testrepo.merge_base(s.from_object.id, s.to_object.id) is not None
7878

7979

80-
def test_revparse_range_errors(testrepo):
80+
def test_revparse_range_errors(testrepo: Repository) -> None:
8181
with raises(KeyError):
8282
testrepo.revparse('nope..2be571915')
8383

8484
with raises(InvalidSpecError):
8585
testrepo.revparse('master............2be571915')
8686

8787

88-
def test_revparse_repr(testrepo):
88+
def test_revparse_repr(testrepo: Repository) -> None:
8989
s = testrepo.revparse('HEAD...i18n')
9090
assert (
9191
repr(s)

test/test_revwalk.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
"""Tests for revision walk."""
2727

28+
from pygit2 import Repository
2829
from pygit2.enums import SortMode
2930

3031
# In the order given by git log
@@ -50,62 +51,62 @@
5051
]
5152

5253

53-
def test_log(testrepo):
54+
def test_log(testrepo: Repository) -> None:
5455
ref = testrepo.lookup_reference('HEAD')
5556
for i, entry in enumerate(ref.log()):
5657
assert entry.committer.name == REVLOGS[i][0]
5758
assert entry.message == REVLOGS[i][1]
5859

5960

60-
def test_walk(testrepo):
61+
def test_walk(testrepo: Repository) -> None:
6162
walker = testrepo.walk(log[0], SortMode.TIME)
6263
assert [x.id for x in walker] == log
6364

6465

65-
def test_reverse(testrepo):
66+
def test_reverse(testrepo: Repository) -> None:
6667
walker = testrepo.walk(log[0], SortMode.TIME | SortMode.REVERSE)
6768
assert [x.id for x in walker] == list(reversed(log))
6869

6970

70-
def test_hide(testrepo):
71+
def test_hide(testrepo: Repository) -> None:
7172
walker = testrepo.walk(log[0], SortMode.TIME)
7273
walker.hide('4ec4389a8068641da2d6578db0419484972284c8')
7374
assert len(list(walker)) == 2
7475

7576

76-
def test_hide_prefix(testrepo):
77+
def test_hide_prefix(testrepo: Repository) -> None:
7778
walker = testrepo.walk(log[0], SortMode.TIME)
7879
walker.hide('4ec4389a')
7980
assert len(list(walker)) == 2
8081

8182

82-
def test_reset(testrepo):
83+
def test_reset(testrepo: Repository) -> None:
8384
walker = testrepo.walk(log[0], SortMode.TIME)
8485
walker.reset()
8586
assert list(walker) == []
8687

8788

88-
def test_push(testrepo):
89+
def test_push(testrepo: Repository) -> None:
8990
walker = testrepo.walk(log[-1], SortMode.TIME)
9091
assert [x.id for x in walker] == log[-1:]
9192
walker.reset()
9293
walker.push(log[0])
9394
assert [x.id for x in walker] == log
9495

9596

96-
def test_sort(testrepo):
97+
def test_sort(testrepo: Repository) -> None:
9798
walker = testrepo.walk(log[0], SortMode.TIME)
9899
walker.sort(SortMode.TIME | SortMode.REVERSE)
99100
assert [x.id for x in walker] == list(reversed(log))
100101

101102

102-
def test_simplify_first_parent(testrepo):
103+
def test_simplify_first_parent(testrepo: Repository) -> None:
103104
walker = testrepo.walk(log[0], SortMode.TIME)
104105
walker.simplify_first_parent()
105106
assert len(list(walker)) == 3
106107

107108

108-
def test_default_sorting(testrepo):
109+
def test_default_sorting(testrepo: Repository) -> None:
109110
walker = testrepo.walk(log[0], SortMode.NONE)
110111
list1 = list([x.id for x in walker])
111112
walker = testrepo.walk(log[0])

test/test_status.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525

2626
import pytest
2727

28+
from pygit2 import Repository
2829
from pygit2.enums import FileStatus
2930

3031

31-
def test_status(dirtyrepo):
32+
def test_status(dirtyrepo: Repository) -> None:
3233
"""
3334
For every file in the status, check that the flags are correct.
3435
"""
@@ -38,7 +39,7 @@ def test_status(dirtyrepo):
3839
assert status == git_status[filepath]
3940

4041

41-
def test_status_untracked_no(dirtyrepo):
42+
def test_status_untracked_no(dirtyrepo: Repository) -> None:
4243
git_status = dirtyrepo.status(untracked_files='no')
4344
assert not any(status & FileStatus.WT_NEW for status in git_status.values())
4445

@@ -67,15 +68,19 @@ def test_status_untracked_no(dirtyrepo):
6768
),
6869
],
6970
)
70-
def test_status_untracked_normal(dirtyrepo, untracked_files, expected):
71+
def test_status_untracked_normal(
72+
dirtyrepo: Repository, untracked_files: str, expected: set[str]
73+
) -> None:
7174
git_status = dirtyrepo.status(untracked_files=untracked_files)
7275
assert {
7376
file for file, status in git_status.items() if status & FileStatus.WT_NEW
7477
} == expected
7578

7679

7780
@pytest.mark.parametrize('ignored,expected', [(True, {'ignored'}), (False, set())])
78-
def test_status_ignored(dirtyrepo, ignored, expected):
81+
def test_status_ignored(
82+
dirtyrepo: Repository, ignored: bool, expected: set[str]
83+
) -> None:
7984
git_status = dirtyrepo.status(ignored=ignored)
8085
assert {
8186
file for file, status in git_status.items() if status & FileStatus.IGNORED

test/test_tag.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@
2828
import pytest
2929

3030
import pygit2
31+
from pygit2 import Repository
3132
from pygit2.enums import ObjectType
3233

3334
TAG_SHA = '3d2962987c695a29f1f80b6c3aa4ec046ef44369'
3435

3536

36-
def test_read_tag(barerepo):
37+
def test_read_tag(barerepo: Repository) -> None:
3738
repo = barerepo
3839
tag = repo[TAG_SHA]
39-
target = repo[tag.target]
4040
assert isinstance(tag, pygit2.Tag)
41-
assert ObjectType.TAG == tag.type
42-
assert ObjectType.COMMIT == target.type
41+
target = repo[tag.target]
42+
assert isinstance(target, pygit2.Commit)
43+
assert int(ObjectType.TAG) == tag.type
44+
assert int(ObjectType.COMMIT) == target.type
4345
assert 'root' == tag.name
4446
assert 'Tagged root commit.\n' == tag.message
4547
assert 'Initial test data commit.\n' == target.message
@@ -48,7 +50,7 @@ def test_read_tag(barerepo):
4850
)
4951

5052

51-
def test_new_tag(barerepo):
53+
def test_new_tag(barerepo: Repository) -> None:
5254
name = 'thetag'
5355
target = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
5456
message = 'Tag a blob.\n'
@@ -57,10 +59,11 @@ def test_new_tag(barerepo):
5759
target_prefix = target[:5]
5860
too_short_prefix = target[:3]
5961
with pytest.raises(ValueError):
60-
barerepo.create_tag(name, too_short_prefix, ObjectType.BLOB, tagger, message)
62+
barerepo.create_tag(name, too_short_prefix, ObjectType.BLOB, tagger, message) # type: ignore
6163

6264
sha = barerepo.create_tag(name, target_prefix, ObjectType.BLOB, tagger, message)
6365
tag = barerepo[sha]
66+
assert isinstance(tag, pygit2.Tag)
6467

6568
assert '3ee44658fd11660e828dfc96b9b5c5f38d5b49bb' == tag.id
6669
assert name == tag.name
@@ -70,7 +73,7 @@ def test_new_tag(barerepo):
7073
assert name == barerepo[tag.id].name
7174

7275

73-
def test_modify_tag(barerepo):
76+
def test_modify_tag(barerepo: Repository) -> None:
7477
name = 'thetag'
7578
target = 'af431f20fc541ed6d5afede3e2dc7160f6f01f16'
7679
message = 'Tag a blob.\n'
@@ -87,7 +90,8 @@ def test_modify_tag(barerepo):
8790
setattr(tag, 'message', message)
8891

8992

90-
def test_get_object(barerepo):
93+
def test_get_object(barerepo: Repository) -> None:
9194
repo = barerepo
9295
tag = repo[TAG_SHA]
96+
assert isinstance(tag, pygit2.Tag)
9397
assert repo[tag.target].id == tag.get_object().id

test/test_treebuilder.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,42 @@
2323
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424
# Boston, MA 02110-1301, USA.
2525

26+
from pygit2 import Repository, Tree
2627

2728
TREE_SHA = '967fce8df97cc71722d3c2a5930ef3e6f1d27b12'
2829

2930

30-
def test_new_empty_treebuilder(barerepo):
31+
def test_new_empty_treebuilder(barerepo: Repository) -> None:
3132
barerepo.TreeBuilder()
3233

3334

34-
def test_noop_treebuilder(barerepo):
35+
def test_noop_treebuilder(barerepo: Repository) -> None:
3536
tree = barerepo[TREE_SHA]
37+
assert isinstance(tree, Tree)
3638
bld = barerepo.TreeBuilder(TREE_SHA)
3739
result = bld.write()
3840

3941
assert len(bld) == len(tree)
4042
assert tree.id == result
4143

4244

43-
def test_noop_treebuilder_from_tree(barerepo):
45+
def test_noop_treebuilder_from_tree(barerepo: Repository) -> None:
4446
tree = barerepo[TREE_SHA]
47+
assert isinstance(tree, Tree)
4548
bld = barerepo.TreeBuilder(tree)
4649
result = bld.write()
4750

4851
assert len(bld) == len(tree)
4952
assert tree.id == result
5053

5154

52-
def test_rebuild_treebuilder(barerepo):
55+
def test_rebuild_treebuilder(barerepo: Repository) -> None:
5356
tree = barerepo[TREE_SHA]
57+
assert isinstance(tree, Tree)
5458
bld = barerepo.TreeBuilder()
5559
for entry in tree:
5660
name = entry.name
61+
assert name is not None
5762
assert bld.get(name) is None
5863
bld.insert(name, entry.id, entry.filemode)
5964
assert bld.get(name).id == entry.id

0 commit comments

Comments
 (0)