55
66from __future__ import annotations
77
8- from collections . abc import Callable
9- from typing import TYPE_CHECKING , Any , ClassVar
8+ from contextlib import suppress
9+ from typing import TYPE_CHECKING , ClassVar
1010
1111from graphrag .config .enums import OutputType
1212from graphrag .storage .blob_pipeline_storage import create_blob_storage
1515from graphrag .storage .memory_pipeline_storage import MemoryPipelineStorage
1616
1717if TYPE_CHECKING :
18+ from collections .abc import Callable
19+
1820 from graphrag .storage .pipeline_storage import PipelineStorage
1921
2022
@@ -31,47 +33,52 @@ class StorageFactory:
3133 storage_types : ClassVar [dict [str , type ]] = {} # For backward compatibility
3234
3335 @classmethod
34- def register (cls , storage_type : str , creator : Callable [..., PipelineStorage ]) -> None :
36+ def register (
37+ cls , storage_type : str , creator : Callable [..., PipelineStorage ]
38+ ) -> None :
3539 """Register a custom storage implementation.
36-
40+
3741 Args:
3842 storage_type: The type identifier for the storage.
3943 creator: A callable that creates an instance of the storage.
4044 """
4145 cls ._storage_registry [storage_type ] = creator
42-
46+
4347 # For backward compatibility with code that may access storage_types directly
44- if callable (creator ) and hasattr (creator , "__annotations__" ) and "return" in creator .__annotations__ :
45- try :
48+ if (
49+ callable (creator )
50+ and hasattr (creator , "__annotations__" )
51+ and "return" in creator .__annotations__
52+ ):
53+ with suppress (TypeError , KeyError ):
4654 cls .storage_types [storage_type ] = creator .__annotations__ ["return" ]
47- except (TypeError , KeyError ):
48- # Just ignore if we can't maintain backward compatibility in this case
49- pass
5055
5156 @classmethod
5257 def create_storage (
5358 cls , storage_type : OutputType | str , kwargs : dict
5459 ) -> PipelineStorage :
5560 """Create a storage object from the provided type.
56-
61+
5762 Args:
5863 storage_type: The type of storage to create.
5964 kwargs: Additional keyword arguments for the storage constructor.
60-
65+
6166 Returns
6267 -------
6368 A PipelineStorage instance.
64-
69+
6570 Raises
6671 ------
6772 ValueError: If the storage type is not registered.
6873 """
69- storage_type_str = storage_type .value if isinstance (storage_type , OutputType ) else storage_type
70-
74+ storage_type_str = (
75+ storage_type .value if isinstance (storage_type , OutputType ) else storage_type
76+ )
77+
7178 if storage_type_str not in cls ._storage_registry :
7279 msg = f"Unknown storage type: { storage_type } "
7380 raise ValueError (msg )
74-
81+
7582 return cls ._storage_registry [storage_type_str ](** kwargs )
7683
7784 @classmethod
@@ -89,4 +96,4 @@ def is_supported_storage_type(cls, storage_type: str) -> bool:
8996StorageFactory .register (OutputType .blob .value , create_blob_storage )
9097StorageFactory .register (OutputType .cosmosdb .value , create_cosmosdb_storage )
9198StorageFactory .register (OutputType .file .value , create_file_storage )
92- StorageFactory .register (OutputType .memory .value , lambda ** kwargs : MemoryPipelineStorage ())
99+ StorageFactory .register (OutputType .memory .value , lambda ** _ : MemoryPipelineStorage ())
0 commit comments