Skip to content

Commit

Permalink
fix: add missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
jyecusch committed Jun 26, 2023
1 parent d505d0e commit 6472631
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions nitric/api/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ class Secrets(object):
This client insulates application code from stack specific secrets managements services.
"""

def __init__(self):
def __init__(self: Secrets):
"""Construct a Nitric Storage Client."""
self._channel: Union[Channel, None] = new_default_channel()
self._channel: Channel = new_default_channel()
self.secrets_stub = SecretServiceStub(channel=self._channel)

def __del__(self):
# close the channel when this client is destroyed
if self._channel is not None:
self._channel.close()

def secret(self, name: str):
def secret(self, name: str) -> SecretContainerRef:
"""Return a reference to a secret container from the connected secrets management service."""
return SecretContainerRef(_secrets=self, name=name)

Expand Down Expand Up @@ -86,15 +86,15 @@ async def put(self, value: Union[str, bytes]) -> SecretVersion:
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def version(self, version: str):
def version(self, version: str) -> SecretVersion:
"""
Return a reference to a specific version of a secret.
Can be used to retrieve the secret value associated with the version.
"""
return SecretVersion(_secrets=self._secrets, secret=self, id=version)

def latest(self):
def latest(self) -> SecretVersion:
"""
Return a reference to the 'latest' secret version.
Expand Down Expand Up @@ -152,10 +152,10 @@ def __str__(self) -> str:
def __bytes__(self) -> bytes:
return self.value

def as_string(self):
def as_string(self) -> str:
"""Return the content of this secret value as a string."""
return str(self)

def as_bytes(self):
def as_bytes(self) -> bytes:
"""Return the content of this secret value."""
return bytes(self)
4 changes: 2 additions & 2 deletions nitric/resources/buckets.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, name: str):
super().__init__()
self.name = name

async def _register(self):
async def _register(self) -> None:
try:
await self._resources_stub.declare(
resource_declare_request=ResourceDeclareRequest(resource=self._to_resource())
Expand All @@ -67,7 +67,7 @@ def _perms_to_actions(self, *args: BucketPermission) -> List[int]:
return [action for perm in args for action in permission_actions_map[perm]]

def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Bucket) # type:ignore
return Resource(name=self.name, type=ResourceType.Bucket) # type:ignore

def allow(self, *args: BucketPermission) -> BucketRef:
"""Request the required permissions for this resource."""
Expand Down
4 changes: 2 additions & 2 deletions nitric/resources/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, name: str):
super().__init__()
self.name = name

async def _register(self):
async def _register(self) -> None:
try:
await self._resources_stub.declare(
resource_declare_request=ResourceDeclareRequest(resource=self._to_resource())
Expand All @@ -52,7 +52,7 @@ async def _register(self):
raise exception_from_grpc_error(grpc_err)

def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Collection) # type:ignore
return Resource(name=self.name, type=ResourceType.Collection) # type:ignore

def _perms_to_actions(self, *args: CollectionPermission) -> List[int]:
permission_actions_map: dict[CollectionPermission, List[int]] = {
Expand Down
7 changes: 4 additions & 3 deletions nitric/resources/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from __future__ import annotations

from nitric.exception import exception_from_grpc_error
from typing import List, Union, Literal
from typing import List, Literal
from grpclib import GRPCError
from nitric.api.queues import QueueRef, Queues
from nitric.application import Nitric
Expand All @@ -34,6 +34,7 @@

QueuePermission = Literal["sending", "receiving"]


class Queue(SecureResource):
"""A queue resource."""

Expand All @@ -46,7 +47,7 @@ def __init__(self, name: str):
self.name = name

def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Queue) # type:ignore
return Resource(name=self.name, type=ResourceType.Queue) # type:ignore

def _perms_to_actions(self, *args: QueuePermission) -> List[int]:
permission_actions_map: dict[QueuePermission, List[int]] = {
Expand All @@ -56,7 +57,7 @@ def _perms_to_actions(self, *args: QueuePermission) -> List[int]:

return [action for perm in args for action in permission_actions_map[perm]]

async def _register(self):
async def _register(self) -> None:
try:
await self._resources_stub.declare(
resource_declare_request=ResourceDeclareRequest(resource=self._to_resource())
Expand Down
7 changes: 3 additions & 4 deletions nitric/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

from grpclib import GRPCError
from nitric.proto.nitric.resource.v1 import (
Action,
PolicyResource,
Resource as WireResource,
ResourceType,
Expand All @@ -50,7 +49,7 @@ def __init__(self):
self._resources_stub = ResourceServiceStub(channel=self._channel)

@abstractmethod
async def _register(self):
async def _register(self) -> None:
pass

@classmethod
Expand Down Expand Up @@ -82,7 +81,7 @@ def _to_resource(self) -> WireResource:
def _perms_to_actions(self, *args: Any) -> List[int]:
pass

async def _register_policy_async(self, *args: str):
async def _register_policy_async(self, *args: str) -> None:
# if self._reg is not None:
# await asyncio.wait({self._reg})

Expand All @@ -100,7 +99,7 @@ async def _register_policy_async(self, *args: str):
except GRPCError as grpc_err:
raise exception_from_grpc_error(grpc_err)

def _register_policy(self, *args: str):
def _register_policy(self, *args: str) -> None:
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(self._register_policy_async(*args))
Expand Down
16 changes: 9 additions & 7 deletions nitric/resources/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
# limitations under the License.
#
from __future__ import annotations

from typing import Coroutine, Callable

from nitric.application import Nitric
from nitric.faas import FunctionServer, Middleware, RateWorkerOptions, Frequency, EventContext
from nitric.faas import FunctionServer, RateWorkerOptions, Frequency, EventHandler


class Schedule:
Expand All @@ -27,15 +30,15 @@ class Schedule:
description: str
server: FunctionServer

def start(self):
def start(self) -> Coroutine:
"""Start the function server that executes the scheduled middleware."""
return self.server.start()

def __init__(self, description: str):
"""Construct a new schedule."""
self.description = description

def every(self, rate_description: str, *middleware: Middleware[EventContext]):
def every(self, rate_description: str, handler: EventHandler) -> None:
"""
Register middleware to be run at the specified rate.
Expand All @@ -59,17 +62,16 @@ def every(self, rate_description: str, *middleware: Middleware[EventContext]):
opts = RateWorkerOptions(self.description, int(rate), freq)

self.server = FunctionServer(opts)
self.server.event(*middleware)
self.server.event(handler)
# type ignored because the register call is treated as protected.
return Nitric._register_worker(self.server) # type: ignore


def schedule(description: str, every: str):
def schedule(description: str, every: str) -> Callable[[EventHandler], None]:
"""Return a schedule decorator."""

def decorator(func: Middleware[EventContext]):
def decorator(func: EventHandler) -> None:
r = Schedule(description)
r.every(every, func)
return r

return decorator
10 changes: 4 additions & 6 deletions nitric/resources/topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from nitric.api.events import Events, TopicRef
from nitric.exception import exception_from_grpc_error
from typing import List, Union, Callable, Literal
from typing import List, Callable, Literal
from grpclib import GRPCError
from nitric.application import Nitric
from nitric.faas import FunctionServer, SubscriptionWorkerOptions, EventHandler
Expand All @@ -47,7 +47,7 @@ def __init__(self, name: str):
super().__init__()
self.name = name

async def _register(self):
async def _register(self) -> None:
try:
await self._resources_stub.declare(
resource_declare_request=ResourceDeclareRequest(resource=self._to_resource())
Expand All @@ -56,12 +56,10 @@ async def _register(self):
raise exception_from_grpc_error(grpc_err)

def _to_resource(self) -> Resource:
return Resource(name=self.name, type=ResourceType.Topic) # type:ignore
return Resource(name=self.name, type=ResourceType.Topic) # type:ignore

def _perms_to_actions(self, *args: TopicPermission) -> List[int]:
_permMap: dict[TopicPermission, List[int]] = {
"publishing": [Action.TopicEventPublish]
}
_permMap: dict[TopicPermission, List[int]] = {"publishing": [Action.TopicEventPublish]}

return [action for perm in args for action in _permMap[perm]]

Expand Down

0 comments on commit 6472631

Please sign in to comment.