11from __future__ import annotations
22
33import asyncio
4+ import functools
45import json
56import shutil
67import urllib .parse
@@ -119,7 +120,7 @@ async def open(
119120 kvs_base_path = Path (configuration .storage_dir ) / cls ._STORAGE_SUBDIR
120121
121122 if not kvs_base_path .exists ():
122- await asyncio .to_thread (lambda : kvs_base_path .mkdir ( parents = True , exist_ok = True ) )
123+ await asyncio .to_thread (kvs_base_path .mkdir , parents = True , exist_ok = True )
123124
124125 # Get a new instance by ID.
125126 if id :
@@ -133,7 +134,7 @@ async def open(
133134 continue
134135
135136 try :
136- file = await asyncio .to_thread (lambda p = path_to_metadata : p .open ( mode = 'r' , encoding = 'utf-8' ) )
137+ file = await asyncio .to_thread (path_to_metadata .open , mode = 'r' , encoding = 'utf-8' )
137138 try :
138139 file_content = json .load (file )
139140 metadata = KeyValueStoreMetadata (** file_content )
@@ -162,7 +163,7 @@ async def open(
162163
163164 # If the key-value store directory exists, reconstruct the client from the metadata file.
164165 if path_to_kvs .exists () and path_to_metadata .exists ():
165- file = await asyncio .to_thread (lambda : path_to_metadata .open ( mode = 'r' , encoding = 'utf-8' ) )
166+ file = await asyncio .to_thread (path_to_metadata .open , mode = 'r' , encoding = 'utf-8' )
166167 try :
167168 file_content = json .load (file )
168169 finally :
@@ -212,7 +213,7 @@ async def purge(self) -> None:
212213 for file_path in self .path_to_kvs .glob ('*' ):
213214 if file_path .name == METADATA_FILENAME :
214215 continue
215- await asyncio .to_thread (lambda f = file_path : f .unlink ( missing_ok = True ) )
216+ await asyncio .to_thread (file_path .unlink , missing_ok = True )
216217
217218 await self ._update_metadata (
218219 update_accessed_at = True ,
@@ -239,7 +240,9 @@ async def get_value(self, *, key: str) -> KeyValueStoreRecord | None:
239240 # Read the metadata file
240241 async with self ._lock :
241242 try :
242- file = await asyncio .to_thread (lambda : record_metadata_filepath .open (mode = 'r' , encoding = 'utf-8' ))
243+ file = await asyncio .to_thread (
244+ functools .partial (record_metadata_filepath .open , mode = 'r' , encoding = 'utf-8' ),
245+ )
243246 except FileNotFoundError :
244247 logger .warning (f'Metadata file disappeared for key "{ key } ", aborting get_value' )
245248 return None
@@ -346,11 +349,11 @@ async def delete_value(self, *, key: str) -> None:
346349 async with self ._lock :
347350 # Delete the value file and its metadata if found
348351 if record_path .exists ():
349- await asyncio .to_thread (lambda : record_path .unlink ( missing_ok = True ) )
352+ await asyncio .to_thread (record_path .unlink , missing_ok = True )
350353
351354 # Delete the metadata file if it exists
352355 if metadata_path .exists ():
353- await asyncio .to_thread (lambda : metadata_path .unlink ( missing_ok = True ) )
356+ await asyncio .to_thread (metadata_path .unlink , missing_ok = True )
354357 else :
355358 logger .warning (f'Found value file for key "{ key } " but no metadata file when trying to delete it.' )
356359
@@ -395,7 +398,7 @@ async def iterate_keys(
395398
396399 # Try to read and parse the metadata file
397400 try :
398- metadata_content = await asyncio .to_thread (lambda f = file_path : f .read_text ( encoding = 'utf-8' ) )
401+ metadata_content = await asyncio .to_thread (file_path .read_text , encoding = 'utf-8' )
399402 except FileNotFoundError :
400403 logger .warning (f'Metadata file disappeared for key "{ key_name } ", skipping it.' )
401404 continue
@@ -475,7 +478,7 @@ async def _update_metadata(
475478 self ._metadata .modified_at = now
476479
477480 # Ensure the parent directory for the metadata file exists.
478- await asyncio .to_thread (lambda : self .path_to_metadata .parent .mkdir ( parents = True , exist_ok = True ) )
481+ await asyncio .to_thread (self .path_to_metadata .parent .mkdir , parents = True , exist_ok = True )
479482
480483 # Dump the serialized metadata to the file.
481484 data = await json_dumps (self ._metadata .model_dump ())
0 commit comments