generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 7
refactor: adding type annotations from mypy stubs #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,13 @@ | |
| import copy | ||
| import datetime | ||
| import logging | ||
| from collections.abc import MutableMapping | ||
| from dataclasses import dataclass, field | ||
| from enum import Enum | ||
| from typing import TYPE_CHECKING, Any, Protocol, TypeAlias | ||
| from typing import TYPE_CHECKING, Any, Protocol, TypeAlias, cast | ||
|
|
||
| import boto3 # type: ignore | ||
| from botocore.config import Config # type: ignore | ||
| import boto3 | ||
| from botocore.config import Config | ||
|
|
||
| from aws_durable_execution_sdk_python.exceptions import ( | ||
| CallableRuntimeError, | ||
|
|
@@ -17,7 +18,11 @@ | |
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import MutableMapping | ||
| from mypy_boto3_lambda import LambdaClient as Boto3LambdaClient | ||
| from mypy_boto3_lambda.type_defs import ( | ||
| CheckpointDurableExecutionResponseTypeDef, | ||
| GetDurableExecutionStateResponseTypeDef, | ||
| ) | ||
|
|
||
| from aws_durable_execution_sdk_python.identifier import OperationIdentifier | ||
|
|
||
|
|
@@ -1031,9 +1036,9 @@ def get_execution_state( | |
| class LambdaClient(DurableServiceClient): | ||
| """Persist durable operations to the Lambda Durable Function APIs.""" | ||
|
|
||
| _cached_boto_client: Any = None | ||
| _cached_boto_client: Boto3LambdaClient | None = None | ||
|
|
||
| def __init__(self, client: Any) -> None: | ||
| def __init__(self, client: Boto3LambdaClient) -> None: | ||
| self.client = client | ||
|
|
||
| @classmethod | ||
|
|
@@ -1066,19 +1071,20 @@ def checkpoint( | |
| client_token: str | None, | ||
| ) -> CheckpointOutput: | ||
| try: | ||
| params = { | ||
| "DurableExecutionArn": durable_execution_arn, | ||
| "CheckpointToken": checkpoint_token, | ||
| "Updates": [o.to_dict() for o in updates], | ||
| } | ||
| optional_params: dict[str, str] = {} | ||
| if client_token is not None: | ||
| params["ClientToken"] = client_token | ||
|
|
||
| result: MutableMapping[str, Any] = self.client.checkpoint_durable_execution( | ||
| **params | ||
| optional_params["ClientToken"] = client_token | ||
|
|
||
| result: CheckpointDurableExecutionResponseTypeDef = ( | ||
| self.client.checkpoint_durable_execution( | ||
| DurableExecutionArn=durable_execution_arn, | ||
| CheckpointToken=checkpoint_token, | ||
| Updates=cast(Any, [o.to_dict() for o in updates]), | ||
| **optional_params, # type: ignore[arg-type] | ||
| ) | ||
| ) | ||
|
|
||
| return CheckpointOutput.from_dict(result) | ||
| return CheckpointOutput.from_dict(cast(MutableMapping[str, Any], result)) | ||
| except Exception as e: | ||
| checkpoint_error = CheckpointError.from_exception(e) | ||
| logger.exception( | ||
|
|
@@ -1094,13 +1100,15 @@ def get_execution_state( | |
| max_items: int = 1000, | ||
| ) -> StateOutput: | ||
| try: | ||
| result: MutableMapping[str, Any] = self.client.get_durable_execution_state( | ||
| DurableExecutionArn=durable_execution_arn, | ||
| CheckpointToken=checkpoint_token, | ||
| Marker=next_marker, | ||
| MaxItems=max_items, | ||
| result: GetDurableExecutionStateResponseTypeDef = ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar here on MutableMapping |
||
| self.client.get_durable_execution_state( | ||
| DurableExecutionArn=durable_execution_arn, | ||
| CheckpointToken=checkpoint_token, | ||
| Marker=next_marker, | ||
| MaxItems=max_items, | ||
| ) | ||
| ) | ||
| return StateOutput.from_dict(result) | ||
| return StateOutput.from_dict(cast(MutableMapping[str, Any], result)) | ||
| except Exception as e: | ||
| error = GetExecutionStateError.from_exception(e) | ||
| logger.exception( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be very tempted to keep this
MutableMapping[str, Any]instead ofCheckpointDurableExecutionResponseTypeDef- we're immediately serializing the result into a custom dataclass from a dict, and this way we avoid the extra cast..What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
cast()has virtually zero runtime cost - it just returns the value unchanged, purely a type checker hint - I'd keep the specificTypeDefs.I see some trends about Python ecosystem is moving towards more stricter typing, and
mypyisn't the only option anymore. We now have ty from Astral (the ruff folks) which is gaining traction. Using precise types now means we're better positioned as the tooling improves.Personally I'd keep it, but happy to revert.