Skip to content

Commit 01d4fdc

Browse files
authored
Make usage of raw bookmarks easier (#1065)
(Async)Neo4jBookmarkManager's `initial_bookmarks` parameter, as well as `Bookmarks.from_raw_values` `values` parameter accept anything of type `Iterable[str]`. Unfortunately, `str` itself implements that type and yields the characters of the string. That most certainly not what the user intended. In an ideal world, we could tell the type checker to only accept `Iterable[str]` when *not* of type `str`. See also python/typing#256. To help users out, we now explicitly check for `isinstance(input, str)` and turn such inputs into an iterable with the input string as the only element.
1 parent fae56a3 commit 01d4fdc

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/neo4j/_async/bookmark_manager.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ def __init__(
5151
if not initial_bookmarks:
5252
self._bookmarks = set()
5353
else:
54-
self._bookmarks = set(getattr(
55-
initial_bookmarks, "raw_values",
56-
t.cast(t.Iterable[str], initial_bookmarks)
57-
))
54+
if not hasattr(initial_bookmarks, "raw_values"):
55+
initial_bookmarks = Bookmarks.from_raw_values(
56+
t.cast(t.Iterable[str], initial_bookmarks)
57+
)
58+
self._bookmarks = set(
59+
t.cast(Bookmarks, initial_bookmarks).raw_values
60+
)
5861
self._lock = AsyncCooperativeLock()
5962

6063
async def update_bookmarks(

src/neo4j/_sync/bookmark_manager.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ def __init__(
5151
if not initial_bookmarks:
5252
self._bookmarks = set()
5353
else:
54-
self._bookmarks = set(getattr(
55-
initial_bookmarks, "raw_values",
56-
t.cast(t.Iterable[str], initial_bookmarks)
57-
))
54+
if not hasattr(initial_bookmarks, "raw_values"):
55+
initial_bookmarks = Bookmarks.from_raw_values(
56+
t.cast(t.Iterable[str], initial_bookmarks)
57+
)
58+
self._bookmarks = set(
59+
t.cast(Bookmarks, initial_bookmarks).raw_values
60+
)
5861
self._lock = CooperativeLock()
5962

6063
def update_bookmarks(

src/neo4j/api.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,12 @@ def from_raw_values(cls, values: t.Iterable[str]) -> Bookmarks:
287287
bookmarks.
288288
289289
:param values: ASCII string values (raw bookmarks)
290-
:type values: Iterable[str]
291290
"""
291+
if isinstance(values, str):
292+
# Unfortunately, str itself is an iterable of str, iterating
293+
# over characters. Type checkers will not catch this, so we help
294+
# the user out.
295+
values = values,
292296
obj = cls()
293297
bookmarks = []
294298
for value in values:

0 commit comments

Comments
 (0)