Skip to content

Commit 551af8d

Browse files
committed
improve types according to test/test_options.py
1 parent 197cf81 commit 551af8d

File tree

3 files changed

+136
-47
lines changed

3 files changed

+136
-47
lines changed

pygit2/_pygit2.pyi

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ from .enums import (
3636
BlobFilter,
3737
BranchType,
3838
CheckoutStrategy,
39+
ConfigLevel,
3940
DeltaStatus,
4041
DescribeStrategy,
4142
DiffFind,
@@ -1078,7 +1079,79 @@ def discover_repository(
10781079
def hash(data: bytes) -> Oid: ...
10791080
def hashfile(path: str) -> Oid: ...
10801081
def init_file_backend(path: str, flags: int = 0) -> object: ...
1081-
def option(opt: Option, *args) -> None: ...
1082+
@overload
1083+
def option(
1084+
opt: Literal[
1085+
Option.GET_MWINDOW_FILE_LIMIT,
1086+
Option.GET_MWINDOW_MAPPED_LIMIT,
1087+
Option.GET_MWINDOW_SIZE,
1088+
],
1089+
) -> int: ...
1090+
@overload
1091+
def option(
1092+
opt: Literal[
1093+
Option.SET_MWINDOW_FILE_LIMIT,
1094+
Option.SET_MWINDOW_MAPPED_LIMIT,
1095+
Option.SET_MWINDOW_SIZE,
1096+
],
1097+
value: int,
1098+
) -> None: ...
1099+
@overload
1100+
def option(opt: Literal[Option.GET_SEARCH_PATH], level: ConfigLevel) -> str: ...
1101+
@overload
1102+
def option(
1103+
opt: Literal[Option.SET_SEARCH_PATH], level: ConfigLevel, value: str
1104+
) -> None: ...
1105+
@overload
1106+
def option(
1107+
opt: Literal[Option.SET_CACHE_OBJECT_LIMIT], object_type: ObjectType, limit: int
1108+
) -> None: ...
1109+
@overload
1110+
def option(opt: Literal[Option.SET_CACHE_MAX_SIZE], max_size: int) -> None: ...
1111+
@overload
1112+
def option(opt: Literal[Option.GET_CACHED_MEMORY]) -> tuple[int, int]: ...
1113+
1114+
# not implemented:
1115+
# Option.GET_TEMPLATE_PATH
1116+
# Option.SET_TEMPLATE_PATH
1117+
1118+
@overload
1119+
def option(
1120+
opt: Literal[Option.SET_SSL_CERT_LOCATIONS],
1121+
file: str | bytes | None,
1122+
dir: str | bytes | None,
1123+
) -> None: ...
1124+
1125+
# not implemented:
1126+
# Option.SET_USER_AGENT
1127+
1128+
@overload
1129+
def option(
1130+
opt: Literal[
1131+
Option.ENABLE_CACHING,
1132+
Option.ENABLE_STRICT_OBJECT_CREATION,
1133+
Option.ENABLE_STRICT_SYMBOLIC_REF_CREATION,
1134+
Option.ENABLE_OFS_DELTA,
1135+
Option.ENABLE_FSYNC_GITDIR,
1136+
Option.ENABLE_STRICT_HASH_VERIFICATION,
1137+
Option.ENABLE_UNSAVED_INDEX_SAFETY,
1138+
Option.DISABLE_PACK_KEEP_FILE_CHECKS,
1139+
Option.SET_OWNER_VALIDATION,
1140+
],
1141+
value: bool,
1142+
) -> None: ...
1143+
@overload
1144+
def option(opt: Literal[Option.GET_OWNER_VALIDATION]) -> int: ...
1145+
1146+
# not implemented:
1147+
# Option.SET_SSL_CIPHERS
1148+
# Option.GET_USER_AGENT
1149+
# Option.GET_WINDOWS_SHAREMODE
1150+
# Option.SET_WINDOWS_SHAREMODE
1151+
# Option.SET_ALLOCATOR
1152+
# Option.GET_PACK_MAX_OBJECTS
1153+
# Option.SET_PACK_MAX_OBJECTS
1154+
10821155
def reference_is_valid_name(refname: str) -> bool: ...
10831156
def tree_entry_cmp(a: Object, b: Object) -> int: ...
10841157
def _cache_enums() -> None: ...

pygit2/settings.py

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@
2727
Settings mapping.
2828
"""
2929

30-
from ssl import get_default_verify_paths
30+
from ssl import DefaultVerifyPaths, get_default_verify_paths
31+
from typing import overload
3132

3233
import pygit2.enums
3334

3435
from ._pygit2 import option
35-
from .enums import Option
36+
from .enums import ConfigLevel, Option
3637
from .errors import GitError
3738

3839

3940
class SearchPathList:
40-
def __getitem__(self, key):
41+
def __getitem__(self, key: ConfigLevel) -> str:
4142
return option(Option.GET_SEARCH_PATH, key)
4243

43-
def __setitem__(self, key, value):
44+
def __setitem__(self, key: ConfigLevel, value: str) -> None:
4445
option(Option.SET_SEARCH_PATH, key, value)
4546

4647

@@ -50,12 +51,15 @@ class Settings:
5051
__slots__ = '_default_tls_verify_paths', '_ssl_cert_dir', '_ssl_cert_file'
5152

5253
_search_path = SearchPathList()
54+
_default_tls_verify_paths: DefaultVerifyPaths | None
55+
_ssl_cert_file: str | bytes | None
56+
_ssl_cert_dir: str | bytes | None
5357

54-
def __init__(self):
58+
def __init__(self) -> None:
5559
"""Initialize global pygit2 and libgit2 settings."""
5660
self._initialize_tls_certificate_locations()
5761

58-
def _initialize_tls_certificate_locations(self):
62+
def _initialize_tls_certificate_locations(self) -> None:
5963
"""Set up initial TLS file and directory lookup locations."""
6064
self._default_tls_verify_paths = get_default_verify_paths()
6165
try:
@@ -72,7 +76,7 @@ def _initialize_tls_certificate_locations(self):
7276
self._ssl_cert_dir = None
7377

7478
@property
75-
def search_path(self):
79+
def search_path(self) -> SearchPathList:
7680
"""Configuration file search path.
7781
7882
This behaves like an array whose indices correspond to ConfigLevel values.
@@ -81,35 +85,35 @@ def search_path(self):
8185
return self._search_path
8286

8387
@property
84-
def mwindow_size(self):
88+
def mwindow_size(self) -> int:
8589
"""Get or set the maximum mmap window size"""
8690
return option(Option.GET_MWINDOW_SIZE)
8791

8892
@mwindow_size.setter
89-
def mwindow_size(self, value):
93+
def mwindow_size(self, value: int) -> None:
9094
option(Option.SET_MWINDOW_SIZE, value)
9195

9296
@property
93-
def mwindow_mapped_limit(self):
97+
def mwindow_mapped_limit(self) -> int:
9498
"""
9599
Get or set the maximum memory that will be mapped in total by the
96100
library
97101
"""
98102
return option(Option.GET_MWINDOW_MAPPED_LIMIT)
99103

100104
@mwindow_mapped_limit.setter
101-
def mwindow_mapped_limit(self, value):
105+
def mwindow_mapped_limit(self, value: int) -> None:
102106
option(Option.SET_MWINDOW_MAPPED_LIMIT, value)
103107

104108
@property
105-
def cached_memory(self):
109+
def cached_memory(self) -> tuple[int, int]:
106110
"""
107111
Get the current bytes in cache and the maximum that would be
108112
allowed in the cache.
109113
"""
110114
return option(Option.GET_CACHED_MEMORY)
111115

112-
def enable_caching(self, value=True):
116+
def enable_caching(self, value: bool = True) -> None:
113117
"""
114118
Enable or disable caching completely.
115119
@@ -119,15 +123,15 @@ def enable_caching(self, value=True):
119123
"""
120124
return option(Option.ENABLE_CACHING, value)
121125

122-
def disable_pack_keep_file_checks(self, value=True):
126+
def disable_pack_keep_file_checks(self, value: bool = True) -> None:
123127
"""
124128
This will cause .keep file existence checks to be skipped when
125129
accessing packfiles, which can help performance with remote
126130
filesystems.
127131
"""
128132
return option(Option.DISABLE_PACK_KEEP_FILE_CHECKS, value)
129133

130-
def cache_max_size(self, value):
134+
def cache_max_size(self, value: int) -> None:
131135
"""
132136
Set the maximum total data size that will be cached in memory
133137
across all repositories before libgit2 starts evicting objects
@@ -137,7 +141,9 @@ def cache_max_size(self, value):
137141
"""
138142
return option(Option.SET_CACHE_MAX_SIZE, value)
139143

140-
def cache_object_limit(self, object_type: pygit2.enums.ObjectType, value):
144+
def cache_object_limit(
145+
self, object_type: pygit2.enums.ObjectType, value: int
146+
) -> None:
141147
"""
142148
Set the maximum data size for the given type of object to be
143149
considered eligible for caching in memory. Setting to value to
@@ -148,36 +154,46 @@ def cache_object_limit(self, object_type: pygit2.enums.ObjectType, value):
148154
return option(Option.SET_CACHE_OBJECT_LIMIT, object_type, value)
149155

150156
@property
151-
def ssl_cert_file(self):
157+
def ssl_cert_file(self) -> str | bytes | None:
152158
"""TLS certificate file path."""
153159
return self._ssl_cert_file
154160

155161
@ssl_cert_file.setter
156-
def ssl_cert_file(self, value):
162+
def ssl_cert_file(self, value: str | bytes) -> None:
157163
"""Set the TLS cert file path."""
158164
self.set_ssl_cert_locations(value, self._ssl_cert_dir)
159165

160166
@ssl_cert_file.deleter
161-
def ssl_cert_file(self):
167+
def ssl_cert_file(self) -> None:
162168
"""Reset the TLS cert file path."""
163-
self.ssl_cert_file = self._default_tls_verify_paths.cafile
169+
self.ssl_cert_file = self._default_tls_verify_paths.cafile # type: ignore[union-attr]
164170

165171
@property
166-
def ssl_cert_dir(self):
172+
def ssl_cert_dir(self) -> str | bytes | None:
167173
"""TLS certificates lookup directory path."""
168174
return self._ssl_cert_dir
169175

170176
@ssl_cert_dir.setter
171-
def ssl_cert_dir(self, value):
177+
def ssl_cert_dir(self, value: str | bytes) -> None:
172178
"""Set the TLS certificate lookup folder."""
173179
self.set_ssl_cert_locations(self._ssl_cert_file, value)
174180

175181
@ssl_cert_dir.deleter
176-
def ssl_cert_dir(self):
182+
def ssl_cert_dir(self) -> None:
177183
"""Reset the TLS certificate lookup folder."""
178-
self.ssl_cert_dir = self._default_tls_verify_paths.capath
179-
180-
def set_ssl_cert_locations(self, cert_file, cert_dir):
184+
self.ssl_cert_dir = self._default_tls_verify_paths.capath # type: ignore[union-attr]
185+
186+
@overload
187+
def set_ssl_cert_locations(
188+
self, cert_file: str | bytes | None, cert_dir: str | bytes
189+
) -> None: ...
190+
@overload
191+
def set_ssl_cert_locations(
192+
self, cert_file: str | bytes, cert_dir: str | bytes | None
193+
) -> None: ...
194+
def set_ssl_cert_locations(
195+
self, cert_file: str | bytes | None, cert_dir: str | bytes | None
196+
) -> None:
181197
"""
182198
Set the SSL certificate-authority locations.
183199

test/test_options.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,83 +28,83 @@
2828
from pygit2.enums import ConfigLevel, ObjectType, Option
2929

3030

31-
def __option(getter, setter, value):
32-
old_value = option(getter)
33-
option(setter, value)
34-
assert value == option(getter)
31+
def __option(getter: Option, setter: Option, value: object) -> None:
32+
old_value = option(getter) # type: ignore[call-overload]
33+
option(setter, value) # type: ignore[call-overload]
34+
assert value == option(getter) # type: ignore[call-overload]
3535
# Reset to avoid side effects in later tests
36-
option(setter, old_value)
36+
option(setter, old_value) # type: ignore[call-overload]
3737

3838

39-
def __proxy(name, value):
39+
def __proxy(name: str, value: object) -> None:
4040
old_value = getattr(pygit2.settings, name)
4141
setattr(pygit2.settings, name, value)
4242
assert value == getattr(pygit2.settings, name)
4343
# Reset to avoid side effects in later tests
4444
setattr(pygit2.settings, name, old_value)
4545

4646

47-
def test_mwindow_size():
47+
def test_mwindow_size() -> None:
4848
__option(Option.GET_MWINDOW_SIZE, Option.SET_MWINDOW_SIZE, 200 * 1024)
4949

5050

51-
def test_mwindow_size_proxy():
51+
def test_mwindow_size_proxy() -> None:
5252
__proxy('mwindow_size', 300 * 1024)
5353

5454

55-
def test_mwindow_mapped_limit_200():
55+
def test_mwindow_mapped_limit_200() -> None:
5656
__option(
5757
Option.GET_MWINDOW_MAPPED_LIMIT, Option.SET_MWINDOW_MAPPED_LIMIT, 200 * 1024
5858
)
5959

6060

61-
def test_mwindow_mapped_limit_300():
61+
def test_mwindow_mapped_limit_300() -> None:
6262
__proxy('mwindow_mapped_limit', 300 * 1024)
6363

6464

65-
def test_cache_object_limit():
65+
def test_cache_object_limit() -> None:
6666
new_limit = 2 * 1024
6767
option(Option.SET_CACHE_OBJECT_LIMIT, ObjectType.BLOB, new_limit)
6868

6969

70-
def test_cache_object_limit_proxy():
70+
def test_cache_object_limit_proxy() -> None:
7171
new_limit = 4 * 1024
7272
pygit2.settings.cache_object_limit(ObjectType.BLOB, new_limit)
7373

7474

75-
def test_cached_memory():
75+
def test_cached_memory() -> None:
7676
value = option(Option.GET_CACHED_MEMORY)
7777
assert value[1] == 256 * 1024**2
7878

7979

80-
def test_cached_memory_proxy():
80+
def test_cached_memory_proxy() -> None:
8181
assert pygit2.settings.cached_memory[1] == 256 * 1024**2
8282

8383

84-
def test_enable_caching():
84+
def test_enable_caching() -> None:
8585
pygit2.settings.enable_caching(False)
8686
pygit2.settings.enable_caching(True)
8787
# Lower level API
8888
option(Option.ENABLE_CACHING, False)
8989
option(Option.ENABLE_CACHING, True)
9090

9191

92-
def test_disable_pack_keep_file_checks():
92+
def test_disable_pack_keep_file_checks() -> None:
9393
pygit2.settings.disable_pack_keep_file_checks(False)
9494
pygit2.settings.disable_pack_keep_file_checks(True)
9595
# Lower level API
9696
option(Option.DISABLE_PACK_KEEP_FILE_CHECKS, False)
9797
option(Option.DISABLE_PACK_KEEP_FILE_CHECKS, True)
9898

9999

100-
def test_cache_max_size_proxy():
100+
def test_cache_max_size_proxy() -> None:
101101
pygit2.settings.cache_max_size(128 * 1024**2)
102102
assert pygit2.settings.cached_memory[1] == 128 * 1024**2
103103
pygit2.settings.cache_max_size(256 * 1024**2)
104104
assert pygit2.settings.cached_memory[1] == 256 * 1024**2
105105

106106

107-
def test_search_path():
107+
def test_search_path() -> None:
108108
paths = [
109109
(ConfigLevel.GLOBAL, '/tmp/global'),
110110
(ConfigLevel.XDG, '/tmp/xdg'),
@@ -116,7 +116,7 @@ def test_search_path():
116116
assert path == option(Option.GET_SEARCH_PATH, level)
117117

118118

119-
def test_search_path_proxy():
119+
def test_search_path_proxy() -> None:
120120
paths = [
121121
(ConfigLevel.GLOBAL, '/tmp2/global'),
122122
(ConfigLevel.XDG, '/tmp2/xdg'),
@@ -128,5 +128,5 @@ def test_search_path_proxy():
128128
assert path == pygit2.settings.search_path[level]
129129

130130

131-
def test_owner_validation():
131+
def test_owner_validation() -> None:
132132
__option(Option.GET_OWNER_VALIDATION, Option.SET_OWNER_VALIDATION, 0)

0 commit comments

Comments
 (0)