From 80e15b062228d105b980240833ce0dd4cbb128cb Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sat, 4 Jun 2022 18:47:54 +0300 Subject: [PATCH] fix type errors with mypy --strict --- sync_ics2gcal/gcal.py | 61 ++++++++++++++++--------------- sync_ics2gcal/ical.py | 10 ++--- sync_ics2gcal/manage_calendars.py | 6 +-- sync_ics2gcal/sync.py | 2 +- sync_ics2gcal/sync_calendar.py | 2 +- 5 files changed, 41 insertions(+), 40 deletions(-) diff --git a/sync_ics2gcal/gcal.py b/sync_ics2gcal/gcal.py index 7fce052..6e0995a 100644 --- a/sync_ics2gcal/gcal.py +++ b/sync_ics2gcal/gcal.py @@ -89,6 +89,9 @@ class EventsSearchResults(NamedTuple): new: List[EventData] +BatchRequestCallback = Callable[[str, Any, Optional[Exception]], None] + + class GoogleCalendarService: """class for make google calendar service Resource @@ -97,7 +100,7 @@ class GoogleCalendarService: """ @staticmethod - def default(): + def default() -> discovery.Resource: """make service Resource from default credentials (authorize) ( https://developers.google.com/identity/protocols/application-default-credentials ) ( https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#google.auth.default ) @@ -111,7 +114,7 @@ class GoogleCalendarService: return service @staticmethod - def from_srv_acc_file(service_account_file: str): + def from_srv_acc_file(service_account_file: str) -> discovery.Resource: """make service Resource from service account filename (authorize)""" scopes = ["https://www.googleapis.com/auth/calendar"] @@ -125,7 +128,7 @@ class GoogleCalendarService: return service @staticmethod - def from_config(config: Optional[Dict[str, Optional[str]]] = None): + def from_config(config: Optional[Dict[str, str]] = None) -> discovery.Resource: """make service Resource from config dict Arguments: @@ -137,7 +140,7 @@ class GoogleCalendarService: """ if config is not None and "service_account" in config: - service_account_filename: str = str(config["service_account"]) + service_account_filename: str = config["service_account"] service = GoogleCalendarService.from_srv_acc_file(service_account_filename) else: service = GoogleCalendarService.default() @@ -171,7 +174,9 @@ class GoogleCalendar: self.service: discovery.Resource = service self.calendar_id: str = str(calendar_id) - def _make_request_callback(self, action: str, events_by_req: EventList) -> Callable: + def _make_request_callback( + self, action: str, events_by_req: EventList + ) -> BatchRequestCallback: """make callback for log result of batch request Arguments: @@ -182,7 +187,9 @@ class GoogleCalendar: callback function """ - def callback(request_id: str, response: Any, exception: Optional[Exception]): + def callback( + request_id: str, response: Any, exception: Optional[Exception] + ) -> None: event: EventData = events_by_req[int(request_id)] event_key: Optional[str] = select_event_key(event) key: str = event_key if event_key is not None else "" @@ -232,7 +239,7 @@ class GoogleCalendar: self.logger.info("%d events listed", len(events)) return events - def find_exists(self, events: List) -> EventsSearchResults: + def find_exists(self, events: EventList) -> EventsSearchResults: """find existing events from list, by 'iCalUID' field Arguments: @@ -250,7 +257,7 @@ class GoogleCalendar: def list_callback( request_id: str, response: Any, exception: Optional[Exception] - ): + ) -> None: found: bool = False cur_event: EventData = events_by_req[int(request_id)] if exception is None: @@ -284,7 +291,7 @@ class GoogleCalendar: self.logger.info("%d events exists, %d not found", len(exists), len(not_found)) return EventsSearchResults(exists, not_found) - def insert_events(self, events: EventList): + def insert_events(self, events: EventList) -> None: """insert list of events Arguments: @@ -308,7 +315,7 @@ class GoogleCalendar: i += 1 batch.execute() - def patch_events(self, event_tuples: List[EventTuple]): + def patch_events(self, event_tuples: List[EventTuple]) -> None: """patch (update) events Arguments: @@ -335,7 +342,7 @@ class GoogleCalendar: i += 1 batch.execute() - def update_events(self, event_tuples: List[EventTuple]): + def update_events(self, event_tuples: List[EventTuple]) -> None: """update events Arguments: @@ -364,7 +371,7 @@ class GoogleCalendar: i += 1 batch.execute() - def delete_events(self, events: EventList): + def delete_events(self, events: EventList) -> None: """delete events Arguments: @@ -400,7 +407,7 @@ class GoogleCalendar: calendar Resource """ - calendar: CalendarData = CalendarData(summary=summary) + calendar = CalendarData(summary=summary) if time_zone is not None: calendar["timeZone"] = time_zone @@ -408,33 +415,27 @@ class GoogleCalendar: self.calendar_id = created_calendar["id"] return created_calendar - def delete(self): + def delete(self) -> None: """delete calendar""" self.service.calendars().delete(calendarId=self.calendar_id).execute() - def make_public(self): + def make_public(self) -> None: """make calendar public""" - rule_public: ACLRule = ACLRule(scope=ACLScope(type="default"), role="reader") - return ( - self.service.acl() - .insert(calendarId=self.calendar_id, body=rule_public) - .execute() - ) + rule_public = ACLRule(scope=ACLScope(type="default"), role="reader") + self.service.acl().insert( + calendarId=self.calendar_id, body=rule_public + ).execute() - def add_owner(self, email: str): + def add_owner(self, email: str) -> None: """add calendar owner by email Arguments: email -- email to add """ - rule_owner: ACLRule = ACLRule( - scope=ACLScope(type="user", value=email), role="owner" - ) - return ( - self.service.acl() - .insert(calendarId=self.calendar_id, body=rule_owner) - .execute() - ) + rule_owner = ACLRule(scope=ACLScope(type="user", value=email), role="owner") + self.service.acl().insert( + calendarId=self.calendar_id, body=rule_owner + ).execute() diff --git a/sync_ics2gcal/ical.py b/sync_ics2gcal/ical.py index 57e2c31..bef526b 100644 --- a/sync_ics2gcal/ical.py +++ b/sync_ics2gcal/ical.py @@ -60,7 +60,7 @@ def gcal_date_or_datetime( return result -class EventConverter(Event): +class EventConverter(Event): # type: ignore """Convert icalendar event to google calendar resource ( https://developers.google.com/calendar/v3/reference/events#resource-representations ) """ @@ -75,7 +75,7 @@ class EventConverter(Event): string value """ - return self.decoded(prop).decode(encoding="utf-8") + return str(self.decoded(prop).decode(encoding="utf-8")) def _datetime_str_prop(self, prop: str) -> str: """utc datetime as string from property @@ -131,7 +131,7 @@ class EventConverter(Event): prop: EventDataKey, func: Callable[[str], str], ics_prop: Optional[str] = None, - ): + ) -> None: """get property from ical event if existed, and put to gcal event Arguments: @@ -179,13 +179,13 @@ class CalendarConverter: def __init__(self, calendar: Optional[Calendar] = None): self.calendar: Optional[Calendar] = calendar - def load(self, filename: str): + def load(self, filename: str) -> None: """load calendar from ics file""" with open(filename, "r", encoding="utf-8") as f: self.calendar = Calendar.from_ical(f.read()) self.logger.info("%s loaded", filename) - def loads(self, string: str): + def loads(self, string: str) -> None: """load calendar from ics string""" self.calendar = Calendar.from_ical(string) diff --git a/sync_ics2gcal/manage_calendars.py b/sync_ics2gcal/manage_calendars.py index dfcb6f5..833380c 100644 --- a/sync_ics2gcal/manage_calendars.py +++ b/sync_ics2gcal/manage_calendars.py @@ -8,7 +8,7 @@ from . import GoogleCalendar, GoogleCalendarService def load_config(filename: str) -> Optional[Dict[str, Any]]: - result = None + result: Optional[Dict[str, Any]] = None try: with open(filename, "r", encoding="utf-8") as f: result = yaml.safe_load(f) @@ -21,7 +21,7 @@ def load_config(filename: str) -> Optional[Dict[str, Any]]: class PropertyCommands: """get/set google calendar properties""" - def __init__(self, _service): + def __init__(self, _service: Any) -> None: self._service = _service def get(self, calendar_id: str, property_name: str) -> None: @@ -146,7 +146,7 @@ class Commands: print("{}: {}".format(summary, calendar_id)) -def main(): +def main() -> None: fire.Fire(Commands, name="manage-ics2gcal") diff --git a/sync_ics2gcal/sync.py b/sync_ics2gcal/sync.py index 75cc03c..a1bc5cc 100644 --- a/sync_ics2gcal/sync.py +++ b/sync_ics2gcal/sync.py @@ -77,7 +77,7 @@ class CalendarSync: return ComparedEvents(items_to_insert, items_to_update, items_to_delete) - def _filter_events_to_update(self): + def _filter_events_to_update(self) -> None: """filter 'to_update' events by 'updated' datetime""" def filter_updated(event_tuple: EventTuple) -> bool: diff --git a/sync_ics2gcal/sync_calendar.py b/sync_ics2gcal/sync_calendar.py index e744687..8b1be4c 100644 --- a/sync_ics2gcal/sync_calendar.py +++ b/sync_ics2gcal/sync_calendar.py @@ -14,7 +14,7 @@ ConfigDate = Union[str, datetime.datetime] def load_config() -> Dict[str, Any]: with open("config.yml", "r", encoding="utf-8") as f: result = yaml.safe_load(f) - return result + return result # type: ignore def get_start_date(date: ConfigDate) -> datetime.datetime: