Skip to content

Commit 5ea674d

Browse files
authored
Merge pull request #135 from ivanik7/fetch-reports-list-type
Update fetch_reports to accept lists with HasHashedPublicKey and RolingKeyPairSource at the same time
2 parents 4b1dfcc + a2d705c commit 5ea674d

File tree

2 files changed

+42
-119
lines changed

2 files changed

+42
-119
lines changed

findmy/reports/account.py

Lines changed: 27 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,6 @@ def fetch_reports(
237237
date_to: datetime | None,
238238
) -> MaybeCoro[list[LocationReport]]: ...
239239

240-
@overload
241-
@abstractmethod
242-
def fetch_reports(
243-
self,
244-
keys: Sequence[HasHashedPublicKey],
245-
date_from: datetime,
246-
date_to: datetime | None,
247-
) -> MaybeCoro[dict[HasHashedPublicKey, list[LocationReport]]]: ...
248-
249240
@overload
250241
@abstractmethod
251242
def fetch_reports(
@@ -256,26 +247,24 @@ def fetch_reports(
256247
) -> MaybeCoro[list[LocationReport]]: ...
257248

258249
@overload
250+
@abstractmethod
259251
def fetch_reports(
260252
self,
261-
keys: Sequence[RollingKeyPairSource],
253+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
262254
date_from: datetime,
263255
date_to: datetime | None,
264-
) -> MaybeCoro[dict[RollingKeyPairSource, list[LocationReport]]]: ...
256+
) -> MaybeCoro[dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]]: ...
265257

266258
@abstractmethod
267259
def fetch_reports(
268260
self,
269261
keys: HasHashedPublicKey
270-
| Sequence[HasHashedPublicKey]
271-
| RollingKeyPairSource
272-
| Sequence[RollingKeyPairSource],
262+
| Sequence[HasHashedPublicKey | RollingKeyPairSource]
263+
| RollingKeyPairSource,
273264
date_from: datetime,
274265
date_to: datetime | None,
275266
) -> MaybeCoro[
276-
list[LocationReport]
277-
| dict[HasHashedPublicKey, list[LocationReport]]
278-
| dict[RollingKeyPairSource, list[LocationReport]]
267+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
279268
]:
280269
"""
281270
Fetch location reports for `HasHashedPublicKey`s between `date_from` and `date_end`.
@@ -292,14 +281,6 @@ def fetch_last_reports(
292281
hours: int = 7 * 24,
293282
) -> MaybeCoro[list[LocationReport]]: ...
294283

295-
@overload
296-
@abstractmethod
297-
def fetch_last_reports(
298-
self,
299-
keys: Sequence[HasHashedPublicKey],
300-
hours: int = 7 * 24,
301-
) -> MaybeCoro[dict[HasHashedPublicKey, list[LocationReport]]]: ...
302-
303284
@overload
304285
@abstractmethod
305286
def fetch_last_reports(
@@ -312,22 +293,19 @@ def fetch_last_reports(
312293
@abstractmethod
313294
def fetch_last_reports(
314295
self,
315-
keys: Sequence[RollingKeyPairSource],
296+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
316297
hours: int = 7 * 24,
317-
) -> MaybeCoro[dict[RollingKeyPairSource, list[LocationReport]]]: ...
298+
) -> MaybeCoro[dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]]: ...
318299

319300
@abstractmethod
320301
def fetch_last_reports(
321302
self,
322303
keys: HasHashedPublicKey
323-
| Sequence[HasHashedPublicKey]
324304
| RollingKeyPairSource
325-
| Sequence[RollingKeyPairSource],
305+
| Sequence[HasHashedPublicKey | RollingKeyPairSource],
326306
hours: int = 7 * 24,
327307
) -> MaybeCoro[
328-
list[LocationReport]
329-
| dict[HasHashedPublicKey, list[LocationReport]]
330-
| dict[RollingKeyPairSource, list[LocationReport]]
308+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
331309
]:
332310
"""
333311
Fetch location reports for a sequence of `HasHashedPublicKey`s for the last `hours` hours.
@@ -665,14 +643,6 @@ async def fetch_reports(
665643
date_to: datetime | None,
666644
) -> list[LocationReport]: ...
667645

668-
@overload
669-
async def fetch_reports(
670-
self,
671-
keys: Sequence[HasHashedPublicKey],
672-
date_from: datetime,
673-
date_to: datetime | None,
674-
) -> dict[HasHashedPublicKey, list[LocationReport]]: ...
675-
676646
@overload
677647
async def fetch_reports(
678648
self,
@@ -684,25 +654,22 @@ async def fetch_reports(
684654
@overload
685655
async def fetch_reports(
686656
self,
687-
keys: Sequence[RollingKeyPairSource],
657+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
688658
date_from: datetime,
689659
date_to: datetime | None,
690-
) -> dict[RollingKeyPairSource, list[LocationReport]]: ...
660+
) -> dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]: ...
691661

692662
@require_login_state(LoginState.LOGGED_IN)
693663
@override
694664
async def fetch_reports(
695665
self,
696666
keys: HasHashedPublicKey
697-
| Sequence[HasHashedPublicKey]
698667
| RollingKeyPairSource
699-
| Sequence[RollingKeyPairSource],
668+
| Sequence[HasHashedPublicKey | RollingKeyPairSource],
700669
date_from: datetime,
701670
date_to: datetime | None,
702671
) -> (
703-
list[LocationReport]
704-
| dict[HasHashedPublicKey, list[LocationReport]]
705-
| dict[RollingKeyPairSource, list[LocationReport]]
672+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
706673
):
707674
"""See `BaseAppleAccount.fetch_reports`."""
708675
date_to = date_to or datetime.now().astimezone()
@@ -720,13 +687,6 @@ async def fetch_last_reports(
720687
hours: int = 7 * 24,
721688
) -> list[LocationReport]: ...
722689

723-
@overload
724-
async def fetch_last_reports(
725-
self,
726-
keys: Sequence[HasHashedPublicKey],
727-
hours: int = 7 * 24,
728-
) -> dict[HasHashedPublicKey, list[LocationReport]]: ...
729-
730690
@overload
731691
async def fetch_last_reports(
732692
self,
@@ -737,23 +697,20 @@ async def fetch_last_reports(
737697
@overload
738698
async def fetch_last_reports(
739699
self,
740-
keys: Sequence[RollingKeyPairSource],
700+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
741701
hours: int = 7 * 24,
742-
) -> dict[RollingKeyPairSource, list[LocationReport]]: ...
702+
) -> dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]: ...
743703

744704
@require_login_state(LoginState.LOGGED_IN)
745705
@override
746706
async def fetch_last_reports(
747707
self,
748708
keys: HasHashedPublicKey
749-
| Sequence[HasHashedPublicKey]
750709
| RollingKeyPairSource
751-
| Sequence[RollingKeyPairSource],
710+
| Sequence[HasHashedPublicKey | RollingKeyPairSource],
752711
hours: int = 7 * 24,
753712
) -> (
754-
list[LocationReport]
755-
| dict[HasHashedPublicKey, list[LocationReport]]
756-
| dict[RollingKeyPairSource, list[LocationReport]]
713+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
757714
):
758715
"""See `BaseAppleAccount.fetch_last_reports`."""
759716
end = datetime.now(tz=timezone.utc)
@@ -1093,14 +1050,6 @@ def fetch_reports(
10931050
date_to: datetime | None,
10941051
) -> list[LocationReport]: ...
10951052

1096-
@overload
1097-
def fetch_reports(
1098-
self,
1099-
keys: Sequence[HasHashedPublicKey],
1100-
date_from: datetime,
1101-
date_to: datetime | None,
1102-
) -> dict[HasHashedPublicKey, list[LocationReport]]: ...
1103-
11041053
@overload
11051054
def fetch_reports(
11061055
self,
@@ -1112,24 +1061,21 @@ def fetch_reports(
11121061
@overload
11131062
def fetch_reports(
11141063
self,
1115-
keys: Sequence[RollingKeyPairSource],
1064+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
11161065
date_from: datetime,
11171066
date_to: datetime | None,
1118-
) -> dict[RollingKeyPairSource, list[LocationReport]]: ...
1067+
) -> dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]: ...
11191068

11201069
@override
11211070
def fetch_reports(
11221071
self,
11231072
keys: HasHashedPublicKey
1124-
| Sequence[HasHashedPublicKey]
1125-
| RollingKeyPairSource
1126-
| Sequence[RollingKeyPairSource],
1073+
| Sequence[HasHashedPublicKey | RollingKeyPairSource]
1074+
| RollingKeyPairSource,
11271075
date_from: datetime,
11281076
date_to: datetime | None,
11291077
) -> (
1130-
list[LocationReport]
1131-
| dict[HasHashedPublicKey, list[LocationReport]]
1132-
| dict[RollingKeyPairSource, list[LocationReport]]
1078+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
11331079
):
11341080
"""See `AsyncAppleAccount.fetch_reports`."""
11351081
coro = self._asyncacc.fetch_reports(keys, date_from, date_to)
@@ -1142,13 +1088,6 @@ def fetch_last_reports(
11421088
hours: int = 7 * 24,
11431089
) -> list[LocationReport]: ...
11441090

1145-
@overload
1146-
def fetch_last_reports(
1147-
self,
1148-
keys: Sequence[HasHashedPublicKey],
1149-
hours: int = 7 * 24,
1150-
) -> dict[HasHashedPublicKey, list[LocationReport]]: ...
1151-
11521091
@overload
11531092
def fetch_last_reports(
11541093
self,
@@ -1159,22 +1098,19 @@ def fetch_last_reports(
11591098
@overload
11601099
def fetch_last_reports(
11611100
self,
1162-
keys: Sequence[RollingKeyPairSource],
1101+
keys: Sequence[HasHashedPublicKey | RollingKeyPairSource],
11631102
hours: int = 7 * 24,
1164-
) -> dict[RollingKeyPairSource, list[LocationReport]]: ...
1103+
) -> dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]: ...
11651104

11661105
@override
11671106
def fetch_last_reports(
11681107
self,
11691108
keys: HasHashedPublicKey
1170-
| Sequence[HasHashedPublicKey]
11711109
| RollingKeyPairSource
1172-
| Sequence[RollingKeyPairSource],
1110+
| Sequence[HasHashedPublicKey | RollingKeyPairSource],
11731111
hours: int = 7 * 24,
11741112
) -> (
1175-
list[LocationReport]
1176-
| dict[HasHashedPublicKey, list[LocationReport]]
1177-
| dict[RollingKeyPairSource, list[LocationReport]]
1113+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
11781114
):
11791115
"""See `AsyncAppleAccount.fetch_last_reports`."""
11801116
coro = self._asyncacc.fetch_last_reports(keys, hours)

findmy/reports/reports.py

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,6 @@ async def fetch_reports(
245245
device: HasHashedPublicKey,
246246
) -> list[LocationReport]: ...
247247

248-
@overload
249-
async def fetch_reports(
250-
self,
251-
date_from: datetime,
252-
date_to: datetime,
253-
device: Sequence[HasHashedPublicKey],
254-
) -> dict[HasHashedPublicKey, list[LocationReport]]: ...
255-
256248
@overload
257249
async def fetch_reports(
258250
self,
@@ -266,43 +258,34 @@ async def fetch_reports(
266258
self,
267259
date_from: datetime,
268260
date_to: datetime,
269-
device: Sequence[RollingKeyPairSource],
270-
) -> dict[RollingKeyPairSource, list[LocationReport]]: ...
261+
device: Sequence[HasHashedPublicKey | RollingKeyPairSource],
262+
) -> dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]: ...
271263

272264
async def fetch_reports(
273265
self,
274266
date_from: datetime,
275267
date_to: datetime,
276268
device: HasHashedPublicKey
277-
| Sequence[HasHashedPublicKey]
278269
| RollingKeyPairSource
279-
| Sequence[RollingKeyPairSource],
270+
| Sequence[HasHashedPublicKey | RollingKeyPairSource],
280271
) -> (
281-
list[LocationReport]
282-
| dict[HasHashedPublicKey, list[LocationReport]]
283-
| dict[RollingKeyPairSource, list[LocationReport]]
272+
list[LocationReport] | dict[HasHashedPublicKey | RollingKeyPairSource, list[LocationReport]]
284273
):
285274
"""
286275
Fetch location reports for a certain device.
287276
288277
When ``device`` is a single :class:`.HasHashedPublicKey`, this method will return
289278
a list of location reports corresponding to that key.
290-
When ``device`` is a sequence of :class:`.HasHashedPublicKey`s, it will return a dictionary
291-
with the :class:`.HasHashedPublicKey` as key, and a list of location reports as value.
292279
When ``device`` is a :class:`.RollingKeyPairSource`, it will return a list of
293280
location reports corresponding to that source.
281+
When ``device`` is a sequence of :class:`.HasHashedPublicKey`s or RollingKeyPairSource's,
282+
it will return a dictionary with the :class:`.HasHashedPublicKey` or `.RollingKeyPairSource`
283+
as key, and a list of location reports as value.
294284
"""
295-
key_devs: (
296-
dict[HasHashedPublicKey, HasHashedPublicKey]
297-
| dict[HasHashedPublicKey, RollingKeyPairSource]
298-
) = {}
285+
key_devs: dict[HasHashedPublicKey, HasHashedPublicKey | RollingKeyPairSource] = {}
299286
if isinstance(device, HasHashedPublicKey):
300287
# single key
301288
key_devs = {device: device}
302-
elif isinstance(device, list) and all(isinstance(x, HasHashedPublicKey) for x in device):
303-
# multiple static keys
304-
device = cast("list[HasHashedPublicKey]", device)
305-
key_devs = {key: key for key in device}
306289
elif isinstance(device, RollingKeyPairSource):
307290
# key generator
308291
# add 12h margin to the generator
@@ -313,13 +296,17 @@ async def fetch_reports(
313296
date_to + timedelta(hours=12),
314297
)
315298
}
316-
elif isinstance(device, list) and all(isinstance(x, RollingKeyPairSource) for x in device):
299+
300+
elif isinstance(device, list) and all(
301+
isinstance(x, HasHashedPublicKey | RollingKeyPairSource) for x in device
302+
):
317303
# multiple key generators
318304
# add 12h margin to each generator
319-
device = cast("list[RollingKeyPairSource]", device)
320-
key_devs = {
305+
device = cast("list[HasHashedPublicKey | RollingKeyPairSource]", device)
306+
key_devs = {key: key for key in device if isinstance(key, HasHashedPublicKey)} | {
321307
key: dev
322308
for dev in device
309+
if isinstance(dev, RollingKeyPairSource)
323310
for key in dev.keys_between(
324311
date_from - timedelta(hours=12),
325312
date_to + timedelta(hours=12),

0 commit comments

Comments
 (0)