Skip to content

Commit 381616e

Browse files
authored
Deprecate raw initial_bookmarks for bookmark_manager (#1196)
Deprecate passing raw sting bookmarks as `initial_bookmarks` to `GraphDatabase.bookmark_manager()`. Use a `neo4j.Bookmarks` object instead. Fixup changelog & type hints amending #1175
1 parent ff67c2a commit 381616e

File tree

9 files changed

+90
-47
lines changed

9 files changed

+90
-47
lines changed

CHANGELOG.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
6262
- Change string representation of `Neo4jError` to include GQL error information.
6363
- Remove deprecated `Record.__getslice__`. This magic method has been removed in Python 3.0.
6464
If you were calling it directly, please use `Record.__getitem__(slice(...))` or simply `record[...]` instead.
65-
- Remove deprecated class `neo4j.Bookmark` in favor of `neo4j.Bookmarks`.
66-
- Remove deprecated class `session.last_bookmark()` in favor of `last_bookmarks()`.
65+
- Bookmarks
66+
- Remove deprecated class `neo4j.Bookmark` in favor of `neo4j.Bookmarks`.
67+
- Remove deprecated class `session.last_bookmark()` in favor of `last_bookmarks()`.
68+
- Deprecate passing raw sting bookmarks as `initial_bookmarks` to `GraphDatabase.bookmark_manager()`.
69+
Use a `neo4j.Bookmarks` object instead.
70+
- `Driver.session()` no longer accepts raw string bookmarks as `bookmarks` argument.
71+
Use a `neo4j.Bookmarks` object instead.
6772
- Remove deprecated `ServerInfo.connection_id`.
6873
There is no replacement as this is considered internal information.
6974
- Remove deprecated driver configuration option `trust`.
@@ -234,17 +239,17 @@ See also https://github.com/neo4j/neo4j-python-driver/wiki for a full changelog.
234239
manager related methods:
235240
- `neo4j.BookmarkManger` and `neo4j.AsyncBookmarkManger` abstract base
236241
classes:
237-
- ``update_bookmarks`` has no longer a ``database`` argument.
238-
- ``get_bookmarks`` has no longer a ``database`` argument.
239-
- The ``get_all_bookmarks`` method was removed.
240-
- The ``forget`` method was removed.
242+
- `update_bookmarks` has no longer a `database` argument.
243+
- `get_bookmarks` has no longer a `database` argument.
244+
- The `get_all_bookmarks` method was removed.
245+
- The `forget` method was removed.
241246
- `neo4j.GraphDatabase.bookmark_manager` and
242247
`neo4j.AsyncGraphDatabase.bookmark_manager` factory methods:
243-
- ``initial_bookmarks`` is no longer a mapping from database name
248+
- `initial_bookmarks` is no longer a mapping from database name
244249
to bookmarks but plain bookmarks.
245-
- ``bookmarks_supplier`` no longer receives the database name as
250+
- `bookmarks_supplier` no longer receives the database name as
246251
an argument.
247-
- ``bookmarks_consumer`` no longer receives the database name as
252+
- `bookmarks_consumer` no longer receives the database name as
248253
an argument.
249254

250255

src/neo4j/_async/bookmark_manager.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,10 @@
3030
TBmConsumer = t.Callable[[Bookmarks], None | t.Awaitable[None]]
3131

3232

33-
def _bookmarks_to_set(
34-
bookmarks: Bookmarks | t.Iterable[str],
35-
) -> set[str]:
36-
if isinstance(bookmarks, Bookmarks):
37-
return set(bookmarks.raw_values)
38-
return set(map(str, bookmarks))
39-
40-
4133
class AsyncNeo4jBookmarkManager(AsyncBookmarkManager):
4234
def __init__(
4335
self,
44-
initial_bookmarks: Bookmarks | t.Iterable[str] | None = None,
36+
initial_bookmarks: Bookmarks | None = None,
4537
bookmarks_supplier: TBmSupplier | None = None,
4638
bookmarks_consumer: TBmConsumer | None = None,
4739
) -> None:
@@ -51,13 +43,7 @@ def __init__(
5143
if not initial_bookmarks:
5244
self._bookmarks = set()
5345
else:
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-
)
46+
self._bookmarks = set(initial_bookmarks.raw_values)
6147
self._lock = AsyncCooperativeLock()
6248

6349
async def update_bookmarks(

src/neo4j/_async/driver.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import asyncio
2020
import typing as t
21+
from types import NoneType
2122

2223

2324
if t.TYPE_CHECKING:
@@ -51,6 +52,7 @@
5152
)
5253
from .._debug import ENABLED as DEBUG_ENABLED
5354
from .._warnings import (
55+
deprecation_warn,
5456
preview_warn,
5557
unclosed_resource_warn,
5658
)
@@ -335,6 +337,11 @@ def bookmark_manager(
335337
:param initial_bookmarks:
336338
The initial set of bookmarks. The returned bookmark manager will
337339
use this to initialize its internal bookmarks.
340+
341+
.. deprecated:: 6.0
342+
Passing raw string bookmarks is deprecated.
343+
Use a :class:`.Bookmarks` object instead.
344+
338345
:param bookmarks_supplier:
339346
Function which will be called every time the default bookmark
340347
manager's method :meth:`.AsyncBookmarkManager.get_bookmarks`
@@ -367,9 +374,27 @@ def bookmark_manager(
367374
an argument.
368375
369376
.. versionchanged:: 5.8 Stabilized from experimental.
377+
378+
.. versionchanged:: 6.0
379+
Deprecated passing raw string bookmarks as initial_bookmarks.
370380
"""
381+
cast_initial_bookmarks: Bookmarks | None
382+
# TODO: 7.0 - remove raw bookmark support
383+
if not isinstance(initial_bookmarks, (Bookmarks, NoneType)):
384+
deprecation_warn(
385+
(
386+
"Passing raw strings as initial_bookmarks is deprecated. "
387+
"Use a Bookmarks object instead."
388+
),
389+
stack_level=2,
390+
)
391+
cast_initial_bookmarks = Bookmarks.from_raw_values(
392+
t.cast(t.Iterable[str], initial_bookmarks)
393+
)
394+
else:
395+
cast_initial_bookmarks = initial_bookmarks
371396
return AsyncNeo4jBookmarkManager(
372-
initial_bookmarks=initial_bookmarks,
397+
initial_bookmarks=cast_initial_bookmarks,
373398
bookmarks_supplier=bookmarks_supplier,
374399
bookmarks_consumer=bookmarks_consumer,
375400
)
@@ -520,7 +545,7 @@ def session(
520545
database: str | None = ...,
521546
fetch_size: int = ...,
522547
impersonated_user: str | None = ...,
523-
bookmarks: t.Iterable[str] | Bookmarks | None = ...,
548+
bookmarks: Bookmarks | None = ...,
524549
default_access_mode: str = ...,
525550
bookmark_manager: (
526551
AsyncBookmarkManager | BookmarkManager | None

src/neo4j/_sync/bookmark_manager.py

Lines changed: 2 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/neo4j/_sync/driver.py

Lines changed: 27 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testkitbackend/_async/requests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,11 @@ async def new_bookmark_manager(backend, data):
624624

625625
bmm_kwargs = {}
626626
data.mark_item_as_read("initialBookmarks", recursive=True)
627-
bmm_kwargs["initial_bookmarks"] = data.get("initialBookmarks")
627+
initial_bookmarks = data.get("initialBookmarks")
628+
if initial_bookmarks is not None:
629+
bmm_kwargs["initial_bookmarks"] = neo4j.Bookmarks.from_raw_values(
630+
initial_bookmarks
631+
)
628632
if data.get("bookmarksSupplierRegistered"):
629633
bmm_kwargs["bookmarks_supplier"] = bookmarks_supplier(
630634
backend, bookmark_manager_id

testkitbackend/_sync/requests.py

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/unit/async_/test_bookmark_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from __future__ import annotations
1818

1919
import typing as t
20+
from types import NoneType
2021

2122
import pytest
2223

@@ -34,6 +35,9 @@
3435

3536
@copy_signature(neo4j.AsyncGraphDatabase.bookmark_manager)
3637
def bookmark_manager(*args, **kwargs):
38+
if not isinstance(kwargs.get("initial_bookmarks"), (NoneType, Bookmarks)):
39+
with pytest.warns(DeprecationWarning, match="initial_bookmarks"):
40+
return neo4j.AsyncGraphDatabase.bookmark_manager(*args, **kwargs)
3741
return neo4j.AsyncGraphDatabase.bookmark_manager(*args, **kwargs)
3842

3943

tests/unit/sync/test_bookmark_manager.py

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)