Skip to content

Commit 3611e7d

Browse files
just1602WhyNotHugo
authored andcommitted
Add type hint to vdirsyncer/cli/utils.py
1 parent adc974b commit 3611e7d

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

vdirsyncer/cli/utils.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import contextlib
24
import errno
35
import importlib
@@ -12,6 +14,7 @@
1214
from .. import BUGTRACKER_HOME
1315
from .. import DOCS_HOME
1416
from .. import exceptions
17+
from ..storage.base import Storage
1518
from ..sync.exceptions import IdentConflict
1619
from ..sync.exceptions import PartialSync
1720
from ..sync.exceptions import StorageEmpty
@@ -27,7 +30,7 @@
2730

2831
class _StorageIndex:
2932
def __init__(self):
30-
self._storages = {
33+
self._storages: dict[str, str] = {
3134
"caldav": "vdirsyncer.storage.dav.CalDAVStorage",
3235
"carddav": "vdirsyncer.storage.dav.CardDAVStorage",
3336
"filesystem": "vdirsyncer.storage.filesystem.FilesystemStorage",
@@ -37,7 +40,7 @@ def __init__(self):
3740
"google_contacts": "vdirsyncer.storage.google.GoogleContactsStorage",
3841
}
3942

40-
def __getitem__(self, name):
43+
def __getitem__(self, name: str) -> Storage:
4144
item = self._storages[name]
4245
if not isinstance(item, str):
4346
return item
@@ -154,13 +157,18 @@ def handle_cli_error(status_name=None, e=None):
154157
cli_logger.debug("".join(tb))
155158

156159

157-
def get_status_name(pair, collection):
160+
def get_status_name(pair: str, collection: str | None) -> str:
158161
if collection is None:
159162
return pair
160163
return pair + "/" + collection
161164

162165

163-
def get_status_path(base_path, pair, collection=None, data_type=None):
166+
def get_status_path(
167+
base_path: str,
168+
pair: str,
169+
collection: str | None = None,
170+
data_type: str | None = None,
171+
) -> str:
164172
assert data_type is not None
165173
status_name = get_status_name(pair, collection)
166174
path = expand_path(os.path.join(base_path, status_name))
@@ -174,7 +182,12 @@ def get_status_path(base_path, pair, collection=None, data_type=None):
174182
return path
175183

176184

177-
def load_status(base_path, pair, collection=None, data_type=None):
185+
def load_status(
186+
base_path: str,
187+
pair: str,
188+
collection: str | None = None,
189+
data_type: str | None = None
190+
) -> dict | None:
178191
path = get_status_path(base_path, pair, collection, data_type)
179192
if not os.path.exists(path):
180193
return None
@@ -189,7 +202,7 @@ def load_status(base_path, pair, collection=None, data_type=None):
189202
return {}
190203

191204

192-
def prepare_status_path(path):
205+
def prepare_status_path(path: str) -> None:
193206
dirname = os.path.dirname(path)
194207

195208
try:
@@ -200,7 +213,7 @@ def prepare_status_path(path):
200213

201214

202215
@contextlib.contextmanager
203-
def manage_sync_status(base_path, pair_name, collection_name):
216+
def manage_sync_status(base_path: str, pair_name: str, collection_name: str):
204217
path = get_status_path(base_path, pair_name, collection_name, "items")
205218
status = None
206219
legacy_status = None
@@ -225,7 +238,13 @@ def manage_sync_status(base_path, pair_name, collection_name):
225238
yield status
226239

227240

228-
def save_status(base_path, pair, collection=None, data_type=None, data=None):
241+
def save_status(
242+
base_path: str,
243+
pair: str,
244+
collection: str | None = None,
245+
data_type: str | None = None,
246+
data: dict | None = None,
247+
) -> None:
229248
assert data_type is not None
230249
assert data is not None
231250
status_name = get_status_name(pair, collection)
@@ -319,7 +338,7 @@ def handle_storage_init_error(cls, config):
319338
)
320339

321340

322-
def assert_permissions(path, wanted):
341+
def assert_permissions(path: str, wanted: int) -> None:
323342
permissions = os.stat(path).st_mode & 0o777
324343
if permissions > wanted:
325344
cli_logger.warning(

0 commit comments

Comments
 (0)