Skip to content

Commit 7528441

Browse files
committed
improve types according to tests/test_apply_diff.py
1 parent a59944b commit 7528441

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

pygit2/_pygit2.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,15 @@ class Repository:
779779
def create_tag(
780780
self, name: str, oid: _OidArg, type: ObjectType, tagger: Signature, message: str
781781
) -> Oid: ...
782+
def diff(
783+
self,
784+
a: None | str | Reference = None,
785+
b: None | str | Reference = None,
786+
cached: bool = False,
787+
flags: DiffOption = DiffOption.NORMAL,
788+
context_lines: int = 3,
789+
interhunk_lines: int = 0,
790+
) -> Diff: ...
782791
def descendant_of(self, oid1: _OidArg, oid2: _OidArg) -> bool: ...
783792
def expand_id(self, hex: str) -> Oid: ...
784793
def free(self) -> None: ...

test/test_apply_diff.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,40 @@
2424
# Boston, MA 02110-1301, USA.
2525

2626
import pygit2
27+
from pygit2 import Repository, Diff
2728
from pygit2.enums import ApplyLocation, CheckoutStrategy, FileStatus
2829
import pytest
2930

3031
import os
3132
from pathlib import Path
3233

3334

34-
def read_content(testrepo):
35+
def read_content(testrepo: Repository) -> str:
3536
with (Path(testrepo.workdir) / 'hello.txt').open('rb') as f:
3637
return f.read().decode('utf-8')
3738

3839

3940
@pytest.fixture
40-
def new_content():
41-
content = ['bye world', 'adiós', 'au revoir monde']
42-
content = ''.join(x + os.linesep for x in content)
41+
def new_content() -> str:
42+
content_list = ['bye world', 'adiós', 'au revoir monde']
43+
content = ''.join(x + os.linesep for x in content_list)
4344
return content
4445

4546

4647
@pytest.fixture
47-
def old_content(testrepo):
48+
def old_content(testrepo: Repository) -> str:
4849
with (Path(testrepo.workdir) / 'hello.txt').open('rb') as f:
4950
return f.read().decode('utf-8')
5051

5152

5253
@pytest.fixture
53-
def patch_diff(testrepo, new_content):
54+
def patch_diff(testrepo: Repository, new_content: str) -> Diff:
5455
# Create the patch
5556
with (Path(testrepo.workdir) / 'hello.txt').open('wb') as f:
5657
f.write(new_content.encode('utf-8'))
5758

5859
patch = testrepo.diff().patch
60+
assert patch is not None
5961

6062
# Rollback all changes
6163
testrepo.checkout('HEAD', strategy=CheckoutStrategy.FORCE)
@@ -65,7 +67,7 @@ def patch_diff(testrepo, new_content):
6567

6668

6769
@pytest.fixture
68-
def foreign_patch_diff():
70+
def foreign_patch_diff() -> Diff:
6971
patch_contents = """diff --git a/this_file_does_not_exist b/this_file_does_not_exist
7072
index 7f129fd..af431f2 100644
7173
--- a/this_file_does_not_exist
@@ -77,37 +79,45 @@ def foreign_patch_diff():
7779
return pygit2.Diff.parse_diff(patch_contents)
7880

7981

80-
def test_apply_type_error(testrepo):
82+
def test_apply_type_error(testrepo: Repository) -> None:
8183
# Check apply type error
8284
with pytest.raises(TypeError):
83-
testrepo.apply('HEAD')
85+
testrepo.apply('HEAD') # type: ignore
8486

8587

86-
def test_apply_diff_to_workdir(testrepo, new_content, patch_diff):
88+
def test_apply_diff_to_workdir(
89+
testrepo: Repository, new_content: str, patch_diff: Diff
90+
) -> None:
8791
# Apply the patch and compare
8892
testrepo.apply(patch_diff, ApplyLocation.WORKDIR)
8993

9094
assert read_content(testrepo) == new_content
9195
assert testrepo.status_file('hello.txt') == FileStatus.WT_MODIFIED
9296

9397

94-
def test_apply_diff_to_index(testrepo, old_content, patch_diff):
98+
def test_apply_diff_to_index(
99+
testrepo: Repository, old_content: str, patch_diff: Diff
100+
) -> None:
95101
# Apply the patch and compare
96102
testrepo.apply(patch_diff, ApplyLocation.INDEX)
97103

98104
assert read_content(testrepo) == old_content
99105
assert testrepo.status_file('hello.txt') & FileStatus.INDEX_MODIFIED
100106

101107

102-
def test_apply_diff_to_both(testrepo, new_content, patch_diff):
108+
def test_apply_diff_to_both(
109+
testrepo: Repository, new_content: str, patch_diff: Diff
110+
) -> None:
103111
# Apply the patch and compare
104112
testrepo.apply(patch_diff, ApplyLocation.BOTH)
105113

106114
assert read_content(testrepo) == new_content
107115
assert testrepo.status_file('hello.txt') & FileStatus.INDEX_MODIFIED
108116

109117

110-
def test_diff_applies_to_workdir(testrepo, old_content, patch_diff):
118+
def test_diff_applies_to_workdir(
119+
testrepo: Repository, old_content: str, patch_diff: Diff
120+
) -> None:
111121
# See if patch applies
112122
assert testrepo.applies(patch_diff, ApplyLocation.WORKDIR)
113123

@@ -122,7 +132,9 @@ def test_diff_applies_to_workdir(testrepo, old_content, patch_diff):
122132
assert testrepo.applies(patch_diff, ApplyLocation.INDEX)
123133

124134

125-
def test_diff_applies_to_index(testrepo, old_content, patch_diff):
135+
def test_diff_applies_to_index(
136+
testrepo: Repository, old_content: str, patch_diff: Diff
137+
) -> None:
126138
# See if patch applies
127139
assert testrepo.applies(patch_diff, ApplyLocation.INDEX)
128140

@@ -137,7 +149,9 @@ def test_diff_applies_to_index(testrepo, old_content, patch_diff):
137149
assert testrepo.applies(patch_diff, ApplyLocation.WORKDIR)
138150

139151

140-
def test_diff_applies_to_both(testrepo, old_content, patch_diff):
152+
def test_diff_applies_to_both(
153+
testrepo: Repository, old_content: str, patch_diff: Diff
154+
) -> None:
141155
# See if patch applies
142156
assert testrepo.applies(patch_diff, ApplyLocation.BOTH)
143157

@@ -151,7 +165,9 @@ def test_diff_applies_to_both(testrepo, old_content, patch_diff):
151165
assert not testrepo.applies(patch_diff, ApplyLocation.INDEX)
152166

153167

154-
def test_applies_error(testrepo, old_content, patch_diff, foreign_patch_diff):
168+
def test_applies_error(
169+
testrepo: Repository, old_content: str, patch_diff: Diff, foreign_patch_diff: Diff
170+
) -> None:
155171
# Try to apply a "foreign" patch that affects files that aren't in the repo;
156172
# ensure we get OSError about the missing file (due to raise_error)
157173
with pytest.raises(OSError):

0 commit comments

Comments
 (0)