1
+ from __future__ import annotations
2
+
1
3
import io
2
4
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
15
6
16
7
import boto3
17
8
18
9
from aws_lambda_powertools .shared import user_agent
19
10
from aws_lambda_powertools .utilities .streaming .compat import PowertoolsStreamingBody
11
+ from aws_lambda_powertools .utilities .streaming .constants import MESSAGE_STREAM_NOT_WRITABLE
20
12
21
13
if TYPE_CHECKING :
22
14
from mmap import mmap
@@ -51,8 +43,8 @@ def __init__(
51
43
self ,
52
44
bucket : str ,
53
45
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 ,
56
48
** sdk_options ,
57
49
):
58
50
self .bucket = bucket
@@ -65,10 +57,10 @@ def __init__(
65
57
self ._closed : bool = False
66
58
67
59
# Caches the size of the object
68
- self ._size : Optional [ int ] = None
60
+ self ._size : int | None = None
69
61
70
62
self ._s3_client = boto3_client
71
- self ._raw_stream : Optional [ PowertoolsStreamingBody ] = None
63
+ self ._raw_stream : PowertoolsStreamingBody | None = None
72
64
73
65
self ._sdk_options = sdk_options
74
66
self ._sdk_options ["Bucket" ] = bucket
@@ -78,7 +70,7 @@ def __init__(
78
70
self ._sdk_options ["VersionId" ] = version_id
79
71
80
72
@property
81
- def s3_client (self ) -> " S3Client" :
73
+ def s3_client (self ) -> S3Client :
82
74
"""
83
75
Returns a boto3 S3 client
84
76
"""
@@ -102,7 +94,7 @@ def size(self) -> int:
102
94
@property
103
95
def raw_stream (self ) -> PowertoolsStreamingBody :
104
96
"""
105
- Returns the boto3 StreamingBody, starting the stream from the seeked position.
97
+ Returns the boto3 StreamingBody, starting the stream from the sought position.
106
98
"""
107
99
if self ._raw_stream is None :
108
100
range_header = f"bytes={ self ._position } -"
@@ -152,19 +144,19 @@ def writable(self) -> bool:
152
144
def tell (self ) -> int :
153
145
return self ._position
154
146
155
- def read (self , size : Optional [ int ] = - 1 ) -> bytes :
147
+ def read (self , size : int | None = - 1 ) -> bytes :
156
148
size = None if size == - 1 else size
157
149
data = self .raw_stream .read (size )
158
150
if data is not None :
159
151
self ._position += len (data )
160
152
return data
161
153
162
- def readline (self , size : Optional [ int ] = None ) -> bytes :
154
+ def readline (self , size : int | None = None ) -> bytes :
163
155
data = self .raw_stream .readline (size )
164
156
self ._position += len (data )
165
157
return data
166
158
167
- def readlines (self , hint : int = - 1 ) -> List [bytes ]:
159
+ def readlines (self , hint : int = - 1 ) -> list [bytes ]:
168
160
# boto3's StreamingResponse doesn't implement the "hint" parameter
169
161
data = self .raw_stream .readlines ()
170
162
self ._position += sum (len (line ) for line in data )
@@ -194,19 +186,19 @@ def fileno(self) -> int:
194
186
raise NotImplementedError ("this stream is not backed by a file descriptor" )
195
187
196
188
def flush (self ) -> None :
197
- raise NotImplementedError ("this stream is not writable" )
189
+ raise NotImplementedError (MESSAGE_STREAM_NOT_WRITABLE )
198
190
199
191
def isatty (self ) -> bool :
200
192
return False
201
193
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 )
204
196
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 )
207
199
208
200
def writelines (
209
201
self ,
210
- data : Iterable [Union [ bytes , Union [ bytearray , memoryview , Sequence [Any ], " mmap" , " _CData" ]] ],
202
+ data : Iterable [bytes | bytearray | memoryview | Sequence [Any ] | mmap | _CData ],
211
203
) -> None :
212
- raise NotImplementedError ("this stream is not writable" )
204
+ raise NotImplementedError (MESSAGE_STREAM_NOT_WRITABLE )
0 commit comments