Skip to content

Commit ab67989

Browse files
authored
chore: update decorator static types (#111)
2 parents 74d43ca + 6c9832b commit ab67989

4 files changed

Lines changed: 35 additions & 31 deletions

File tree

nitric/py.typed

Whitespace-only changes.

nitric/resources/apis.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _route(self, match: str, opts: Optional[RouteOptions] = None) -> Route:
181181
self.routes.append(r)
182182
return r
183183

184-
def all(self, match: str, opts: Optional[MethodOptions] = None):
184+
def all(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
185185
"""Define an HTTP route which will respond to HTTP GET requests."""
186186

187187
def decorator(function: HttpHandler) -> None:
@@ -201,78 +201,80 @@ def decorator(function: HttpHandler) -> None:
201201

202202
return decorator
203203

204-
def methods(self, methods: List[HttpMethod], match: str, opts: Optional[MethodOptions] = None):
204+
def methods(
205+
self, methods: List[HttpMethod], match: str, opts: Optional[MethodOptions] = None
206+
) -> Callable[[HttpHandler], None]:
205207
"""Define an HTTP route which will respond to specific HTTP requests defined by a list of verbs."""
206208
if opts is None:
207209
opts = MethodOptions()
208210

209-
def decorator(function: HttpHandler):
211+
def decorator(function: HttpHandler) -> None:
210212
r = self._route(match)
211213
r.method(methods, function, opts=opts)
212214

213215
return decorator
214216

215-
def get(self, match: str, opts: Optional[MethodOptions] = None):
217+
def get(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
216218
"""Define an HTTP route which will respond to HTTP GET requests."""
217219
if opts is None:
218220
opts = MethodOptions()
219221

220-
def decorator(function: HttpHandler):
222+
def decorator(function: HttpHandler) -> None:
221223
r = self._route(match)
222224
r.get(function, opts=opts)
223225

224226
return decorator
225227

226-
def post(self, match: str, opts: Optional[MethodOptions] = None):
228+
def post(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
227229
"""Define an HTTP route which will respond to HTTP POST requests."""
228230
if opts is None:
229231
opts = MethodOptions()
230232

231-
def decorator(function: HttpHandler):
233+
def decorator(function: HttpHandler) -> None:
232234
r = self._route(match)
233235
r.post(function, opts=opts)
234236

235237
return decorator
236238

237-
def delete(self, match: str, opts: Optional[MethodOptions] = None):
239+
def delete(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
238240
"""Define an HTTP route which will respond to HTTP DELETE requests."""
239241
if opts is None:
240242
opts = MethodOptions()
241243

242-
def decorator(function: HttpHandler):
244+
def decorator(function: HttpHandler) -> None:
243245
r = self._route(match)
244246
r.delete(function, opts=opts)
245247

246248
return decorator
247249

248-
def options(self, match: str, opts: Optional[MethodOptions] = None):
250+
def options(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
249251
"""Define an HTTP route which will respond to HTTP OPTIONS requests."""
250252
if opts is None:
251253
opts = MethodOptions()
252254

253-
def decorator(function: HttpHandler):
255+
def decorator(function: HttpHandler) -> None:
254256
r = self._route(match)
255257
r.options(function, opts=opts)
256258

257259
return decorator
258260

259-
def patch(self, match: str, opts: Optional[MethodOptions] = None):
261+
def patch(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
260262
"""Define an HTTP route which will respond to HTTP PATCH requests."""
261263
if opts is None:
262264
opts = MethodOptions()
263265

264-
def decorator(function: HttpHandler):
266+
def decorator(function: HttpHandler) -> None:
265267
r = self._route(match)
266268
r.patch(function, opts=opts)
267269

268270
return decorator
269271

270-
def put(self, match: str, opts: Optional[MethodOptions] = None):
272+
def put(self, match: str, opts: Optional[MethodOptions] = None) -> Callable[[HttpHandler], None]:
271273
"""Define an HTTP route which will respond to HTTP PUT requests."""
272274
if opts is None:
273275
opts = MethodOptions()
274276

275-
def decorator(function: HttpHandler):
277+
def decorator(function: HttpHandler) -> None:
276278
r = self._route(match)
277279
r.put(function, opts=opts)
278280

@@ -311,31 +313,31 @@ def __init__(self, api: Api, path: str, opts: RouteOptions):
311313

312314
def method(
313315
self, methods: List[HttpMethod], *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None
314-
):
316+
) -> None:
315317
"""Register middleware for multiple HTTP Methods."""
316318
return Method(self, methods, *middleware, opts=opts).start()
317319

318-
def get(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
320+
def get(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
319321
"""Register middleware for HTTP GET requests."""
320322
return self.method([HttpMethod.GET], *middleware, opts=opts)
321323

322-
def post(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
324+
def post(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
323325
"""Register middleware for HTTP POST requests."""
324326
return self.method([HttpMethod.POST], *middleware, opts=opts)
325327

326-
def put(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
328+
def put(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
327329
"""Register middleware for HTTP PUT requests."""
328330
return self.method([HttpMethod.PUT], *middleware, opts=opts)
329331

330-
def patch(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
332+
def patch(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
331333
"""Register middleware for HTTP PATCH requests."""
332334
return self.method([HttpMethod.PATCH], *middleware, opts=opts)
333335

334-
def delete(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
336+
def delete(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
335337
"""Register middleware for HTTP DELETE requests."""
336338
return self.method([HttpMethod.DELETE], *middleware, opts=opts)
337339

338-
def options(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None):
340+
def options(self, *middleware: HttpMiddleware | HttpHandler, opts: Optional[MethodOptions] = None) -> None:
339341
"""Register middleware for HTTP OPTIONS requests."""
340342
return self.method([HttpMethod.OPTIONS], *middleware, opts=opts)
341343

@@ -361,7 +363,7 @@ def __init__(
361363
self.server = FunctionServer(ApiWorkerOptions(route.api.name, route.path, methods, opts))
362364
self.server.http(*route.api.middleware, *route.middleware, *middleware)
363365

364-
def start(self):
366+
def start(self) -> None:
365367
"""Start the server which will respond to incoming requests."""
366368
Nitric._register_worker(self.server) # type: ignore
367369

nitric/resources/buckets.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
from nitric.exception import exception_from_grpc_error
2222
from nitric.api.storage import BucketRef, Storage
23-
from typing import List, Union
23+
from typing import List, Union, Callable
2424
from enum import Enum
2525
from grpclib import GRPCError
2626

2727
from nitric.application import Nitric
28-
from nitric.faas import FunctionServer, BucketNotificationWorkerOptions, Middleware, BucketNotificationContext
28+
from nitric.faas import FunctionServer, BucketNotificationWorkerOptions, BucketNotificationHandler
2929
from nitric.proto.nitric.resource.v1 import (
3030
Resource,
3131
ResourceType,
@@ -87,11 +87,13 @@ def allow(self, *args: Union[BucketPermission, str]) -> BucketRef:
8787

8888
return Storage().bucket(self.name)
8989

90-
def on(self, notification_type: str, notification_prefix_filter: str):
90+
def on(
91+
self, notification_type: str, notification_prefix_filter: str
92+
) -> Callable[[BucketNotificationHandler], None]:
9193
"""Create and return a bucket notification decorator for this bucket."""
9294
print("this has been called")
9395

94-
def decorator(func: Middleware[BucketNotificationContext]):
96+
def decorator(func: BucketNotificationHandler) -> None:
9597
print("this has been called")
9698
self._server = FunctionServer(
9799
BucketNotificationWorkerOptions(

nitric/resources/topics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
from nitric.api.events import Events, TopicRef
2222
from nitric.exception import exception_from_grpc_error
23-
from typing import List, Union
23+
from typing import List, Union, Callable
2424
from enum import Enum
2525
from grpclib import GRPCError
2626
from nitric.application import Nitric
27-
from nitric.faas import Middleware, FunctionServer, SubscriptionWorkerOptions, EventContext
27+
from nitric.faas import FunctionServer, SubscriptionWorkerOptions, EventHandler
2828
from nitric.proto.nitric.resource.v1 import (
2929
Resource,
3030
ResourceType,
@@ -80,10 +80,10 @@ def allow(self, *args: Union[TopicPermission, str]) -> TopicRef:
8080

8181
return Events().topic(self.name)
8282

83-
def subscribe(self):
83+
def subscribe(self) -> Callable[[EventHandler], None]:
8484
"""Create and return a subscription decorator for this topic."""
8585

86-
def decorator(func: Middleware[EventContext]):
86+
def decorator(func: EventHandler) -> None:
8787
server = FunctionServer(SubscriptionWorkerOptions(topic=self.name))
8888
server.event(func)
8989
# type ignored because the register call is treated as protected.

0 commit comments

Comments
 (0)