Skip to content

Commit 062b7a9

Browse files
Adjust the type hint for Graph open to reflect a SPARQLUpdateStore configuration (#3239)
* Adjust the type hint for Graph open to reflect a SPARQLUpdateStore configuration Also adjust the type hint for ReadOnlyGraphAggregate and for further stores * fix doc string --------- Co-authored-by: Edmond Chuc <[email protected]>
1 parent f358e89 commit 062b7a9

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

rdflib/graph.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ def rollback(self: _GraphT) -> _GraphT:
562562
self.__store.rollback()
563563
return self
564564

565-
def open(self, configuration: str, create: bool = False) -> Optional[int]:
565+
def open(
566+
self, configuration: Union[str, tuple[str, str]], create: bool = False
567+
) -> Optional[int]:
566568
"""Open the graph store
567569
568570
Might be necessary for stores that require opening a connection to a
@@ -2824,7 +2826,7 @@ def commit(self) -> NoReturn:
28242826
def rollback(self) -> NoReturn:
28252827
raise ModificationException()
28262828

2827-
def open(self, configuration: str, create: bool = False) -> None:
2829+
def open(self, configuration: str | tuple[str, str], create: bool = False) -> None:
28282830
# TODO: is there a use case for this method?
28292831
for graph in self.graphs:
28302832
# type error: Too many arguments for "open" of "Graph"

rdflib/plugins/stores/auditable.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from __future__ import annotations
1919

2020
import threading
21-
from typing import TYPE_CHECKING, Any, Generator, Iterator, List, Optional, Tuple
21+
from typing import TYPE_CHECKING, Any, Generator, Iterator, List, Optional, Tuple, Union
2222

2323
from rdflib.graph import ConjunctiveGraph, Graph
2424
from rdflib.store import Store
@@ -62,7 +62,9 @@ def __init__(self, store: Store):
6262
] = []
6363
self.rollbackLock = threading.RLock()
6464

65-
def open(self, configuration: str, create: bool = True) -> Optional[int]:
65+
def open(
66+
self, configuration: Union[str, tuple[str, str]], create: bool = True
67+
) -> Optional[int]:
6668
return self.store.open(configuration, create)
6769

6870
def close(self, commit_pending_transaction: bool = False) -> None:

rdflib/plugins/stores/berkeleydb.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44
from os import mkdir
55
from os.path import abspath, exists
66
from threading import Thread
7-
from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, List, Optional, Tuple
7+
from typing import (
8+
TYPE_CHECKING,
9+
Any,
10+
Callable,
11+
Dict,
12+
Generator,
13+
List,
14+
Optional,
15+
Tuple,
16+
Union,
17+
)
818
from urllib.request import pathname2url
919

1020
from rdflib.store import NO_STORE, VALID_STORE, Store
@@ -127,10 +137,16 @@ def _init_db_environment(
127137
def is_open(self) -> bool:
128138
return self.__open
129139

130-
def open(self, path: str, create: bool = True) -> Optional[int]:
140+
def open(
141+
self, configuration: Union[str, tuple[str, str]], create: bool = True
142+
) -> Optional[int]:
131143
if not has_bsddb:
132144
return NO_STORE
133-
homeDir = path # noqa: N806
145+
146+
if type(configuration) is str:
147+
homeDir = configuration # noqa: N806
148+
else:
149+
raise Exception("Invalid configuration provided")
134150

135151
if self.__identifier is None:
136152
self.__identifier = URIRef(pathname2url(abspath(homeDir)))

rdflib/plugins/stores/sparqlstore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ def __init__(
148148
self._queries = 0
149149

150150
# type error: Missing return statement
151-
def open(self, configuration: str, create: bool = False) -> Optional[int]: # type: ignore[return]
151+
def open(self, configuration: Union[str, tuple[str, str]], create: bool = False) -> Optional[int]: # type: ignore[return]
152152
"""This method is included so that calls to this Store via Graph, e.g. Graph("SPARQLStore"),
153153
can set the required parameters
154154
"""
155-
if type(configuration) == str: # noqa: E721
155+
if type(configuration) is str:
156156
self.query_endpoint = configuration
157157
else:
158158
raise Exception(

rdflib/store.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,10 @@ def node_pickler(self) -> NodePickler:
205205
def create(self, configuration: str) -> None:
206206
self.dispatcher.dispatch(StoreCreatedEvent(configuration=configuration))
207207

208-
def open(self, configuration: str, create: bool = False) -> Optional[int]:
209-
"""
210-
Opens the store specified by the configuration string. If
208+
def open(
209+
self, configuration: Union[str, tuple[str, str]], create: bool = False
210+
) -> Optional[int]:
211+
"""Opens the store specified by the configuration string or tuple. If
211212
create is True a store will be created if it does not already
212213
exist. If create is False and a store does not already exist
213214
an exception is raised. An exception is also raised if a store

0 commit comments

Comments
 (0)