Add some type hints

This commit is contained in:
Hugo Osvaldo Barrera 2023-05-09 11:49:01 +02:00
parent 079a156bf8
commit c55b969791

View file

@ -13,6 +13,9 @@ import contextlib
import itertools import itertools
import logging import logging
from vdirsyncer.storage.base import Storage
from vdirsyncer.vobject import Item
from ..exceptions import UserError from ..exceptions import UserError
from ..utils import uniq from ..utils import uniq
from .exceptions import BothReadOnly from .exceptions import BothReadOnly
@ -21,6 +24,7 @@ from .exceptions import PartialSync
from .exceptions import StorageEmpty from .exceptions import StorageEmpty
from .exceptions import SyncConflict from .exceptions import SyncConflict
from .status import ItemMetadata from .status import ItemMetadata
from .status import SqliteStatus
from .status import SubStatus from .status import SubStatus
sync_logger = logging.getLogger(__name__) sync_logger = logging.getLogger(__name__)
@ -30,22 +34,22 @@ class _StorageInfo:
"""A wrapper class that holds prefetched items, the status and other """A wrapper class that holds prefetched items, the status and other
things.""" things."""
def __init__(self, storage, status): def __init__(self, storage: Storage, status: SubStatus):
self.storage = storage self.storage = storage
self.status = status self.status = status
self._item_cache = {} self._item_cache = {} # type: ignore[var-annotated]
async def prepare_new_status(self): async def prepare_new_status(self) -> bool:
storage_nonempty = False storage_nonempty = False
prefetch = [] prefetch = []
def _store_props(ident, props): def _store_props(ident: str, props: ItemMetadata) -> None:
try: try:
self.status.insert_ident(ident, props) self.status.insert_ident(ident, props)
except IdentAlreadyExists as e: except IdentAlreadyExists as e:
raise e.to_ident_conflict(self.storage) raise e.to_ident_conflict(self.storage)
async for href, etag in self.storage.list(): async for href, etag in self.storage.list(): # type: ignore[attr-defined]
storage_nonempty = True storage_nonempty = True
ident, meta = self.status.get_by_href(href) ident, meta = self.status.get_by_href(href)
@ -68,7 +72,7 @@ class _StorageInfo:
return storage_nonempty return storage_nonempty
def is_changed(self, ident): def is_changed(self, ident: str) -> bool:
old_meta = self.status.get(ident) old_meta = self.status.get(ident)
if old_meta is None: # new item if old_meta is None: # new item
return True return True
@ -81,30 +85,28 @@ class _StorageInfo:
and (old_meta.hash is None or new_meta.hash != old_meta.hash) and (old_meta.hash is None or new_meta.hash != old_meta.hash)
) )
def set_item_cache(self, ident, item): def set_item_cache(self, ident, item) -> None:
actual_hash = self.status.get_new(ident).hash actual_hash = self.status.get_new(ident).hash
assert actual_hash == item.hash assert actual_hash == item.hash
self._item_cache[ident] = item self._item_cache[ident] = item
def get_item_cache(self, ident): def get_item_cache(self, ident: str) -> Item:
return self._item_cache[ident] return self._item_cache[ident]
async def sync( async def sync(
storage_a, storage_a: Storage,
storage_b, storage_b: Storage,
status, status: SqliteStatus,
conflict_resolution=None, conflict_resolution=None,
force_delete=False, force_delete=False,
error_callback=None, error_callback=None,
partial_sync="revert", partial_sync="revert",
): ) -> None:
"""Synchronizes two storages. """Synchronizes two storages.
:param storage_a: The first storage :param storage_a: The first storage
:type storage_a: :class:`vdirsyncer.storage.base.Storage`
:param storage_b: The second storage :param storage_b: The second storage
:type storage_b: :class:`vdirsyncer.storage.base.Storage`
:param status: {ident: (href_a, etag_a, href_b, etag_b)} :param status: {ident: (href_a, etag_a, href_b, etag_b)}
metadata about the two storages for detection of changes. Will be metadata about the two storages for detection of changes. Will be
modified by the function and should be passed to it at the next sync. modified by the function and should be passed to it at the next sync.