11import logging
2+ from collections .abc import Iterator
23from contextlib import contextmanager
34from datetime import datetime
45from pathlib import Path
56from time import sleep , time
6- from typing import Any , Dict , Iterator , List , Literal , Optional , Tuple , Union
7+ from typing import Any , Literal
78
89from biocframe import BiocFrame
910from sqlalchemy import create_engine , func , text
@@ -39,7 +40,7 @@ class BiocFileCache:
3940 - Cleanup of expired resources
4041 """
4142
42- def __init__ (self , cache_dir : Optional [ Union [ str , Path ]] = None , config : Optional [ CacheConfig ] = None ):
43+ def __init__ (self , cache_dir : str | Path | None = None , config : CacheConfig | None = None ):
4344 """Initialize cache with optional configuration.
4445
4546 Args:
@@ -106,7 +107,7 @@ def _setup_database(self) -> None:
106107
107108 return SCHEMA_VERSION
108109
109- def _get_detached_resource (self , session : Session , obj : Union [ Resource , Metadata ] ) -> Optional [ dict ] :
110+ def _get_detached_resource (self , session : Session , obj : Resource | Metadata ) -> dict | None :
110111 """Get a detached copy of a resource."""
111112 if obj is None :
112113 return None
@@ -205,7 +206,7 @@ def cleanup(self) -> int:
205206 ######>> get resources <<######
206207 ###############################
207208
208- def get (self , rname : str = None , rid : str = None ) -> Optional [ dict ] :
209+ def get (self , rname : str = None , rid : str = None ) -> dict | None :
209210 """Get resource by name from cache.
210211
211212 Args:
@@ -244,10 +245,10 @@ def get(self, rname: str = None, rid: str = None) -> Optional[dict]:
244245 def add (
245246 self ,
246247 rname : str ,
247- fpath : Union [ str , Path ] ,
248+ fpath : str | Path ,
248249 rtype : Literal ["local" , "web" , "relative" ] = "relative" ,
249250 action : Literal ["copy" , "move" , "asis" ] = "copy" ,
250- expires : Optional [ datetime ] = None ,
251+ expires : datetime | None = None ,
251252 download : bool = True ,
252253 ext : bool = True ,
253254 ) -> dict :
@@ -334,7 +335,7 @@ def add(
334335 session .commit ()
335336 raise Exception ("Failed to add resource" ) from e
336337
337- def add_batch (self , resources : List [ Dict [str , Any ]]) -> BiocFrame :
338+ def add_batch (self , resources : list [ dict [str , Any ]]) -> BiocFrame :
338339 """Add multiple resources in a single transaction.
339340
340341 Args:
@@ -356,7 +357,7 @@ def add_batch(self, resources: List[Dict[str, Any]]) -> BiocFrame:
356357 def update (
357358 self ,
358359 rname : str ,
359- fpath : Union [ str , Path ] ,
360+ fpath : str | Path ,
360361 action : Literal ["copy" , "move" , "asis" ] = "copy" ,
361362 ) -> dict :
362363 """Update an existing resource.
@@ -430,7 +431,7 @@ def remove(self, rname: str) -> None:
430431 session .rollback ()
431432 raise Exception (f"Failed to remove resource '{ rname } '" ) from e
432433
433- def list_resources (self , rtype : Optional [ str ] = None , expired : Optional [ bool ] = None ) -> BiocFrame :
434+ def list_resources (self , rtype : str | None = None , expired : bool | None = None ) -> BiocFrame :
434435 """List resources in the cache with optional filtering.
435436
436437 Args:
@@ -521,7 +522,7 @@ def validate_resource(self, resource: Resource) -> bool:
521522 # session.merge(resource)
522523 # session.commit()
523524
524- def verify_cache (self ) -> Tuple [int , int ]:
525+ def verify_cache (self ) -> tuple [int , int ]:
525526 """Verify integrity of all cached resources.
526527
527528 Returns:
@@ -559,7 +560,7 @@ def search(self, query: str, field: str = "rname", exact: bool = False) -> BiocF
559560
560561 return BiocFrame (convert_to_columnar ([self ._get_detached_resource (session , r ) for r in resources ]))
561562
562- def get_stats (self ) -> Dict [str , Any ]:
563+ def get_stats (self ) -> dict [str , Any ]:
563564 """Get statistics about the cache."""
564565 with self .get_session () as session :
565566 total = session .query (Resource ).count ()
0 commit comments