Skip to content

Commit d49d6fc

Browse files
add: typed example
1 parent 3e05095 commit d49d6fc

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

examples/typed_calls.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
The async client can be used as a replacement for the synchronous
3+
IndicoClient to make concurrent calls to the platform
4+
"""
5+
6+
import asyncio
7+
from typing import AsyncIterator, List
8+
9+
from indico import AsyncIndicoClient, IndicoConfig
10+
from indico.queries import CreateDataset
11+
from indico.queries.datasets import GetDataset, ListDatasets
12+
from indico.types.dataset import Dataset
13+
from indico.types.submission import Submission
14+
15+
"""
16+
Example illustrating how to use the client in typed contexts
17+
"""
18+
19+
config = IndicoConfig(host="try.indico.io")
20+
21+
22+
async def main():
23+
async with AsyncIndicoClient(config=config) as client:
24+
ipa_version: str = await client.get_ipa_version()
25+
print(ipa_version)
26+
27+
filename: str = "my_file_for_all_datasets.pdf"
28+
29+
# CreateDataset is typed to return a Dataset, so multiple concurrent calls
30+
# via asyncio.gather should, and does, return List[Dataset]
31+
datasets: List[Dataset] = await asyncio.gather(
32+
*(
33+
client.call(CreateDataset(name=f"My Dataset {i}", files=[filename]))
34+
for i in range(1, 4)
35+
)
36+
)
37+
assert len(datasets) == 3
38+
39+
# paginated calls are also properly typed
40+
pages: AsyncIterator[List[Dataset]] = client.paginate(ListDatasets())
41+
async for datasets in pages:
42+
for d in datasets:
43+
print(d.id)
44+
45+
# incorrect typing will throw mypy / ide linting errors when using those tools.
46+
# here, Pyright correctly reports '"Dataset" is not the same as "Submission"'
47+
not_a_submission: Submission = await client.call(GetDataset(datasets[0].id))
48+
49+
50+
if __name__ == "__main__":
51+
# How to run a Python script using async
52+
asyncio.run(main())

indico/client/request.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ class RequestChain(Generic[ResponseType]):
121121
def requests(
122122
self,
123123
) -> "Iterator[Union[RequestChain[Any], HTTPRequest[Any], Delay]]":
124-
...
124+
raise NotImplementedError(
125+
"RequestChains must define an iterator for their requests;"
126+
"otherwise, subclass GraphQLResponse instead."
127+
)
125128

126129

127130
class Delay:

0 commit comments

Comments
 (0)