Skip to content

Commit 69dd9f4

Browse files
committed
mypy: enable disallow_untyped_defs for test folder
and fix remaining errors
1 parent 5d1f6c5 commit 69dd9f4

File tree

12 files changed

+120
-86
lines changed

12 files changed

+120
-86
lines changed

mypy.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[mypy]
2+
3+
[mypy-test.*]
4+
disallow_untyped_defs = True

pygit2/_pygit2.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ class Repository:
867867
) -> Oid: ...
868868
def diff(
869869
self,
870-
a: None | str | bytes | Oid | Reference = None,
871-
b: None | str | bytes | Oid | Reference = None,
870+
a: None | str | bytes | Commit | Oid | Reference = None,
871+
b: None | str | bytes | Commit | Oid | Reference = None,
872872
cached: bool = False,
873873
flags: DiffOption = DiffOption.NORMAL,
874874
context_lines: int = 3,
@@ -1010,7 +1010,7 @@ class Repository:
10101010
include_ignored: bool = False,
10111011
keep_all: bool = False,
10121012
paths: list[str] | None = None,
1013-
) -> None: ...
1013+
) -> Oid: ...
10141014
def stash_apply(
10151015
self,
10161016
index: int = 0,
@@ -1036,7 +1036,7 @@ class Repository:
10361036
def walk(
10371037
self, oid: _OidArg | None, sort_mode: SortMode = SortMode.NONE
10381038
) -> Walker: ...
1039-
def write(self, type: int, data: bytes) -> Oid: ...
1039+
def write(self, type: int, data: bytes | str) -> Oid: ...
10401040
def write_archive(
10411041
self,
10421042
treeish: str | Tree | Object | Oid,
@@ -1147,7 +1147,7 @@ class Worktree:
11471147
def prune(self, force=False) -> None: ...
11481148

11491149
def discover_repository(
1150-
path: str, across_fs: bool = False, ceiling_dirs: str = ...
1150+
path: str | Path, across_fs: bool = False, ceiling_dirs: str = ...
11511151
) -> str | None: ...
11521152
def hash(data: bytes | str) -> Oid: ...
11531153
def hashfile(path: str) -> Oid: ...
@@ -1211,7 +1211,7 @@ def option(
12111211
Option.DISABLE_PACK_KEEP_FILE_CHECKS,
12121212
Option.SET_OWNER_VALIDATION,
12131213
],
1214-
value: bool,
1214+
value: bool | Literal[0, 1],
12151215
) -> None: ...
12161216
@overload
12171217
def option(opt: Literal[Option.GET_OWNER_VALIDATION]) -> int: ...

test/conftest.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import platform
22
from pathlib import Path
3+
from typing import Generator
34

45
import pytest
56

67
import pygit2
8+
from pygit2 import Repository
79

810
from . import utils
911

1012

1113
@pytest.fixture(scope='session', autouse=True)
12-
def global_git_config():
14+
def global_git_config() -> None:
1315
# Do not use global config for better test reproducibility.
1416
# https://github.com/libgit2/pygit2/issues/989
1517
levels = [
@@ -26,74 +28,76 @@ def global_git_config():
2628

2729

2830
@pytest.fixture
29-
def pygit2_empty_key():
31+
def pygit2_empty_key() -> tuple[Path, str, str]:
3032
path = Path(__file__).parent / 'keys' / 'pygit2_empty'
3133
return path, f'{path}.pub', 'empty'
3234

3335

3436
@pytest.fixture
35-
def barerepo(tmp_path):
37+
def barerepo(tmp_path: Path) -> Generator[Repository, None, None]:
3638
with utils.TemporaryRepository('barerepo.zip', tmp_path) as path:
3739
yield pygit2.Repository(path)
3840

3941

4042
@pytest.fixture
41-
def barerepo_path(tmp_path):
43+
def barerepo_path(tmp_path: Path) -> Generator[tuple[Repository, Path], None, None]:
4244
with utils.TemporaryRepository('barerepo.zip', tmp_path) as path:
4345
yield pygit2.Repository(path), path
4446

4547

4648
@pytest.fixture
47-
def blameflagsrepo(tmp_path):
49+
def blameflagsrepo(tmp_path: Path) -> Generator[Repository, None, None]:
4850
with utils.TemporaryRepository('blameflagsrepo.zip', tmp_path) as path:
4951
yield pygit2.Repository(path)
5052

5153

5254
@pytest.fixture
53-
def dirtyrepo(tmp_path):
55+
def dirtyrepo(tmp_path: Path) -> Generator[Repository, None, None]:
5456
with utils.TemporaryRepository('dirtyrepo.zip', tmp_path) as path:
5557
yield pygit2.Repository(path)
5658

5759

5860
@pytest.fixture
59-
def emptyrepo(barerepo, tmp_path):
61+
def emptyrepo(
62+
barerepo: Repository, tmp_path: Path
63+
) -> Generator[Repository, None, None]:
6064
with utils.TemporaryRepository('emptyrepo.zip', tmp_path) as path:
6165
repo = pygit2.Repository(path)
6266
repo.remotes.create('origin', barerepo.path)
6367
yield repo
6468

6569

6670
@pytest.fixture
67-
def encodingrepo(tmp_path):
71+
def encodingrepo(tmp_path: Path) -> Generator[Repository, None, None]:
6872
with utils.TemporaryRepository('encoding.zip', tmp_path) as path:
6973
yield pygit2.Repository(path)
7074

7175

7276
@pytest.fixture
73-
def mergerepo(tmp_path):
77+
def mergerepo(tmp_path: Path) -> Generator[Repository, None, None]:
7478
with utils.TemporaryRepository('testrepoformerging.zip', tmp_path) as path:
7579
yield pygit2.Repository(path)
7680

7781

7882
@pytest.fixture
79-
def testrepo(tmp_path):
83+
def testrepo(tmp_path: Path) -> Generator[Repository, None, None]:
8084
with utils.TemporaryRepository('testrepo.zip', tmp_path) as path:
8185
yield pygit2.Repository(path)
8286

8387

8488
@pytest.fixture
85-
def testrepo_path(tmp_path):
89+
def testrepo_path(tmp_path: Path) -> Generator[tuple[Repository, Path], None, None]:
8690
with utils.TemporaryRepository('testrepo.zip', tmp_path) as path:
8791
yield pygit2.Repository(path), path
8892

8993

9094
@pytest.fixture
91-
def testrepopacked(tmp_path):
95+
def testrepopacked(tmp_path: Path) -> Generator[Repository, None, None]:
9296
with utils.TemporaryRepository('testrepopacked.zip', tmp_path) as path:
9397
yield pygit2.Repository(path)
9498

9599

96100
@pytest.fixture
97-
def gpgsigned(tmp_path):
101+
def gpgsigned(tmp_path: Path) -> Generator[Repository, None, None]:
98102
with utils.TemporaryRepository('gpgsigned.zip', tmp_path) as path:
99103
yield pygit2.Repository(path)

test/test_blame.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ def test_blame_flags(blameflagsrepo: Repository) -> None:
9797
def test_blame_with_invalid_index(testrepo: Repository) -> None:
9898
blame = testrepo.blame(PATH)
9999

100-
def test():
100+
with pytest.raises(IndexError):
101101
blame[100000]
102-
blame[-1]
103102

104-
with pytest.raises(IndexError):
105-
test()
103+
with pytest.raises(OverflowError):
104+
blame[-1]
106105

107106

108107
def test_blame_for_line(testrepo: Repository) -> None:

test/test_blob.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_create_blob(testrepo: Repository) -> None:
110110
assert len(BLOB_NEW_CONTENT) == len(blob_buffer)
111111
assert BLOB_NEW_CONTENT == blob_buffer
112112

113-
def set_content():
113+
def set_content() -> None:
114114
blob_buffer[:2] = b'hi'
115115

116116
with pytest.raises(TypeError):

test/test_config.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,27 @@ def config(testrepo: Repository) -> Generator[object, None, None]:
4444
pass
4545

4646

47-
def test_config(config):
47+
def test_config(config: Config) -> None:
4848
assert config is not None
4949

5050

51-
def test_global_config():
51+
def test_global_config() -> None:
5252
try:
5353
assert Config.get_global_config() is not None
5454
except IOError:
5555
# There is no user config
5656
pass
5757

5858

59-
def test_system_config():
59+
def test_system_config() -> None:
6060
try:
6161
assert Config.get_system_config() is not None
6262
except IOError:
6363
# There is no system config
6464
pass
6565

6666

67-
def test_new():
67+
def test_new() -> None:
6868
# Touch file
6969
open(CONFIG_FILENAME, 'w').close()
7070

@@ -81,7 +81,7 @@ def test_new():
8181
assert config_read['core.editor'] == 'ed'
8282

8383

84-
def test_add():
84+
def test_add() -> None:
8585
with open(CONFIG_FILENAME, 'w') as new_file:
8686
new_file.write('[this]\n\tthat = true\n')
8787
new_file.write('[something "other"]\n\there = false')
@@ -94,7 +94,7 @@ def test_add():
9494
assert not config.get_bool('something.other.here')
9595

9696

97-
def test_add_aspath():
97+
def test_add_aspath() -> None:
9898
with open(CONFIG_FILENAME, 'w') as new_file:
9999
new_file.write('[this]\n\tthat = true\n')
100100

@@ -103,7 +103,7 @@ def test_add_aspath():
103103
assert 'this.that' in config
104104

105105

106-
def test_read(config):
106+
def test_read(config: Config) -> None:
107107
with pytest.raises(TypeError):
108108
config[()]
109109
with pytest.raises(TypeError):
@@ -121,7 +121,7 @@ def test_read(config):
121121
assert config.get_int('core.repositoryformatversion') == 0
122122

123123

124-
def test_write(config):
124+
def test_write(config: Config) -> None:
125125
with pytest.raises(TypeError):
126126
config.__setitem__((), 'This should not work')
127127

@@ -148,7 +148,7 @@ def test_write(config):
148148
assert 'core.dummy3' not in config
149149

150150

151-
def test_multivar():
151+
def test_multivar() -> None:
152152
with open(CONFIG_FILENAME, 'w') as new_file:
153153
new_file.write('[this]\n\tthat = foobar\n\tthat = foobeer\n')
154154

@@ -175,7 +175,7 @@ def test_multivar():
175175
assert [] == list(config.get_multivar('this.that', ''))
176176

177177

178-
def test_iterator(config):
178+
def test_iterator(config: Config) -> None:
179179
lst = {}
180180
for entry in config:
181181
assert entry.level > -1
@@ -185,7 +185,7 @@ def test_iterator(config):
185185
assert lst['core.bare']
186186

187187

188-
def test_parsing():
188+
def test_parsing() -> None:
189189
assert Config.parse_bool('on')
190190
assert Config.parse_bool('1')
191191

test/test_credentials.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,15 @@ def test_keypair_from_memory(
137137
pygit2.clone_repository(url, tmp_path, callbacks=callbacks)
138138

139139

140-
def test_callback(testrepo: Repository):
140+
def test_callback(testrepo: Repository) -> None:
141141
class MyCallbacks(pygit2.RemoteCallbacks):
142-
def credentials(testrepo, url, username, allowed):
143-
assert allowed & CredentialType.USERPASS_PLAINTEXT
142+
def credentials(
143+
self,
144+
url: str,
145+
username_from_url: str | None,
146+
allowed_types: CredentialType,
147+
) -> Username | UserPass | Keypair:
148+
assert allowed_types & CredentialType.USERPASS_PLAINTEXT
144149
raise Exception("I don't know the password")
145150

146151
url = 'https://github.com/github/github'
@@ -150,10 +155,15 @@ def credentials(testrepo, url, username, allowed):
150155

151156

152157
@utils.requires_network
153-
def test_bad_cred_type(testrepo: Repository):
158+
def test_bad_cred_type(testrepo: Repository) -> None:
154159
class MyCallbacks(pygit2.RemoteCallbacks):
155-
def credentials(testrepo, url, username, allowed):
156-
assert allowed & CredentialType.USERPASS_PLAINTEXT
160+
def credentials(
161+
self,
162+
url: str,
163+
username_from_url: str | None,
164+
allowed_types: CredentialType,
165+
) -> Username | UserPass | Keypair:
166+
assert allowed_types & CredentialType.USERPASS_PLAINTEXT
157167
return Keypair('git', 'foo.pub', 'foo', 'sekkrit')
158168

159169
url = 'https://github.com/github/github'
@@ -163,9 +173,11 @@ def credentials(testrepo, url, username, allowed):
163173

164174

165175
@utils.requires_network
166-
def test_fetch_certificate_check(testrepo: Repository):
176+
def test_fetch_certificate_check(testrepo: Repository) -> None:
167177
class MyCallbacks(pygit2.RemoteCallbacks):
168-
def certificate_check(testrepo, certificate, valid, host):
178+
def certificate_check(
179+
self, certificate: None, valid: bool, host: bytes
180+
) -> bool:
169181
assert certificate is None
170182
assert valid is True
171183
assert host == b'github.com'
@@ -188,7 +200,7 @@ def certificate_check(testrepo, certificate, valid, host):
188200

189201

190202
@utils.requires_network
191-
def test_user_pass(testrepo: Repository):
203+
def test_user_pass(testrepo: Repository) -> None:
192204
credentials = UserPass('libgit2', 'libgit2')
193205
callbacks = pygit2.RemoteCallbacks(credentials=credentials)
194206

@@ -200,7 +212,7 @@ def test_user_pass(testrepo: Repository):
200212
@utils.requires_proxy
201213
@utils.requires_network
202214
@utils.requires_future_libgit2
203-
def test_proxy(testrepo: Repository):
215+
def test_proxy(testrepo: Repository) -> None:
204216
credentials = UserPass('libgit2', 'libgit2')
205217
callbacks = pygit2.RemoteCallbacks(credentials=credentials)
206218

test/test_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def test_parse_diff_null() -> None:
433433
pygit2.Diff.parse_diff(None) # type: ignore
434434

435435

436-
def test_parse_diff_bad():
436+
def test_parse_diff_bad() -> None:
437437
diff = textwrap.dedent(
438438
"""
439439
diff --git a/file1 b/file1

test/test_index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ def test_entry_repr(testrepo: Repository) -> None:
298298
)
299299

300300

301-
def test_create_empty():
301+
def test_create_empty() -> None:
302302
Index()
303303

304304

305-
def test_create_empty_read_tree_as_string():
305+
def test_create_empty_read_tree_as_string() -> None:
306306
index = Index()
307307
# no repo associated, so we don't know where to read from
308308
with pytest.raises(TypeError):

test/test_patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"""
8282

8383

84-
def test_patch_create_from_buffers():
84+
def test_patch_create_from_buffers() -> None:
8585
patch = pygit2.Patch.create_from(
8686
BLOB_OLD_CONTENT,
8787
BLOB_NEW_CONTENT,

0 commit comments

Comments
 (0)