Skip to content

Commit 4a999e3

Browse files
committed
Add type guards to satify mypy
1 parent 1aedf0b commit 4a999e3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

garminconnect/__init__.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,16 @@ def login(self, /, tokenstore: str | None = None) -> tuple[str | None, str | Non
403403
raise GarminConnectAuthenticationError(
404404
"Failed to retrieve profile"
405405
) from e
406-
if not prof or "displayName" not in prof:
406+
if not prof or not isinstance(prof, dict) or "displayName" not in prof:
407407
raise GarminConnectAuthenticationError("Invalid profile data found")
408408
# Use profile data directly since garth.profile is read-only
409409
self.display_name = prof.get("displayName")
410410
self.full_name = prof.get("fullName")
411411
else:
412-
self.display_name = self.garth.profile.get("displayName")
413-
self.full_name = self.garth.profile.get("fullName")
412+
profile = self.garth.profile
413+
if isinstance(profile, dict):
414+
self.display_name = profile.get("displayName")
415+
self.full_name = profile.get("fullName")
414416

415417
settings = self.garth.connectapi(self.garmin_connect_user_settings_url)
416418

@@ -419,7 +421,7 @@ def login(self, /, tokenstore: str | None = None) -> tuple[str | None, str | Non
419421
"Failed to retrieve user settings"
420422
)
421423

422-
if "userData" not in settings:
424+
if not isinstance(settings, dict) or "userData" not in settings:
423425
raise GarminConnectAuthenticationError("Invalid user settings found")
424426

425427
self.unit_system = settings["userData"].get("measurementSystem")
@@ -477,11 +479,13 @@ def resume_login(
477479
result1, result2 = self.garth.resume_login(client_state, mfa_code)
478480

479481
if self.garth.profile:
480-
self.display_name = self.garth.profile["displayName"]
481-
self.full_name = self.garth.profile["fullName"]
482+
profile = self.garth.profile
483+
if isinstance(profile, dict):
484+
self.display_name = profile.get("displayName")
485+
self.full_name = profile.get("fullName")
482486

483487
settings = self.garth.connectapi(self.garmin_connect_user_settings_url)
484-
if settings and "userData" in settings:
488+
if settings and isinstance(settings, dict) and "userData" in settings:
485489
self.unit_system = settings["userData"]["measurementSystem"]
486490

487491
return result1, result2

0 commit comments

Comments
 (0)