77import random
88import re
99import time
10+ from collections .abc import AsyncGenerator , Generator , Iterable , Sequence
1011from concurrent .futures import ThreadPoolExecutor , as_completed
1112from functools import partial , reduce , wraps
1213from typing import (
1314 IO ,
1415 Any ,
15- AsyncGenerator ,
1616 Callable ,
17- Dict ,
18- Generator ,
19- Iterable ,
20- List ,
2117 Literal ,
2218 Optional ,
23- Sequence ,
24- Tuple ,
25- Type ,
2619 TypeVar ,
2720 Union ,
2821 cast ,
@@ -86,15 +79,15 @@ def s3_build_uri(bucket: str, key: str) -> str:
8679 return f"s3://{ bucket } /{ key } "
8780
8881
89- def s3_split_path (s3uri : str ) -> Tuple [str , str , str ]:
82+ def s3_split_path (s3uri : str ) -> tuple [str , str , str ]:
9083 match = s3re .match (s3uri )
9184 if not match :
9285 raise ValueError (f"Not a s3 uri: { s3uri } " )
9386 scheme , bucket , key = match .groups ()
9487 return scheme , bucket , key
9588
9689
97- _default_configs : Dict [str , Config ] = {}
90+ _default_configs : dict [str , Config ] = {}
9891
9992_pre_configure : Optional [Callable [[str , str , Optional [Config ]], Optional [Config ]]] = None
10093
@@ -322,9 +315,9 @@ def secret_binary_value(name: str, session: Optional[Session] = None, config: Op
322315
323316def ssm_parameter (
324317 name : str , decrypt = False , session : Optional [Session ] = None , config : Optional [Config ] = None
325- ) -> Union [str , List [str ]]:
318+ ) -> Union [str , list [str ]]:
326319 ssm = ssm_client (session = session , config = config )
327- value = cast (Union [str , List [str ]], ssm .get_parameter (Name = name , WithDecryption = decrypt )["Parameter" ]["Value" ])
320+ value = cast (Union [str , list [str ]], ssm .get_parameter (Name = name , WithDecryption = decrypt )["Parameter" ]["Value" ])
328321 return value
329322
330323
@@ -359,7 +352,7 @@ def s3_get_tags(
359352 session : Optional [Session ] = None ,
360353 config : Optional [Config ] = None ,
361354 client : Optional [S3Client ] = None ,
362- ) -> Dict [str , str ]:
355+ ) -> dict [str , str ]:
363356 client = client or s3_resource (session = session , config = config ).meta .client
364357 result = client .get_object_tagging (Bucket = bucket , Key = key )
365358 tags = {pair ["Key" ]: pair ["Value" ] for pair in result ["TagSet" ]}
@@ -369,7 +362,7 @@ def s3_get_tags(
369362def s3_replace_tags (
370363 bucket : str ,
371364 key : str ,
372- tags : Dict [str , str ],
365+ tags : dict [str , str ],
373366 session : Optional [Session ] = None ,
374367 config : Optional [Config ] = None ,
375368 client : Optional [S3Client ] = None ,
@@ -384,11 +377,11 @@ def s3_replace_tags(
384377def s3_update_tags (
385378 bucket : str ,
386379 key : str ,
387- tags : Dict [str , str ],
380+ tags : dict [str , str ],
388381 session : Optional [Session ] = None ,
389382 config : Optional [Config ] = None ,
390383 client : Optional [S3Client ] = None ,
391- ) -> Dict [str , str ]:
384+ ) -> dict [str , str ]:
392385 client = client or s3_resource (session = session , config = config ).meta .client
393386
394387 result = client .get_object_tagging (Bucket = bucket , Key = key )
@@ -407,7 +400,7 @@ def s3_get_all_keys(
407400 session : Optional [Session ] = None ,
408401 config : Optional [Config ] = None ,
409402 client : Optional [S3Client ] = None ,
410- ) -> List [str ]:
403+ ) -> list [str ]:
411404 client = client or s3_resource (session = session , config = config ).meta .client
412405 paginator = client .get_paginator ("list_objects_v2" )
413406 page_iterator = paginator .paginate (Bucket = bucket , Prefix = prefix )
@@ -442,7 +435,7 @@ def s3_delete_keys(
442435
443436
444437def s3_delete_versioned_keys (
445- keys : Iterable [Tuple [str , str ]], bucket : str , session : Optional [Session ] = None , config : Optional [Config ] = None
438+ keys : Iterable [tuple [str , str ]], bucket : str , session : Optional [Session ] = None , config : Optional [Config ] = None
446439):
447440 # delete specific versions, rather than deleting "objects" and adding delete_marker
448441 buck = s3_bucket (bucket , session = session , config = config )
@@ -512,7 +505,8 @@ def filter_items(items: Sequence[Union[DeleteMarkerEntryTypeDef, ObjectVersionTy
512505
513506 for i in range (0 , len (version_list ), 1000 ):
514507 response = client .delete_objects (
515- Bucket = bucket , Delete = {"Objects" : version_list [i : i + 1000 ], "Quiet" : True } # type: ignore[typeddict-item]
508+ Bucket = bucket ,
509+ Delete = {"Objects" : version_list [i : i + 1000 ], "Quiet" : True }, # type: ignore[typeddict-item]
516510 )
517511 print (response )
518512
@@ -608,7 +602,7 @@ def s3_put(bucket: Bucket, key: str, body: Union[bytes, str], encoding: str = "u
608602 return obj
609603
610604
611- def s3_list_prefixes (s3_path : str , session : Optional [Session ] = None , config : Optional [Config ] = None ) -> List [str ]:
605+ def s3_list_prefixes (s3_path : str , session : Optional [Session ] = None , config : Optional [Config ] = None ) -> list [str ]:
612606 _na , bucket , prefix = s3_split_path (s3_path )
613607
614608 if not prefix .endswith ("/" ):
@@ -619,7 +613,7 @@ def s3_list_prefixes(s3_path: str, session: Optional[Session] = None, config: Op
619613 return [o ["Prefix" ].replace (prefix , "" ).strip ("/" ) for o in result .get ("CommonPrefixes" , [])]
620614
621615
622- def s3_list_folders (bucket_name : str , bucket_prefix : str , page_size : int = 100 ) -> List [str ]:
616+ def s3_list_folders (bucket_name : str , bucket_prefix : str , page_size : int = 100 ) -> list [str ]:
623617 paginator = s3_client ().get_paginator ("list_objects" )
624618 folders = []
625619 iterator = paginator .paginate (
@@ -656,7 +650,7 @@ def s3_get_size(
656650
657651
658652def s3_upload_multipart_from_copy (
659- obj : Object , parts_keys : Sequence [str ], executor_type : Type [ThreadPoolExecutor ] = ThreadPoolExecutor , ** kwargs
653+ obj : Object , parts_keys : Sequence [str ], executor_type : type [ThreadPoolExecutor ] = ThreadPoolExecutor , ** kwargs
660654):
661655 multipart_upload = obj .initiate_multipart_upload (** kwargs )
662656
@@ -889,7 +883,7 @@ async def _async_wrapper(*args, **kwargs):
889883@dynamodb_retry_backoff ()
890884def get_items_batched (
891885 ddb_table_name : str ,
892- keys : List [ Dict [str , Any ]],
886+ keys : list [ dict [str , Any ]],
893887 client : Optional [DynamoDBClient ] = None ,
894888 session : Optional [Session ] = None ,
895889 config : Optional [Config ] = None ,
@@ -905,12 +899,12 @@ def get_items_batched(
905899
906900def ddb_get_items (
907901 ddb_table_name : str ,
908- keys : List [ Dict [str , Any ]],
902+ keys : list [ dict [str , Any ]],
909903 client : Optional [DynamoDBClient ] = None ,
910904 session : Optional [Session ] = None ,
911905 config : Optional [Config ] = None ,
912906 ** kwargs ,
913- ) -> List [ Dict [str , Any ]]:
907+ ) -> list [ dict [str , Any ]]:
914908 client = client or cast (DynamoDBClient , dynamodb (session = session , config = config ).meta .client )
915909 result = []
916910 remaining = keys
@@ -1106,7 +1100,7 @@ async def async_stream_from_s3(
11061100_RE_CANCELLATION_REASONS = re .compile (r"^.+\[(\w+[^\]]*?)\]$" )
11071101
11081102
1109- def transaction_cancellation_reasons (err : ClientError ) -> List [str ]:
1103+ def transaction_cancellation_reasons (err : ClientError ) -> list [str ]:
11101104 """
11111105 get cancellation reasons as strings .. e.g. ['None', 'ConditionalCheckFailed', 'None']
11121106 """
@@ -1121,7 +1115,7 @@ def transaction_cancellation_reasons(err: ClientError) -> List[str]:
11211115 return [reason .strip () for reason in match .group (1 ).split ("," )]
11221116
11231117
1124- def transaction_cancelled_for_only_reasons (err : ClientError , * match_reason : str ) -> List [str ]:
1118+ def transaction_cancelled_for_only_reasons (err : ClientError , * match_reason : str ) -> list [str ]:
11251119 """
11261120 returns all reasons ... if all reasons either match match_reason or 'None'
11271121 """
@@ -1130,5 +1124,5 @@ def transaction_cancelled_for_only_reasons(err: ClientError, *match_reason: str)
11301124 return reasons if all (reason in match_reasons for reason in reasons ) else []
11311125
11321126
1133- def cancellation_reasons_if_conditional_check (err : ClientError ) -> List [str ]:
1127+ def cancellation_reasons_if_conditional_check (err : ClientError ) -> list [str ]:
11341128 return transaction_cancelled_for_only_reasons (err , "ConditionalCheckFailed" )
0 commit comments