Skip to content

Commit a2e2699

Browse files
authored
Fixing documentation and type hints (#927)
* When renaming some of the re-auth APIs before stabilizing them in 5.8, some references in the docs were missed. * The docs for `ExpiringAuth` was completely missing becuase the `@preview` decorator broke sphinx. * Fix typing change in `datetime.replace` breaking us (arguments were changed from type `int` to `SupportsIndex`). As a virtual subclass, our custom temporal types should follow this change. * Fix broken layout of `execute_query` parameter docs.
1 parent 832200f commit a2e2699

File tree

7 files changed

+110
-97
lines changed

7 files changed

+110
-97
lines changed

docs/source/api.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ Closing a driver will immediately shut down all connections in the pool.
160160
query, use :meth:`neo4j.Driver.verify_connectivity`.
161161

162162
.. autoclass:: neo4j.Driver()
163-
:members: session, query_bookmark_manager, encrypted, close,
163+
:members: session, execute_query_bookmark_manager, encrypted, close,
164164
verify_connectivity, get_server_info, verify_authentication,
165165
supports_session_auth, supports_multi_db
166166

167-
.. method:: execute_query(query, parameters_=None,routing_=neo4j.RoutingControl.WRITE, database_=None, impersonated_user_=None, bookmark_manager_=self.query_bookmark_manager, result_transformer_=Result.to_eager_result, **kwargs)
167+
.. method:: execute_query(query, parameters_=None,routing_=neo4j.RoutingControl.WRITE, database_=None, impersonated_user_=None, bookmark_manager_=self.execute_query_bookmark_manager, result_transformer_=Result.to_eager_result, **kwargs)
168168

169169
Execute a query in a transaction function and return all results.
170170

@@ -283,9 +283,8 @@ Closing a driver will immediately shut down all connections in the pool.
283283
https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
284284

285285
See also the Session config :ref:`session-auth-ref`.
286-
:type auth_: typing.Union[
287-
typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None
288-
]
286+
:type auth_:
287+
typing.Union[typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None]
289288
:param result_transformer_:
290289
A function that gets passed the :class:`neo4j.Result` object
291290
resulting from the query and converts it to a different type. The
@@ -354,12 +353,11 @@ Closing a driver will immediately shut down all connections in the pool.
354353
If present, the bookmark manager is used to keep the query causally
355354
consistent with all work executed using the same bookmark manager.
356355

357-
Defaults to the driver's :attr:`.query_bookmark_manager`.
356+
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
358357

359358
Pass :data:`None` to disable causal consistency.
360359
:type bookmark_manager_:
361-
typing.Union[neo4j.BookmarkManager, neo4j.BookmarkManager,
362-
None]
360+
typing.Union[BookmarkManager, BookmarkManager, None]
363361
:param kwargs: additional keyword parameters. None of these can end
364362
with a single underscore. This is to avoid collisions with the
365363
keyword configuration parameters of this method. If you need to

docs/source/async_api.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ Closing a driver will immediately shut down all connections in the pool.
149149
query, use :meth:`neo4j.AsyncDriver.verify_connectivity`.
150150

151151
.. autoclass:: neo4j.AsyncDriver()
152-
:members: session, query_bookmark_manager, encrypted, close,
152+
:members: session, execute_query_bookmark_manager, encrypted, close,
153153
verify_connectivity, get_server_info, verify_authentication,
154154
supports_session_auth, supports_multi_db
155155

156-
.. method:: execute_query(query, parameters_=None, routing_=neo4j.RoutingControl.WRITE, database_=None, impersonated_user_=None, bookmark_manager_=self.query_bookmark_manager, result_transformer_=AsyncResult.to_eager_result, **kwargs)
156+
.. method:: execute_query(query, parameters_=None, routing_=neo4j.RoutingControl.WRITE, database_=None, impersonated_user_=None, bookmark_manager_=self.execute_query_bookmark_manager, result_transformer_=AsyncResult.to_eager_result, **kwargs)
157157
:async:
158158

159159
Execute a query in a transaction function and return all results.
@@ -273,9 +273,8 @@ Closing a driver will immediately shut down all connections in the pool.
273273
https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
274274

275275
See also the Session config :ref:`session-auth-ref`.
276-
:type auth_: typing.Union[
277-
typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None
278-
]
276+
:type auth_:
277+
typing.Union[typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None]
279278
:param result_transformer_:
280279
A function that gets passed the :class:`neo4j.AsyncResult` object
281280
resulting from the query and converts it to a different type. The
@@ -344,12 +343,11 @@ Closing a driver will immediately shut down all connections in the pool.
344343
If present, the bookmark manager is used to keep the query causally
345344
consistent with all work executed using the same bookmark manager.
346345

347-
Defaults to the driver's :attr:`.query_bookmark_manager`.
346+
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
348347

349348
Pass :data:`None` to disable causal consistency.
350349
:type bookmark_manager_:
351-
typing.Union[neo4j.AsyncBookmarkManager, neo4j.BookmarkManager,
352-
None]
350+
typing.Union[AsyncBookmarkManager, BookmarkManager, None]
353351
:param kwargs: additional keyword parameters. None of these can end
354352
with a single underscore. This is to avoid collisions with the
355353
keyword configuration parameters of this method. If you need to

src/neo4j/_async/driver.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -805,12 +805,11 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
805805
If present, the bookmark manager is used to keep the query causally
806806
consistent with all work executed using the same bookmark manager.
807807
808-
Defaults to the driver's :attr:`.query_bookmark_manager`.
808+
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
809809
810810
Pass :data:`None` to disable causal consistency.
811811
:type bookmark_manager_:
812-
typing.Union[neo4j.AsyncBookmarkManager, neo4j.BookmarkManager,
813-
None]
812+
typing.Union[AsyncBookmarkManager, BookmarkManager, None]
814813
:param kwargs: additional keyword parameters. None of these can end
815814
with a single underscore. This is to avoid collisions with the
816815
keyword configuration parameters of this method. If you need to
@@ -875,7 +874,7 @@ def execute_query_bookmark_manager(self) -> AsyncBookmarkManager:
875874
async def example(driver: neo4j.AsyncDriver) -> None:
876875
await driver.execute_query("<QUERY 1>")
877876
async with driver.session(
878-
bookmark_manager=driver.query_bookmark_manager
877+
bookmark_manager=driver.execute_query_bookmark_manager
879878
) as session:
880879
# every query inside this session will be causally chained
881880
# (i.e., can read what was written by <QUERY 1>)

src/neo4j/_auth_management.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
class ExpiringAuth:
3535
"""Represents potentially expiring authentication information.
3636
37-
This class is used with :meth:`.AuthManagers.temporal` and
38-
:meth:`.AsyncAuthManagers.temporal`.
37+
This class is used with :meth:`.AuthManagers.expiration_based` and
38+
:meth:`.AsyncAuthManagers.expiration_based`.
3939
4040
:param auth: The authentication information.
4141
:param expires_in: The number of seconds until the authentication
@@ -47,7 +47,8 @@ class ExpiringAuth:
4747
See also https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
4848
4949
.. seealso::
50-
:meth:`.AuthManagers.temporal`, :meth:`.AsyncAuthManagers.temporal`
50+
:meth:`.AuthManagers.expiration_based`,
51+
:meth:`.AsyncAuthManagers.expiration_based`
5152
5253
.. versionadded:: 5.8
5354
"""

src/neo4j/_meta.py

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
# limitations under the License.
1717

1818

19+
from __future__ import annotations
20+
1921
import asyncio
2022
import tracemalloc
2123
import typing as t
2224
from functools import wraps
25+
from inspect import isclass
2326
from warnings import warn
2427

2528

@@ -67,23 +70,7 @@ def foo(x):
6770
pass
6871
6972
"""
70-
def decorator(f):
71-
if asyncio.iscoroutinefunction(f):
72-
@wraps(f)
73-
async def inner(*args, **kwargs):
74-
deprecation_warn(message, stack_level=2)
75-
return await f(*args, **kwargs)
76-
77-
return inner
78-
else:
79-
@wraps(f)
80-
def inner(*args, **kwargs):
81-
deprecation_warn(message, stack_level=2)
82-
return f(*args, **kwargs)
83-
84-
return inner
85-
86-
return decorator
73+
return _make_warning_decorator(message, deprecation_warn)
8774

8875

8976
def deprecated_property(message: str):
@@ -104,20 +91,6 @@ def experimental_warn(message, stack_level=1):
10491
warn(message, category=ExperimentalWarning, stacklevel=stack_level + 1)
10592

10693

107-
class PreviewWarning(Warning):
108-
""" Base class for warnings about experimental features.
109-
"""
110-
111-
112-
def preview_warn(message, stack_level=1):
113-
message += (
114-
" It might be changed without following the deprecation policy. "
115-
"See also "
116-
"https://github.com/neo4j/neo4j-python-driver/wiki/preview-features."
117-
)
118-
warn(message, category=PreviewWarning, stacklevel=stack_level + 1)
119-
120-
12194
def experimental(message) -> t.Callable[[_FuncT], _FuncT]:
12295
""" Decorator for tagging experimental functions and methods.
12396
@@ -147,7 +120,21 @@ def inner(*args, **kwargs):
147120

148121
return inner
149122

150-
return decorator
123+
return _make_warning_decorator(message, experimental_warn)
124+
125+
126+
class PreviewWarning(Warning):
127+
""" Base class for warnings about experimental features.
128+
"""
129+
130+
131+
def preview_warn(message, stack_level=1):
132+
message += (
133+
" It might be changed without following the deprecation policy. "
134+
"See also "
135+
"https://github.com/neo4j/neo4j-python-driver/wiki/preview-features."
136+
)
137+
warn(message, category=PreviewWarning, stacklevel=stack_level + 1)
151138

152139

153140
def preview(message) -> t.Callable[[_FuncT], _FuncT]:
@@ -158,18 +145,47 @@ def preview(message) -> t.Callable[[_FuncT], _FuncT]:
158145
def foo(x):
159146
pass
160147
"""
148+
149+
return _make_warning_decorator(message, preview_warn)
150+
151+
152+
if t.TYPE_CHECKING:
153+
class _WarningFunc(t.Protocol):
154+
def __call__(self, message: str, stack_level: int = 1) -> None:
155+
...
156+
else:
157+
_WarningFunc = object
158+
159+
160+
def _make_warning_decorator(
161+
message: str,
162+
warning_func: _WarningFunc,
163+
) -> t.Callable[[_FuncT], _FuncT]:
161164
def decorator(f):
162165
if asyncio.iscoroutinefunction(f):
163166
@wraps(f)
164167
async def inner(*args, **kwargs):
165-
preview_warn(message, stack_level=2)
168+
warning_func(message, stack_level=2)
166169
return await f(*args, **kwargs)
167170

168171
return inner
172+
if isclass(f):
173+
if hasattr(f, "__init__"):
174+
original_init = f.__init__
175+
@wraps(original_init)
176+
def inner(*args, **kwargs):
177+
warning_func(message, stack_level=2)
178+
return original_init(*args, **kwargs)
179+
180+
f.__init__ = inner
181+
return f
182+
raise TypeError(
183+
"Cannot decorate class without __init__"
184+
)
169185
else:
170186
@wraps(f)
171187
def inner(*args, **kwargs):
172-
preview_warn(message, stack_level=2)
188+
warning_func(message, stack_level=2)
173189
return f(*args, **kwargs)
174190

175191
return inner

src/neo4j/_sync/driver.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,11 @@ def example(driver: neo4j.Driver) -> neo4j.Record::
804804
If present, the bookmark manager is used to keep the query causally
805805
consistent with all work executed using the same bookmark manager.
806806
807-
Defaults to the driver's :attr:`.query_bookmark_manager`.
807+
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
808808
809809
Pass :data:`None` to disable causal consistency.
810810
:type bookmark_manager_:
811-
typing.Union[neo4j.BookmarkManager, neo4j.BookmarkManager,
812-
None]
811+
typing.Union[BookmarkManager, BookmarkManager, None]
813812
:param kwargs: additional keyword parameters. None of these can end
814813
with a single underscore. This is to avoid collisions with the
815814
keyword configuration parameters of this method. If you need to
@@ -874,7 +873,7 @@ def execute_query_bookmark_manager(self) -> BookmarkManager:
874873
def example(driver: neo4j.Driver) -> None:
875874
driver.execute_query("<QUERY 1>")
876875
with driver.session(
877-
bookmark_manager=driver.query_bookmark_manager
876+
bookmark_manager=driver.execute_query_bookmark_manager
878877
) as session:
879878
# every query inside this session will be causally chained
880879
# (i.e., can read what was written by <QUERY 1>)

0 commit comments

Comments
 (0)