Skip to content

Commit 74cdc5a

Browse files
committed
Remove using kwargs to swallow unknown factory config parameters. Unknown config parameters for a given implementation will throw an exception.
1 parent 4ecf218 commit 74cdc5a

File tree

8 files changed

+22
-41
lines changed

8 files changed

+22
-41
lines changed

packages/graphrag-storage/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from typing import Any
2929
from graphrag_storage import Storage, StorageConfig, create_storage, register_storage
3030

3131
class MyStorage(Storage):
32-
def __init__(self, some_setting: str, **kwargs: Any):
32+
def __init__(self, some_setting: str):
3333
# Validate settings and initialize
3434
...
3535

packages/graphrag-storage/graphrag_storage/azure_blob_storage.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,18 @@ class AzureBlobStorage(Storage):
3333

3434
def __init__(
3535
self,
36-
base_dir: str | None = None,
37-
connection_string: str | None = None,
36+
container_name: str,
3837
storage_account_blob_url: str | None = None,
39-
container_name: str | None = None,
38+
connection_string: str | None = None,
39+
base_dir: str | None = None,
4040
encoding: str = "utf-8",
41-
**kwargs: Any,
4241
) -> None:
4342
"""Create a new BlobStorage instance."""
44-
if connection_string is None and storage_account_blob_url is None:
45-
msg = "AzureBlobStorage requires either a connection_string or storage_account_blob_url to be specified."
46-
logger.error(msg)
47-
raise ValueError(msg)
48-
4943
if connection_string is not None and storage_account_blob_url is not None:
5044
msg = "AzureBlobStorage requires only one of connection_string or storage_account_blob_url to be specified, not both."
5145
logger.error(msg)
5246
raise ValueError(msg)
5347

54-
if container_name is None:
55-
msg = "AzureBlobStorage requires a container_name to be specified."
56-
logger.error(msg)
57-
raise ValueError(msg)
58-
5948
_validate_blob_container_name(container_name)
6049

6150
logger.info(
@@ -70,6 +59,11 @@ def __init__(
7059
account_url=storage_account_blob_url,
7160
credential=DefaultAzureCredential(),
7261
)
62+
else:
63+
msg = "AzureBlobStorage requires either a connection_string or storage_account_blob_url to be specified."
64+
logger.error(msg)
65+
raise ValueError(msg)
66+
7367
self._encoding = encoding
7468
self._container_name = container_name
7569
self._connection_string = connection_string

packages/graphrag-storage/graphrag_storage/azure_cosmos_storage.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ class AzureCosmosStorage(Storage):
4141

4242
def __init__(
4343
self,
44-
base_dir: str | None = None,
45-
container_name: str | None = None,
44+
base_dir: str,
45+
container_name: str,
4646
connection_string: str | None = None,
4747
cosmosdb_account_url: str | None = None,
4848
encoding: str = "utf-8",
49-
**kwargs: Any,
5049
) -> None:
5150
"""Create a CosmosDB storage instance."""
5251
logger.info("Creating cosmosdb storage")
@@ -56,28 +55,23 @@ def __init__(
5655
logger.error(msg)
5756
raise ValueError(msg)
5857

59-
if connection_string is None and cosmosdb_account_url is None:
60-
msg = "CosmosDB Storage requires either a connection_string or cosmosdb_account_url to be specified."
61-
logger.error(msg)
62-
raise ValueError(msg)
63-
6458
if connection_string is not None and cosmosdb_account_url is not None:
6559
msg = "CosmosDB Storage requires either a connection_string or cosmosdb_account_url to be specified, not both."
6660
logger.error(msg)
6761
raise ValueError(msg)
6862

69-
if container_name is None:
70-
msg = "CosmosDB Storage requires a container_name to be specified."
71-
logger.error(msg)
72-
raise ValueError(msg)
73-
7463
if connection_string:
7564
self._cosmos_client = CosmosClient.from_connection_string(connection_string)
7665
elif cosmosdb_account_url:
7766
self._cosmos_client = CosmosClient(
7867
url=cosmosdb_account_url,
7968
credential=DefaultAzureCredential(),
8069
)
70+
else:
71+
msg = "CosmosDB Storage requires either a connection_string or cosmosdb_account_url to be specified."
72+
logger.error(msg)
73+
raise ValueError(msg)
74+
8175
self._encoding = encoding
8276
self._database_name = database_name
8377
self._connection_string = connection_string

packages/graphrag-storage/graphrag_storage/file_storage.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,8 @@ class FileStorage(Storage):
3030
_base_dir: Path
3131
_encoding: str
3232

33-
def __init__(
34-
self, base_dir: str | None = None, encoding: str = "utf-8", **kwargs: Any
35-
) -> None:
33+
def __init__(self, base_dir: str, encoding: str = "utf-8") -> None:
3634
"""Create a file based storage."""
37-
if base_dir is None:
38-
msg = "FileStorage requires a base_dir to be specified."
39-
logger.error(msg)
40-
raise ValueError(msg)
41-
4235
self._base_dir = Path(base_dir).resolve()
4336
self._encoding = encoding
4437
logger.info("Creating file storage at [%s]", self._base_dir)

packages/graphrag-storage/graphrag_storage/memory_storage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, **kwargs: Any) -> None:
2222
"base_dir": "",
2323
**kwargs,
2424
}
25+
kwargs.pop("type", None)
2526
super().__init__(**kwargs)
2627
self._storage = {}
2728

packages/graphrag-storage/graphrag_storage/storage.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
class Storage(ABC):
1414
"""Provide a storage interface."""
1515

16-
@abstractmethod
17-
def __init__(self, **kwargs: Any) -> None:
18-
"""Create a storage instance."""
19-
2016
@abstractmethod
2117
def find(
2218
self,

packages/graphrag/graphrag/cache/factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ class CacheFactory(Factory[PipelineCache]):
3030
# --- register built-in cache implementations ---
3131
def create_file_cache(**kwargs) -> PipelineCache:
3232
"""Create a file-based cache implementation."""
33+
kwargs.pop("type", None)
3334
storage = FileStorage(**kwargs)
3435
return JsonPipelineCache(storage)
3536

3637

3738
def create_blob_cache(**kwargs) -> PipelineCache:
3839
"""Create a blob storage-based cache implementation."""
40+
kwargs.pop("type", None)
3941
storage = AzureBlobStorage(**kwargs)
4042
return JsonPipelineCache(storage)
4143

4244

4345
def create_cosmosdb_cache(**kwargs) -> PipelineCache:
4446
"""Create a CosmosDB-based cache implementation."""
47+
kwargs.pop("type", None)
4548
storage = AzureCosmosStorage(**kwargs)
4649
return JsonPipelineCache(storage)
4750

tests/integration/storage/test_file_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def test_get_creation_date():
4343

4444

4545
async def test_child():
46-
storage = FileStorage()
46+
storage = FileStorage(base_dir="")
4747
storage = storage.child("tests/fixtures/text/input")
4848
items = list(storage.find(re.compile(r".*\.txt$")))
4949
assert items == [str(Path("dulce.txt"))]

0 commit comments

Comments
 (0)