diff --git a/src/pyicloud_ipd/base.py b/src/pyicloud_ipd/base.py index 67ef61eb8..fbcfc0949 100644 --- a/src/pyicloud_ipd/base.py +++ b/src/pyicloud_ipd/base.py @@ -1,5 +1,5 @@ import sys -from typing import Any, Dict, NoReturn, Optional, Sequence +from typing import Any, Dict, Iterable, NoReturn, Optional, Sequence from typing_extensions import override import typing from uuid import uuid1 @@ -20,7 +20,7 @@ PyiCloud2SARequiredException, PyiCloudServiceNotActivatedException, ) -from pyicloud_ipd.services.findmyiphone import FindMyiPhoneServiceManager +from pyicloud_ipd.services.findmyiphone import AppleDevice, FindMyiPhoneServiceManager from pyicloud_ipd.services.calendar import CalendarService from pyicloud_ipd.services.ubiquity import UbiquityService from pyicloud_ipd.services.contacts import ContactsService @@ -602,14 +602,14 @@ def _get_webservice_url(self, ws_key: str) -> str: return typing.cast(str, self._webservices[ws_key]["url"]) @property - def devices(self): # type: ignore + def devices(self) -> Sequence[AppleDevice]: """ Return all devices.""" service_root = self._get_webservice_url("findme") - return FindMyiPhoneServiceManager( # type: ignore + return typing.cast(Sequence[AppleDevice], FindMyiPhoneServiceManager( service_root, self.session, self.params - ) + )) @property def account(self): # type: ignore diff --git a/src/pyicloud_ipd/cmdline.py b/src/pyicloud_ipd/cmdline.py index a3d56ca12..71c706055 100644 --- a/src/pyicloud_ipd/cmdline.py +++ b/src/pyicloud_ipd/cmdline.py @@ -12,12 +12,13 @@ from pyicloud_ipd.base import PyiCloudService from pyicloud_ipd.exceptions import PyiCloudFailedLoginException +from pyicloud_ipd.services.findmyiphone import AppleDevice from . import utils DEVICE_ERROR = "Please use the --device switch to indicate which device to use." -def create_pickled_data(idevice, filename: str) -> None: +def create_pickled_data(idevice: AppleDevice, filename: str) -> None: """ This helper will output the idevice to a pickled file named after the passed filename. diff --git a/src/pyicloud_ipd/services/findmyiphone.py b/src/pyicloud_ipd/services/findmyiphone.py index e2cd17fdb..27a1d0be8 100644 --- a/src/pyicloud_ipd/services/findmyiphone.py +++ b/src/pyicloud_ipd/services/findmyiphone.py @@ -1,6 +1,7 @@ -# mypy: ignore-errors import json import sys +from typing import Any, Dict, Iterable, Optional, Sequence +import typing import six @@ -15,7 +16,7 @@ class FindMyiPhoneServiceManager(object): """ - def __init__(self, service_root, session, params): + def __init__(self, service_root: str, session:Any, params: Dict[str, Any]): self.session = session self.params = params self._service_root = service_root @@ -25,10 +26,10 @@ def __init__(self, service_root, session, params): self._fmip_message_url = '%s/sendMessage' % self._fmip_endpoint self._fmip_lost_url = '%s/lostDevice' % self._fmip_endpoint - self._devices = {} + self._devices:Dict[str, AppleDevice] = {} self.refresh_client() - def refresh_client(self): + def refresh_client(self) -> None: """ Refreshes the FindMyiPhoneService endpoint, This ensures that the location data is up-to-date. @@ -67,35 +68,35 @@ def refresh_client(self): if not self._devices: raise PyiCloudNoDevicesException() - def __getitem__(self, key): + def __getitem__(self, key: Any) -> "AppleDevice": if isinstance(key, int): if six.PY3: - key = list(self.keys())[key] + key = list(self.keys())[key] # type: ignore[operator] else: key = self.keys()[key] return self._devices[key] - def __getattr__(self, attr): - return getattr(self._devices, attr) + def __getattr__(self, attr: str) -> "AppleDevice": + return typing.cast(AppleDevice, getattr(self._devices, attr)) - def __unicode__(self): + def __unicode__(self) -> str: return six.text_type(self._devices) - def __str__(self): + def __str__(self) -> str: as_unicode = self.__unicode__() if sys.version_info[0] >= 3: return as_unicode else: return as_unicode.encode('ascii', 'ignore') - def __repr__(self): + def __repr__(self) -> str: return six.text_type(self) - + class AppleDevice(object): def __init__( - self, content, session, params, manager, - sound_url=None, lost_url=None, message_url=None + self, content:Dict[str,Any], session:Any, params:Dict[str, Any], manager: FindMyiPhoneServiceManager, + sound_url:Optional[str]=None, lost_url:Optional[str]=None, message_url:Optional[str]=None ): self.content = content self.manager = manager @@ -106,14 +107,14 @@ def __init__( self.lost_url = lost_url self.message_url = message_url - def update(self, data): + def update(self, data: Dict[str, Any]) -> None: self.content = data - def location(self): + def location(self) -> Any: self.manager.refresh_client() return self.content['location'] - def status(self, additional=[]): + def status(self, additional:Sequence[str]=[]) -> Dict[str, Any]: """ Returns status information for device. This returns only a subset of possible properties. @@ -121,12 +122,12 @@ def status(self, additional=[]): self.manager.refresh_client() fields = ['batteryLevel', 'deviceDisplayName', 'deviceStatus', 'name'] fields += additional - properties = {} + properties: Dict[str, Any] = {} for field in fields: properties[field] = self.content.get(field) return properties - def play_sound(self, subject='Find My iPhone Alert'): + def play_sound(self, subject:str='Find My iPhone Alert') -> None: """ Send a request to the device to play a sound. It's possible to pass a custom message by changing the `subject`. @@ -145,9 +146,9 @@ def play_sound(self, subject='Find My iPhone Alert'): ) def display_message( - self, subject='Find My iPhone Alert', message="This is a note", - sounds=False - ): + self, subject:str='Find My iPhone Alert', message:str="This is a note", + sounds:bool=False + ) -> None: """ Send a request to the device to play a sound. It's possible to pass a custom message by changing the `subject`. @@ -168,10 +169,10 @@ def display_message( ) def lost_device( - self, number, - text='This iPhone has been lost. Please call me.', - newpasscode="" - ): + self, number:str, + text:str='This iPhone has been lost. Please call me.', + newpasscode:str="" + ) -> None: """ Send a request to the device to trigger 'lost mode'. The device will show the message in `text`, and if a number has @@ -194,16 +195,16 @@ def lost_device( ) @property - def data(self): + def data(self) -> Dict[str, Any]: return self.content - def __getitem__(self, key): + def __getitem__(self, key: str) -> Any: return self.content[key] - def __getattr__(self, attr): + def __getattr__(self, attr: str) -> Any: return getattr(self.content, attr) - def __unicode__(self): + def __unicode__(self) -> str: display_name = self['deviceDisplayName'] name = self['name'] return '%s: %s' % ( @@ -211,12 +212,12 @@ def __unicode__(self): name, ) - def __str__(self): + def __str__(self) -> str: as_unicode = self.__unicode__() if sys.version_info[0] >= 3: return as_unicode else: return as_unicode.encode('ascii', 'ignore') - def __repr__(self): + def __repr__(self) -> str: return '' % str(self)