Skip to content

Commit ebdc3ad

Browse files
refactor(streaming): add from __future__ import annotations (#4987)
* refactor(streaming): add from __future__ import annotations and update code according to ruff rules TCH, UP006, UP007, UP037 and FA100. * Fixing constants * Organize types --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 456bf82 commit ebdc3ad

File tree

5 files changed

+48
-65
lines changed

5 files changed

+48
-65
lines changed

aws_lambda_powertools/utilities/streaming/_s3_seekable_io.py

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1+
from __future__ import annotations
2+
13
import io
24
import logging
3-
from typing import (
4-
IO,
5-
TYPE_CHECKING,
6-
Any,
7-
Iterable,
8-
List,
9-
Optional,
10-
Sequence,
11-
TypeVar,
12-
Union,
13-
cast,
14-
)
5+
from typing import IO, TYPE_CHECKING, Any, Iterable, Sequence, TypeVar, cast
156

167
import boto3
178

189
from aws_lambda_powertools.shared import user_agent
1910
from aws_lambda_powertools.utilities.streaming.compat import PowertoolsStreamingBody
11+
from aws_lambda_powertools.utilities.streaming.constants import MESSAGE_STREAM_NOT_WRITABLE
2012

2113
if TYPE_CHECKING:
2214
from mmap import mmap
@@ -51,8 +43,8 @@ def __init__(
5143
self,
5244
bucket: str,
5345
key: str,
54-
version_id: Optional[str] = None,
55-
boto3_client: Optional["S3Client"] = None,
46+
version_id: str | None = None,
47+
boto3_client: S3Client | None = None,
5648
**sdk_options,
5749
):
5850
self.bucket = bucket
@@ -65,10 +57,10 @@ def __init__(
6557
self._closed: bool = False
6658

6759
# Caches the size of the object
68-
self._size: Optional[int] = None
60+
self._size: int | None = None
6961

7062
self._s3_client = boto3_client
71-
self._raw_stream: Optional[PowertoolsStreamingBody] = None
63+
self._raw_stream: PowertoolsStreamingBody | None = None
7264

7365
self._sdk_options = sdk_options
7466
self._sdk_options["Bucket"] = bucket
@@ -78,7 +70,7 @@ def __init__(
7870
self._sdk_options["VersionId"] = version_id
7971

8072
@property
81-
def s3_client(self) -> "S3Client":
73+
def s3_client(self) -> S3Client:
8274
"""
8375
Returns a boto3 S3 client
8476
"""
@@ -102,7 +94,7 @@ def size(self) -> int:
10294
@property
10395
def raw_stream(self) -> PowertoolsStreamingBody:
10496
"""
105-
Returns the boto3 StreamingBody, starting the stream from the seeked position.
97+
Returns the boto3 StreamingBody, starting the stream from the sought position.
10698
"""
10799
if self._raw_stream is None:
108100
range_header = f"bytes={self._position}-"
@@ -152,19 +144,19 @@ def writable(self) -> bool:
152144
def tell(self) -> int:
153145
return self._position
154146

155-
def read(self, size: Optional[int] = -1) -> bytes:
147+
def read(self, size: int | None = -1) -> bytes:
156148
size = None if size == -1 else size
157149
data = self.raw_stream.read(size)
158150
if data is not None:
159151
self._position += len(data)
160152
return data
161153

162-
def readline(self, size: Optional[int] = None) -> bytes:
154+
def readline(self, size: int | None = None) -> bytes:
163155
data = self.raw_stream.readline(size)
164156
self._position += len(data)
165157
return data
166158

167-
def readlines(self, hint: int = -1) -> List[bytes]:
159+
def readlines(self, hint: int = -1) -> list[bytes]:
168160
# boto3's StreamingResponse doesn't implement the "hint" parameter
169161
data = self.raw_stream.readlines()
170162
self._position += sum(len(line) for line in data)
@@ -194,19 +186,19 @@ def fileno(self) -> int:
194186
raise NotImplementedError("this stream is not backed by a file descriptor")
195187

196188
def flush(self) -> None:
197-
raise NotImplementedError("this stream is not writable")
189+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
198190

199191
def isatty(self) -> bool:
200192
return False
201193

202-
def truncate(self, size: Optional[int] = 0) -> int:
203-
raise NotImplementedError("this stream is not writable")
194+
def truncate(self, size: int | None = 0) -> int:
195+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
204196

205-
def write(self, data: Union[bytes, Union[bytearray, memoryview, Sequence[Any], "mmap", "_CData"]]) -> int:
206-
raise NotImplementedError("this stream is not writable")
197+
def write(self, data: bytes | bytearray | memoryview | Sequence[Any] | mmap | _CData) -> int:
198+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
207199

208200
def writelines(
209201
self,
210-
data: Iterable[Union[bytes, Union[bytearray, memoryview, Sequence[Any], "mmap", "_CData"]]],
202+
data: Iterable[bytes | bytearray | memoryview | Sequence[Any] | mmap | _CData],
211203
) -> None:
212-
raise NotImplementedError("this stream is not writable")
204+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MESSAGE_STREAM_NOT_WRITABLE = "this stream is not writable"

aws_lambda_powertools/utilities/streaming/s3_object.py

+22-35
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
11
from __future__ import annotations
22

33
import io
4-
from typing import (
5-
IO,
6-
TYPE_CHECKING,
7-
Any,
8-
Iterable,
9-
List,
10-
Literal,
11-
Optional,
12-
Sequence,
13-
TypeVar,
14-
Union,
15-
cast,
16-
overload,
17-
)
4+
from typing import IO, TYPE_CHECKING, Any, Iterable, Literal, Sequence, TypeVar, cast, overload
185

196
from aws_lambda_powertools.utilities.streaming._s3_seekable_io import _S3SeekableIO
7+
from aws_lambda_powertools.utilities.streaming.constants import MESSAGE_STREAM_NOT_WRITABLE
208
from aws_lambda_powertools.utilities.streaming.transformations import (
219
CsvTransform,
2210
GzipTransform,
2311
)
24-
from aws_lambda_powertools.utilities.streaming.transformations.base import (
25-
BaseTransform,
26-
T,
27-
)
12+
from aws_lambda_powertools.utilities.streaming.types import T
2813

2914
if TYPE_CHECKING:
3015
from mmap import mmap
3116

3217
from mypy_boto3_s3.client import S3Client
3318

19+
from aws_lambda_powertools.utilities.streaming.transformations.base import BaseTransform
20+
3421
_CData = TypeVar("_CData")
3522

3623

@@ -74,10 +61,10 @@ def __init__(
7461
self,
7562
bucket: str,
7663
key: str,
77-
version_id: Optional[str] = None,
78-
boto3_client: Optional[S3Client] = None,
79-
is_gzip: Optional[bool] = False,
80-
is_csv: Optional[bool] = False,
64+
version_id: str | None = None,
65+
boto3_client: S3Client | None = None,
66+
is_gzip: bool | None = False,
67+
is_csv: bool | None = False,
8168
**sdk_options,
8269
):
8370
self.bucket = bucket
@@ -94,14 +81,14 @@ def __init__(
9481
)
9582

9683
# Stores the list of data transformations
97-
self._data_transformations: List[BaseTransform] = []
84+
self._data_transformations: list[BaseTransform] = []
9885
if is_gzip:
9986
self._data_transformations.append(GzipTransform())
10087
if is_csv:
10188
self._data_transformations.append(CsvTransform())
10289

10390
# Stores the cached transformed stream
104-
self._transformed_stream: Optional[IO[bytes]] = None
91+
self._transformed_stream: IO[bytes] | None = None
10592

10693
@property
10794
def size(self) -> int:
@@ -152,8 +139,8 @@ def transform(self, transformations: BaseTransform[T] | Sequence[BaseTransform[T
152139
def transform(
153140
self,
154141
transformations: BaseTransform[T] | Sequence[BaseTransform[T]],
155-
in_place: Optional[bool] = False,
156-
) -> Optional[T]:
142+
in_place: bool | None = False,
143+
) -> T | None:
157144
"""
158145
Applies one or more data transformations to the stream.
159146
@@ -241,10 +228,10 @@ def close(self):
241228
def read(self, size: int = -1) -> bytes:
242229
return self.transformed_stream.read(size)
243230

244-
def readline(self, size: Optional[int] = -1) -> bytes:
231+
def readline(self, size: int | None = -1) -> bytes:
245232
return self.transformed_stream.readline()
246233

247-
def readlines(self, hint: int = -1) -> List[bytes]:
234+
def readlines(self, hint: int = -1) -> list[bytes]:
248235
return self.transformed_stream.readlines(hint)
249236

250237
def __next__(self):
@@ -257,19 +244,19 @@ def fileno(self) -> int:
257244
raise NotImplementedError("this stream is not backed by a file descriptor")
258245

259246
def flush(self) -> None:
260-
raise NotImplementedError("this stream is not writable")
247+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
261248

262249
def isatty(self) -> bool:
263250
return False
264251

265-
def truncate(self, size: Optional[int] = 0) -> int:
266-
raise NotImplementedError("this stream is not writable")
252+
def truncate(self, size: int | None = 0) -> int:
253+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
267254

268-
def write(self, data: Union[bytes, Union[bytearray, memoryview, Sequence[Any], "mmap", "_CData"]]) -> int:
269-
raise NotImplementedError("this stream is not writable")
255+
def write(self, data: bytes | bytearray | memoryview | Sequence[Any] | mmap | _CData) -> int:
256+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)
270257

271258
def writelines(
272259
self,
273-
data: Iterable[Union[bytes, Union[bytearray, memoryview, Sequence[Any], "mmap", "_CData"]]],
260+
data: Iterable[bytes | bytearray | memoryview | Sequence[Any] | mmap | _CData],
274261
) -> None:
275-
raise NotImplementedError("this stream is not writable")
262+
raise NotImplementedError(MESSAGE_STREAM_NOT_WRITABLE)

aws_lambda_powertools/utilities/streaming/transformations/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import abstractmethod
2-
from typing import IO, Generic, TypeVar
2+
from typing import IO, Generic
33

4-
T = TypeVar("T", bound=IO[bytes])
4+
from aws_lambda_powertools.utilities.streaming.types import T
55

66

77
class BaseTransform(Generic[T]):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import IO, TypeVar
2+
3+
T = TypeVar("T", bound=IO[bytes])

0 commit comments

Comments
 (0)