1414from collections .abc import AsyncIterator , Awaitable , Callable
1515from dataclasses import dataclass
1616from pathlib import Path
17- from typing import Any
17+ from typing import Any , Final
1818
1919import botocore .exceptions
2020import pytest
4949from types_aiobotocore_s3 import S3Client
5050from types_aiobotocore_s3 .literals import BucketLocationConstraintType
5151
52+ _BYTE_SIZE_ADAPTER : Final [TypeAdapter [ByteSize ]] = TypeAdapter (ByteSize )
53+
5254
5355@pytest .fixture
5456async def simcore_s3_api (
@@ -111,7 +113,7 @@ async def _(
111113 file ,
112114 MultiPartUploadLinks (
113115 upload_id = "fake" ,
114- chunk_size = TypeAdapter ( ByteSize ) .validate_python (file .stat ().st_size ),
116+ chunk_size = _BYTE_SIZE_ADAPTER .validate_python (file .stat ().st_size ),
115117 urls = [presigned_url ],
116118 ),
117119 )
@@ -135,7 +137,7 @@ async def with_uploaded_file_on_s3(
135137 s3_client : S3Client ,
136138 with_s3_bucket : S3BucketName ,
137139) -> AsyncIterator [UploadedFile ]:
138- test_file = create_file_of_size (TypeAdapter ( ByteSize ) .validate_python ("10Kib" ))
140+ test_file = create_file_of_size (_BYTE_SIZE_ADAPTER .validate_python ("10Kib" ))
139141 await s3_client .upload_file (
140142 Filename = f"{ test_file } " ,
141143 Bucket = with_s3_bucket ,
@@ -590,7 +592,7 @@ async def test_undelete_file(
590592 assert file_metadata .size == with_uploaded_file_on_s3 .local_path .stat ().st_size
591593
592594 # upload another file on top of the existing one
593- new_file = create_file_of_size (TypeAdapter ( ByteSize ) .validate_python ("5Kib" ))
595+ new_file = create_file_of_size (_BYTE_SIZE_ADAPTER .validate_python ("5Kib" ))
594596 await s3_client .upload_file (
595597 Filename = f"{ new_file } " ,
596598 Bucket = with_s3_bucket ,
@@ -745,7 +747,7 @@ async def test_create_single_presigned_upload_link(
745747 [Path , AnyUrl , S3BucketName , S3ObjectKey ], Awaitable [None ]
746748 ],
747749):
748- file = create_file_of_size (TypeAdapter ( ByteSize ) .validate_python ("1Mib" ))
750+ file = create_file_of_size (_BYTE_SIZE_ADAPTER .validate_python ("1Mib" ))
749751 s3_object_key = file .name
750752 presigned_url = await simcore_s3_api .create_single_presigned_upload_link (
751753 bucket = with_s3_bucket ,
@@ -773,7 +775,7 @@ async def test_create_single_presigned_upload_link_with_non_existing_bucket_rais
773775 create_file_of_size : Callable [[ByteSize ], Path ],
774776 default_expiration_time_seconds : int ,
775777):
776- file = create_file_of_size (TypeAdapter ( ByteSize ) .validate_python ("1Mib" ))
778+ file = create_file_of_size (_BYTE_SIZE_ADAPTER .validate_python ("1Mib" ))
777779 s3_object_key = file .name
778780 with pytest .raises (S3BucketInvalidError ):
779781 await simcore_s3_api .create_single_presigned_upload_link (
@@ -1080,7 +1082,7 @@ async def test_copy_file_invalid_raises(
10801082 create_file_of_size : Callable [[ByteSize ], Path ],
10811083 faker : Faker ,
10821084):
1083- file = create_file_of_size (TypeAdapter ( ByteSize ) .validate_python ("1MiB" ))
1085+ file = create_file_of_size (_BYTE_SIZE_ADAPTER .validate_python ("1MiB" ))
10841086 uploaded_file = await upload_file (file )
10851087 dst_object_key = faker .file_name ()
10861088 # NOTE: since aioboto3 13.1.0 this raises S3KeyNotFoundError instead of S3BucketInvalidError
@@ -1105,9 +1107,9 @@ async def test_copy_file_invalid_raises(
11051107 "directory_size, min_file_size, max_file_size" ,
11061108 [
11071109 (
1108- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1109- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1110- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1110+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1111+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1112+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
11111113 )
11121114 ],
11131115 ids = byte_size_ids ,
@@ -1131,9 +1133,9 @@ async def test_get_directory_metadata(
11311133 "directory_size, min_file_size, max_file_size" ,
11321134 [
11331135 (
1134- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1135- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1136- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1136+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1137+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1138+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
11371139 )
11381140 ],
11391141 ids = byte_size_ids ,
@@ -1163,9 +1165,9 @@ async def test_get_directory_metadata_raises(
11631165 "directory_size, min_file_size, max_file_size" ,
11641166 [
11651167 (
1166- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1167- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1168- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1168+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1169+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1170+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
11691171 )
11701172 ],
11711173 ids = byte_size_ids ,
@@ -1199,9 +1201,9 @@ async def test_delete_file_recursively(
11991201 "directory_size, min_file_size, max_file_size" ,
12001202 [
12011203 (
1202- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1203- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1204- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1204+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1205+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1206+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
12051207 )
12061208 ],
12071209 ids = byte_size_ids ,
@@ -1237,9 +1239,9 @@ async def test_delete_file_recursively_raises(
12371239 "directory_size, min_file_size, max_file_size" ,
12381240 [
12391241 (
1240- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1241- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1242- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1242+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1243+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1244+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
12431245 )
12441246 ],
12451247 ids = byte_size_ids ,
@@ -1337,14 +1339,14 @@ def run_async_test(*args, **kwargs) -> None:
13371339 "directory_size, min_file_size, max_file_size" ,
13381340 [
13391341 (
1340- TypeAdapter ( ByteSize ) .validate_python ("1Mib" ),
1341- TypeAdapter ( ByteSize ) .validate_python ("1B" ),
1342- TypeAdapter ( ByteSize ) .validate_python ("10Kib" ),
1342+ _BYTE_SIZE_ADAPTER .validate_python ("1Mib" ),
1343+ _BYTE_SIZE_ADAPTER .validate_python ("1B" ),
1344+ _BYTE_SIZE_ADAPTER .validate_python ("10Kib" ),
13431345 ),
13441346 (
1345- TypeAdapter ( ByteSize ) .validate_python ("500Mib" ),
1346- TypeAdapter ( ByteSize ) .validate_python ("10Mib" ),
1347- TypeAdapter ( ByteSize ) .validate_python ("50Mib" ),
1347+ _BYTE_SIZE_ADAPTER .validate_python ("500Mib" ),
1348+ _BYTE_SIZE_ADAPTER .validate_python ("10Mib" ),
1349+ _BYTE_SIZE_ADAPTER .validate_python ("50Mib" ),
13481350 ),
13491351 ],
13501352 ids = byte_size_ids ,
0 commit comments