Skip to content

Commit a9efacc

Browse files
committed
Use pre-PEP585 types for compatibility with 3.8
1 parent 1058086 commit a9efacc

File tree

10 files changed

+60
-58
lines changed

10 files changed

+60
-58
lines changed

src/dispatch/coroutine.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22
from types import coroutine
3-
from typing import Any, Awaitable
3+
from typing import Any, Awaitable, List, Tuple
44

55
from dispatch.experimental.durable import durable
66
from dispatch.proto import Call
@@ -16,14 +16,14 @@ def call(call: Call) -> Any:
1616

1717
@coroutine
1818
@durable
19-
def gather(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
19+
def gather(*awaitables: Awaitable[Any]) -> List[Any]: # type: ignore[misc]
2020
"""Alias for all."""
2121
return all(*awaitables)
2222

2323

2424
@coroutine
2525
@durable
26-
def all(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
26+
def all(*awaitables: Awaitable[Any]) -> List[Any]: # type: ignore[misc]
2727
"""Concurrently run a set of coroutines, blocking until all coroutines
2828
return or any coroutine raises an error. If any coroutine fails with an
2929
uncaught exception, the exception will be re-raised here."""
@@ -32,7 +32,7 @@ def all(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
3232

3333
@coroutine
3434
@durable
35-
def any(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
35+
def any(*awaitables: Awaitable[Any]) -> List[Any]: # type: ignore[misc]
3636
"""Concurrently run a set of coroutines, blocking until any coroutine
3737
returns or all coroutines raises an error. If all coroutines fail with
3838
uncaught exceptions, the exception(s) will be re-raised here."""
@@ -41,7 +41,7 @@ def any(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
4141

4242
@coroutine
4343
@durable
44-
def race(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
44+
def race(*awaitables: Awaitable[Any]) -> List[Any]: # type: ignore[misc]
4545
"""Concurrently run a set of coroutines, blocking until any coroutine
4646
returns or raises an error. If any coroutine fails with an uncaught
4747
exception, the exception will be re-raised here."""
@@ -50,17 +50,17 @@ def race(*awaitables: Awaitable[Any]) -> list[Any]: # type: ignore[misc]
5050

5151
@dataclass
5252
class AllDirective:
53-
awaitables: tuple[Awaitable[Any], ...]
53+
awaitables: Tuple[Awaitable[Any], ...]
5454

5555

5656
@dataclass
5757
class AnyDirective:
58-
awaitables: tuple[Awaitable[Any], ...]
58+
awaitables: Tuple[Awaitable[Any], ...]
5959

6060

6161
@dataclass
6262
class RaceDirective:
63-
awaitables: tuple[Awaitable[Any], ...]
63+
awaitables: Tuple[Awaitable[Any], ...]
6464

6565

6666
class AnyException(RuntimeError):
@@ -69,7 +69,7 @@ class AnyException(RuntimeError):
6969

7070
__slots__ = ("exceptions",)
7171

72-
def __init__(self, exceptions: list[Exception]):
72+
def __init__(self, exceptions: List[Exception]):
7373
self.exceptions = exceptions
7474

7575
def __str__(self):

src/dispatch/experimental/durable/function.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
MethodType,
1010
TracebackType,
1111
)
12-
from typing import Any, Callable, Coroutine, Generator, Optional, TypeVar, Union, cast
12+
from typing import Any, Callable, Coroutine, Generator, Optional, TypeVar, Union, cast, Dict, Tuple
1313

1414
from . import frame as ext
1515
from .registry import RegisteredFunction, lookup_function, register_function
@@ -78,8 +78,8 @@ class Serializable:
7878
g: Union[GeneratorType, CoroutineType]
7979
registered_fn: RegisteredFunction
8080
wrapped_coroutine: Union["DurableCoroutine", None]
81-
args: tuple[Any, ...]
82-
kwargs: dict[str, Any]
81+
args: Tuple[Any, ...]
82+
kwargs: Dict[str, Any]
8383

8484
def __init__(
8585
self,
@@ -274,7 +274,7 @@ def cr_await(self) -> Any:
274274
return self.coroutine.cr_await
275275

276276
@property
277-
def cr_origin(self) -> Optional[tuple[tuple[str, int, str], ...]]:
277+
def cr_origin(self) -> Optional[Tuple[Tuple[str, int, str], ...]]:
278278
return self.coroutine.cr_origin
279279

280280
def __repr__(self) -> str:

src/dispatch/experimental/durable/registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22
from dataclasses import dataclass
33
from types import FunctionType
4+
from typing import Dict
45

56

67
@dataclass
@@ -46,7 +47,7 @@ def __setstate__(self, state):
4647
self.hash = code_hash
4748

4849

49-
_REGISTRY: dict[str, RegisteredFunction] = {}
50+
_REGISTRY: Dict[str, RegisteredFunction] = {}
5051

5152

5253
def register_function(fn: FunctionType) -> RegisteredFunction:

src/dispatch/function.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Optional,
1616
TypeVar,
1717
overload,
18+
List
1819
)
1920
from urllib.parse import urlparse
2021

@@ -329,7 +330,7 @@ def batch(self) -> Batch:
329330
a set of calls to dispatch."""
330331
return Batch(self)
331332

332-
def dispatch(self, calls: Iterable[Call]) -> list[DispatchID]:
333+
def dispatch(self, calls: Iterable[Call]) -> List[DispatchID]:
333334
"""Dispatch function calls.
334335
335336
Args:
@@ -369,7 +370,7 @@ class Batch:
369370

370371
def __init__(self, client: Client):
371372
self.client = client
372-
self.calls: list[Call] = []
373+
self.calls: List[Call] = []
373374

374375
def add(self, func: Function[P, T], *args: P.args, **kwargs: P.kwargs) -> Batch:
375376
"""Add a call to the specified function to the batch."""
@@ -380,7 +381,7 @@ def add_call(self, call: Call) -> Batch:
380381
self.calls.append(call)
381382
return self
382383

383-
def dispatch(self) -> list[DispatchID]:
384+
def dispatch(self) -> List[DispatchID]:
384385
"""Dispatch dispatches the calls asynchronously.
385386
386387
The batch is reset when the calls are dispatched successfully.

src/dispatch/proto.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from dataclasses import dataclass
55
from traceback import format_exception
66
from types import TracebackType
7-
from typing import Any, Optional
7+
from typing import Any, Optional, Dict, Tuple, List
88

99
import google.protobuf.any_pb2
1010
import google.protobuf.message
@@ -79,7 +79,7 @@ def input(self) -> Any:
7979
self._assert_first_call()
8080
return self._input
8181

82-
def input_arguments(self) -> tuple[tuple[Any, ...], dict[str, Any]]:
82+
def input_arguments(self) -> Tuple[Tuple[Any, ...], Dict[str, Any]]:
8383
"""Returns positional and keyword arguments carried by the input."""
8484
self._assert_first_call()
8585
if not isinstance(self._input, Arguments):
@@ -92,7 +92,7 @@ def coroutine_state(self) -> Any:
9292
return self._coroutine_state
9393

9494
@property
95-
def call_results(self) -> list[CallResult]:
95+
def call_results(self) -> List[CallResult]:
9696
self._assert_resume()
9797
return self._call_results
9898

@@ -124,7 +124,7 @@ def from_poll_results(
124124
cls,
125125
function: str,
126126
coroutine_state: Any,
127-
call_results: list[CallResult],
127+
call_results: List[CallResult],
128128
error: Optional[Error] = None,
129129
):
130130
return Input(
@@ -143,8 +143,8 @@ def from_poll_results(
143143
class Arguments:
144144
"""A container for positional and keyword arguments."""
145145

146-
args: tuple[Any, ...]
147-
kwargs: dict[str, Any]
146+
args: Tuple[Any, ...]
147+
kwargs: Dict[str, Any]
148148

149149

150150
@dataclass
@@ -201,7 +201,7 @@ def exit(
201201
def poll(
202202
cls,
203203
state: Any,
204-
calls: Optional[list[Call]] = None,
204+
calls: Optional[List[Call]] = None,
205205
min_results: int = 1,
206206
max_results: int = 10,
207207
max_wait_seconds: Optional[int] = None,

src/dispatch/scheduler.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pickle
33
import sys
44
from dataclasses import dataclass, field
5-
from typing import Any, Awaitable, Callable, Optional, Protocol, Union
5+
from typing import Any, Awaitable, Callable, Optional, Protocol, Union, List, Dict, Tuple, Set
66

77
from typing_extensions import TypeAlias
88

@@ -84,9 +84,9 @@ def value(self) -> Any:
8484
class AllFuture:
8585
"""A future result of a dispatch.coroutine.all() operation."""
8686

87-
order: list[CoroutineID] = field(default_factory=list)
88-
waiting: set[CoroutineID] = field(default_factory=set)
89-
results: dict[CoroutineID, CoroutineResult] = field(default_factory=dict)
87+
order: List[CoroutineID] = field(default_factory=list)
88+
waiting: Set[CoroutineID] = field(default_factory=set)
89+
results: Dict[CoroutineID, CoroutineResult] = field(default_factory=dict)
9090
first_error: Optional[Exception] = None
9191

9292
def add_result(self, result: Union[CallResult, CoroutineResult]):
@@ -115,7 +115,7 @@ def error(self) -> Optional[Exception]:
115115
assert self.ready()
116116
return self.first_error
117117

118-
def value(self) -> list[Any]:
118+
def value(self) -> List[Any]:
119119
assert self.ready()
120120
assert len(self.waiting) == 0
121121
assert self.first_error is None
@@ -126,10 +126,10 @@ def value(self) -> list[Any]:
126126
class AnyFuture:
127127
"""A future result of a dispatch.coroutine.any() operation."""
128128

129-
order: list[CoroutineID] = field(default_factory=list)
130-
waiting: set[CoroutineID] = field(default_factory=set)
129+
order: List[CoroutineID] = field(default_factory=list)
130+
waiting: Set[CoroutineID] = field(default_factory=set)
131131
first_result: Optional[CoroutineResult] = None
132-
errors: dict[CoroutineID, Exception] = field(default_factory=dict)
132+
errors: Dict[CoroutineID, Exception] = field(default_factory=dict)
133133
generic_error: Optional[Exception] = None
134134

135135
def add_result(self, result: Union[CallResult, CoroutineResult]):
@@ -183,7 +183,7 @@ def value(self) -> Any:
183183
class RaceFuture:
184184
"""A future result of a dispatch.coroutine.race() operation."""
185185

186-
waiting: set[CoroutineID] = field(default_factory=set)
186+
waiting: Set[CoroutineID] = field(default_factory=set)
187187
first_result: Optional[CoroutineResult] = None
188188
first_error: Optional[Exception] = None
189189

@@ -248,12 +248,12 @@ class State:
248248
"""State of the scheduler and the coroutines it's managing."""
249249

250250
version: str
251-
suspended: dict[CoroutineID, Coroutine]
252-
ready: list[Coroutine]
251+
suspended: Dict[CoroutineID, Coroutine]
252+
ready: List[Coroutine]
253253
next_coroutine_id: int
254254
next_call_id: int
255255

256-
prev_callers: list[Coroutine]
256+
prev_callers: List[Coroutine]
257257

258258
outstanding_calls: int
259259

@@ -416,7 +416,7 @@ def _run(self, input: Input) -> Output:
416416
len(state.ready) + len(state.suspended),
417417
)
418418

419-
pending_calls: list[Call] = []
419+
pending_calls: List[Call] = []
420420
while state.ready:
421421
coroutine = state.ready.pop(0)
422422
logger.debug("running %s", coroutine)
@@ -542,8 +542,8 @@ def _run(self, input: Input) -> Output:
542542

543543

544544
def spawn_children(
545-
state: State, coroutine: Coroutine, awaitables: tuple[Awaitable[Any], ...]
546-
) -> list[Coroutine]:
545+
state: State, coroutine: Coroutine, awaitables: Tuple[Awaitable[Any], ...]
546+
) -> List[Coroutine]:
547547
children = []
548548
for awaitable in awaitables:
549549
g = awaitable.__await__()

src/dispatch/signature/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from datetime import datetime, timedelta
3-
from typing import Sequence, cast
3+
from typing import Sequence, cast, Set
44

55
import http_sfv
66
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
@@ -114,8 +114,8 @@ def verify_request(request: Request, key: Ed25519PublicKey, max_age: timedelta):
114114
verify_content_digest(request.headers["Content-Digest"], request.body)
115115

116116

117-
def extract_covered_components(result: VerifyResult) -> set[str]:
118-
covered_components: set[str] = set()
117+
def extract_covered_components(result: VerifyResult) -> Set[str]:
118+
covered_components: Set[str] = set()
119119
for key in result.covered_components.keys():
120120
item = http_sfv.Item()
121121
item.parse(key.encode())

src/dispatch/status.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import enum
2-
from typing import Any, Callable, Type
2+
from typing import Any, Callable, Type, Dict
33

44
from dispatch.error import IncompatibleStateError
55
from dispatch.sdk.v1 import status_pb2 as status_pb
@@ -79,8 +79,8 @@ def __str__(self):
7979
Status.NOT_FOUND.__doc__ = "An operation was performed on a non-existent resource"
8080
Status.NOT_FOUND._proto = status_pb.STATUS_NOT_FOUND
8181

82-
_ERROR_TYPES: dict[Type[Exception], Callable[[Exception], Status]] = {}
83-
_OUTPUT_TYPES: dict[Type[Any], Callable[[Any], Status]] = {}
82+
_ERROR_TYPES: Dict[Type[Exception], Callable[[Exception], Status]] = {}
83+
_OUTPUT_TYPES: Dict[Type[Any], Callable[[Any], Status]] = {}
8484

8585

8686
def status_for_error(error: Exception) -> Status:

src/dispatch/test/service.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from collections import OrderedDict
77
from dataclasses import dataclass
8-
from typing import Optional
8+
from typing import Optional, Dict, Tuple, List, Set
99

1010
import grpc
1111
import httpx
@@ -34,7 +34,7 @@
3434
logger = logging.getLogger(__name__)
3535

3636

37-
RoundTrip: TypeAlias = tuple[function_pb.RunRequest, function_pb.RunResponse]
37+
RoundTrip: TypeAlias = Tuple[function_pb.RunRequest, function_pb.RunResponse]
3838
"""A request to a Dispatch endpoint, and the response that was received."""
3939

4040

@@ -54,7 +54,7 @@ def __init__(
5454
self,
5555
endpoint_client: EndpointClient,
5656
api_key: Optional[str] = None,
57-
retry_on_status: Optional[set[Status]] = None,
57+
retry_on_status: Optional[Set[Status]] = None,
5858
collect_roundtrips: bool = False,
5959
):
6060
"""Initialize the Dispatch service.
@@ -82,12 +82,12 @@ def __init__(
8282

8383
self._next_dispatch_id = 1
8484

85-
self.queue: list[tuple[DispatchID, function_pb.RunRequest, CallType]] = []
85+
self.queue: List[Tuple[DispatchID, function_pb.RunRequest, CallType]] = []
8686

87-
self.pollers: dict[DispatchID, Poller] = {}
88-
self.parents: dict[DispatchID, Poller] = {}
87+
self.pollers: Dict[DispatchID, Poller] = {}
88+
self.parents: Dict[DispatchID, Poller] = {}
8989

90-
self.roundtrips: Optional[OrderedDict[DispatchID, list[RoundTrip]]] = None
90+
self.roundtrips: Optional[OrderedDict[DispatchID, List[RoundTrip]]] = None
9191
if collect_roundtrips:
9292
self.roundtrips = OrderedDict()
9393

@@ -354,5 +354,5 @@ class Poller:
354354
coroutine_state: bytes
355355
# TODO: support max_wait/min_results/max_results
356356

357-
waiting: dict[DispatchID, call_pb.Call]
358-
results: dict[DispatchID, call_pb.CallResult]
357+
waiting: Dict[DispatchID, call_pb.Call]
358+
results: Dict[DispatchID, call_pb.CallResult]

0 commit comments

Comments
 (0)