diff --git a/nitric/api/secrets.py b/nitric/api/secrets.py index 5840627..90d97a2 100644 --- a/nitric/api/secrets.py +++ b/nitric/api/secrets.py @@ -41,9 +41,9 @@ 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): @@ -51,7 +51,7 @@ def __del__(self): 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) @@ -86,7 +86,7 @@ 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. @@ -94,7 +94,7 @@ def version(self, version: str): """ return SecretVersion(_secrets=self._secrets, secret=self, id=version) - def latest(self): + def latest(self) -> SecretVersion: """ Return a reference to the 'latest' secret version. @@ -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) diff --git a/nitric/resources/buckets.py b/nitric/resources/buckets.py index 5b07832..72ca15e 100644 --- a/nitric/resources/buckets.py +++ b/nitric/resources/buckets.py @@ -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()) @@ -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.""" diff --git a/nitric/resources/collections.py b/nitric/resources/collections.py index bbc04b0..029bc22 100644 --- a/nitric/resources/collections.py +++ b/nitric/resources/collections.py @@ -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()) @@ -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]] = { diff --git a/nitric/resources/queues.py b/nitric/resources/queues.py index b22993d..c31e143 100644 --- a/nitric/resources/queues.py +++ b/nitric/resources/queues.py @@ -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 @@ -34,6 +34,7 @@ QueuePermission = Literal["sending", "receiving"] + class Queue(SecureResource): """A queue resource.""" @@ -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]] = { @@ -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()) diff --git a/nitric/resources/resource.py b/nitric/resources/resource.py index 280f755..474b247 100644 --- a/nitric/resources/resource.py +++ b/nitric/resources/resource.py @@ -26,7 +26,6 @@ from grpclib import GRPCError from nitric.proto.nitric.resource.v1 import ( - Action, PolicyResource, Resource as WireResource, ResourceType, @@ -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 @@ -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}) @@ -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)) diff --git a/nitric/resources/schedules.py b/nitric/resources/schedules.py index 5d0a5d0..7dfc556 100644 --- a/nitric/resources/schedules.py +++ b/nitric/resources/schedules.py @@ -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: @@ -27,7 +30,7 @@ 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() @@ -35,7 +38,7 @@ 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. @@ -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 diff --git a/nitric/resources/topics.py b/nitric/resources/topics.py index 9c4cab0..a0f7441 100644 --- a/nitric/resources/topics.py +++ b/nitric/resources/topics.py @@ -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 @@ -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()) @@ -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]]