Skip to content

Commit 79747bb

Browse files
authored
Work around Sphinx issues with postponed evaluation of annotations
With `from __future__ import annotations` in the code, Sphinx gets easily confused. This applies to several areas like: * Type aliases * Things defined in `if typing.TYPE_CHECKING` blocks * Usage of the `typing_extensions` module This PR aims to work around as many of those quirks as possible.
1 parent 1bd382f commit 79747bb

File tree

16 files changed

+130
-85
lines changed

16 files changed

+130
-85
lines changed

docs/source/api.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,11 @@ Closing a driver will immediately shut down all connections in the pool.
248248
:param query_: cypher query to execute
249249
:type query_: typing.LiteralString
250250
:param parameters_: parameters to use in the query
251-
:type parameters_: typing.Optional[typing.Dict[str, typing.Any]]
251+
:type parameters_: typing.Dict[str, typing.Any] | None
252252
:param routing_:
253253
whether to route the query to a reader (follower/read replica) or
254254
a writer (leader) in the cluster. Default is to route to a writer.
255-
:type routing_: neo4j.RoutingControl
255+
:type routing_: RoutingControl
256256
:param database_:
257257
database to execute the query against.
258258

@@ -264,7 +264,7 @@ Closing a driver will immediately shut down all connections in the pool.
264264
as it will not have to resolve the default database first.
265265

266266
See also the Session config :ref:`database-ref`.
267-
:type database_: typing.Optional[str]
267+
:type database_: str | None
268268
:param impersonated_user_:
269269
Name of the user to impersonate.
270270

@@ -274,7 +274,7 @@ Closing a driver will immediately shut down all connections in the pool.
274274
permissions.
275275

276276
See also the Session config :ref:`impersonated-user-ref`.
277-
:type impersonated_user_: typing.Optional[str]
277+
:type impersonated_user_: str | None
278278
:param auth_:
279279
Authentication information to use for this query.
280280

@@ -286,8 +286,7 @@ Closing a driver will immediately shut down all connections in the pool.
286286
https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
287287

288288
See also the Session config :ref:`session-auth-ref`.
289-
:type auth_:
290-
typing.Union[typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None]
289+
:type auth_: typing.Tuple[typing.Any, typing.Any] | Auth | None
291290
:param result_transformer_:
292291
A function that gets passed the :class:`neo4j.Result` object
293292
resulting from the query and converts it to a different type. The
@@ -349,7 +348,7 @@ Closing a driver will immediately shut down all connections in the pool.
349348
)
350349

351350
:type result_transformer_:
352-
typing.Callable[[neo4j.Result], typing.Union[T]]
351+
typing.Callable[[Result], T]
353352
:param bookmark_manager_:
354353
Specify a bookmark manager to use.
355354

@@ -359,8 +358,7 @@ Closing a driver will immediately shut down all connections in the pool.
359358
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
360359

361360
Pass :data:`None` to disable causal consistency.
362-
:type bookmark_manager_:
363-
typing.Union[BookmarkManager, BookmarkManager, None]
361+
:type bookmark_manager_: BookmarkManager | None
364362
:param kwargs: additional keyword parameters. None of these can end
365363
with a single underscore. This is to avoid collisions with the
366364
keyword configuration parameters of this method. If you need to
@@ -369,7 +367,7 @@ Closing a driver will immediately shut down all connections in the pool.
369367
``parameters_``.
370368
:type kwargs: typing.Any
371369

372-
:returns: the result of the ``result_transformer``
370+
:returns: the result of the ``result_transformer_``
373371
:rtype: T
374372

375373
.. versionadded:: 5.5

docs/source/async_api.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ Closing a driver will immediately shut down all connections in the pool.
235235
:param query_: cypher query to execute
236236
:type query_: typing.LiteralString
237237
:param parameters_: parameters to use in the query
238-
:type parameters_: typing.Optional[typing.Dict[str, typing.Any]]
238+
:type parameters_: typing.Dict[str, typing.Any] | None
239239
:param routing_:
240240
whether to route the query to a reader (follower/read replica) or
241241
a writer (leader) in the cluster. Default is to route to a writer.
242-
:type routing_: neo4j.RoutingControl
242+
:type routing_: RoutingControl
243243
:param database_:
244244
database to execute the query against.
245245

@@ -251,7 +251,7 @@ Closing a driver will immediately shut down all connections in the pool.
251251
as it will not have to resolve the default database first.
252252

253253
See also the Session config :ref:`database-ref`.
254-
:type database_: typing.Optional[str]
254+
:type database_: str | None
255255
:param impersonated_user_:
256256
Name of the user to impersonate.
257257

@@ -261,7 +261,7 @@ Closing a driver will immediately shut down all connections in the pool.
261261
permissions.
262262

263263
See also the Session config :ref:`impersonated-user-ref`.
264-
:type impersonated_user_: typing.Optional[str]
264+
:type impersonated_user_: str | None
265265
:param auth_:
266266
Authentication information to use for this query.
267267

@@ -273,8 +273,7 @@ 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_:
277-
typing.Union[typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None]
276+
:type auth_: typing.Tuple[typing.Any, typing.Any] | Auth | None
278277
:param result_transformer_:
279278
A function that gets passed the :class:`neo4j.AsyncResult` object
280279
resulting from the query and converts it to a different type. The
@@ -336,7 +335,7 @@ Closing a driver will immediately shut down all connections in the pool.
336335
)
337336

338337
:type result_transformer_:
339-
typing.Callable[[neo4j.AsyncResult], typing.Awaitable[T]]
338+
typing.Callable[[AsyncResult], typing.Awaitable[T]]
340339
:param bookmark_manager_:
341340
Specify a bookmark manager to use.
342341

@@ -346,8 +345,7 @@ Closing a driver will immediately shut down all connections in the pool.
346345
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
347346

348347
Pass :data:`None` to disable causal consistency.
349-
:type bookmark_manager_:
350-
typing.Union[AsyncBookmarkManager, BookmarkManager, None]
348+
:type bookmark_manager_: AsyncBookmarkManager | BookmarkManager | None
351349
:param kwargs: additional keyword parameters. None of these can end
352350
with a single underscore. This is to avoid collisions with the
353351
keyword configuration parameters of this method. If you need to
@@ -356,7 +354,7 @@ Closing a driver will immediately shut down all connections in the pool.
356354
``parameters_``.
357355
:type kwargs: typing.Any
358356

359-
:returns: the result of the ``result_transformer``
357+
:returns: the result of the ``result_transformer_``
360358
:rtype: T
361359

362360
.. versionadded:: 5.5

docs/source/conf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import os
1515
import sys
16+
import typing
1617

1718

1819
# If extensions (or modules to document with autodoc) are in another directory,
@@ -118,6 +119,17 @@
118119
# Don't include type hints in function signatures
119120
autodoc_typehints = "description"
120121

122+
autodoc_type_aliases = {
123+
# The code-base uses `import typing_extensions as te`.
124+
# Re-write these to use `typing` instead, as Sphinx always resolves against
125+
# the latest version of the `typing` module.
126+
# This is a work-around to make Sphinx resolve type hints correctly, even
127+
# though we're using `from __future__ import annotations`.
128+
"te": typing,
129+
# Type alias that's only defined and imported if `typing.TYPE_CHECKING`
130+
# is `True`.
131+
"_TAuth": "typing.Tuple[typing.Any, typing.Any] | Auth | None",
132+
}
121133

122134
# -- Options for HTML output ----------------------------------------------
123135

src/neo4j/_async/auth_management.py

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

1818

19-
# from __future__ import annotations
20-
# work around for https://github.com/sphinx-doc/sphinx/pull/10880
21-
# make sure TAuth is resolved in the docs, else they're pretty useless
22-
19+
from __future__ import annotations
2320

2421
import typing as t
2522
from logging import getLogger
@@ -32,11 +29,10 @@
3229
)
3330
from .._meta import preview
3431

35-
# work around for https://github.com/sphinx-doc/sphinx/pull/10880
36-
# make sure TAuth is resolved in the docs, else they're pretty useless
37-
# if t.TYPE_CHECKING:
38-
from ..api import _TAuth
39-
from ..exceptions import Neo4jError
32+
33+
if t.TYPE_CHECKING:
34+
from ..api import _TAuth
35+
from ..exceptions import Neo4jError
4036

4137

4238
log = getLogger("neo4j")

src/neo4j/_async/driver.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ def driver(
122122
uri: str,
123123
*,
124124
auth: t.Union[
125-
# work around https://github.com/sphinx-doc/sphinx/pull/10880
126-
# make sure TAuth is resolved in the docs
127-
# TAuth,
128-
t.Union[t.Tuple[t.Any, t.Any], Auth, None],
125+
_TAuth,
129126
AsyncAuthManager,
130127
] = ...,
131128
max_connection_lifetime: float = ...,
@@ -172,10 +169,7 @@ def driver(
172169
def driver(
173170
cls, uri: str, *,
174171
auth: t.Union[
175-
# work around https://github.com/sphinx-doc/sphinx/pull/10880
176-
# make sure TAuth is resolved in the docs
177-
# TAuth,
178-
t.Union[t.Tuple[t.Any, t.Any], Auth, None],
172+
_TAuth,
179173
AsyncAuthManager,
180174
] = None,
181175
**config
@@ -716,7 +710,7 @@ async def example(driver: neo4j.AsyncDriver) -> int:
716710
:param routing_:
717711
whether to route the query to a reader (follower/read replica) or
718712
a writer (leader) in the cluster. Default is to route to a writer.
719-
:type routing_: neo4j.RoutingControl
713+
:type routing_: RoutingControl
720714
:param database_:
721715
database to execute the query against.
722716
@@ -750,9 +744,7 @@ async def example(driver: neo4j.AsyncDriver) -> int:
750744
https://github.com/neo4j/neo4j-python-driver/wiki/preview-features
751745
752746
See also the Session config :ref:`session-auth-ref`.
753-
:type auth_: typing.Union[
754-
typing.Tuple[typing.Any, typing.Any], neo4j.Auth, None
755-
]
747+
:type auth_: typing.Tuple[typing.Any, typing.Any] | Auth | None
756748
:param result_transformer_:
757749
A function that gets passed the :class:`neo4j.AsyncResult` object
758750
resulting from the query and converts it to a different type. The
@@ -814,7 +806,7 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
814806
)
815807
816808
:type result_transformer_:
817-
typing.Callable[[neo4j.AsyncResult], typing.Awaitable[T]]
809+
typing.Callable[[AsyncResult], typing.Awaitable[T]]
818810
:param bookmark_manager_:
819811
Specify a bookmark manager to use.
820812
@@ -824,8 +816,7 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
824816
Defaults to the driver's :attr:`.execute_query_bookmark_manager`.
825817
826818
Pass :data:`None` to disable causal consistency.
827-
:type bookmark_manager_:
828-
typing.Union[AsyncBookmarkManager, BookmarkManager, None]
819+
:type bookmark_manager_: AsyncBookmarkManager | BookmarkManager | None
829820
:param kwargs: additional keyword parameters. None of these can end
830821
with a single underscore. This is to avoid collisions with the
831822
keyword configuration parameters of this method. If you need to
@@ -834,7 +825,7 @@ async def example(driver: neo4j.AsyncDriver) -> neo4j.Record::
834825
``parameters_``.
835826
:type kwargs: typing.Any
836827
837-
:returns: the result of the ``result_transformer``
828+
:returns: the result of the ``result_transformer_``
838829
:rtype: T
839830
840831
.. versionadded:: 5.5

src/neo4j/_async/work/result.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ async def single(self, strict: bool = False) -> t.Optional[Record]:
430430
instead of returning None if there is more than one record or
431431
warning if there are more than 1 record.
432432
:const:`False` by default.
433+
:type strict: bool
433434
434435
:returns: the next :class:`neo4j.Record` or :data:`None` if none remain
435436

src/neo4j/_async/work/session.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,12 @@ async def run(
274274
For more usage details, see :meth:`.AsyncTransaction.run`.
275275
276276
:param query: cypher query
277+
:type query: typing.LiteralString | Query
277278
:param parameters: dictionary of parameters
279+
:type parameters: typing.Dict[str, typing.Any] | None
278280
:param kwargs: additional keyword parameters.
279281
These take precedence over parameters passed as ``parameters``.
282+
:type kwargs: typing.Any
280283
281284
:returns: a new :class:`neo4j.AsyncResult` object
282285
@@ -608,10 +611,15 @@ async def get_two_tx(tx):
608611
argument and does work with the transaction.
609612
``transaction_function(tx, *args, **kwargs)`` where ``tx`` is a
610613
:class:`.AsyncManagedTransaction`.
614+
:type transaction_function:
615+
typing.Callable[[AsyncManagedTransaction, P], typing.Awaitable[R]]
611616
:param args: additional arguments for the `transaction_function`
617+
:type args: P
612618
:param kwargs: key word arguments for the `transaction_function`
619+
:type kwargs: P
613620
614621
:returns: whatever the given `transaction_function` returns
622+
:rtype: R
615623
616624
:raises SessionError: if the session has been closed.
617625
@@ -640,10 +648,15 @@ async def read_transaction(
640648
argument and does work with the transaction.
641649
``transaction_function(tx, *args, **kwargs)`` where ``tx`` is a
642650
:class:`.AsyncManagedTransaction`.
651+
:type transaction_function:
652+
typing.Callable[[AsyncManagedTransaction, P], typing.Awaitable[R]]
643653
:param args: additional arguments for the `transaction_function`
654+
:type args: P
644655
:param kwargs: key word arguments for the `transaction_function`
656+
:type kwargs: P
645657
646658
:returns: a result as returned by the given unit of work
659+
:rtype: R
647660
648661
:raises SessionError: if the session has been closed.
649662
@@ -690,10 +703,15 @@ async def create_node_tx(tx, name):
690703
argument and does work with the transaction.
691704
``transaction_function(tx, *args, **kwargs)`` where ``tx`` is a
692705
:class:`.AsyncManagedTransaction`.
706+
:type transaction_function:
707+
typing.Callable[[AsyncManagedTransaction, P], typing.Awaitable[R]]
693708
:param args: additional arguments for the `transaction_function`
709+
:type args: P
694710
:param kwargs: key word arguments for the `transaction_function`
711+
:type kwargs: P
695712
696713
:returns: a result as returned by the given unit of work
714+
:rtype: R
697715
698716
:raises SessionError: if the session has been closed.
699717
@@ -722,10 +740,15 @@ async def write_transaction(
722740
argument and does work with the transaction.
723741
``transaction_function(tx, *args, **kwargs)`` where ``tx`` is a
724742
:class:`.AsyncManagedTransaction`.
743+
:type transaction_function:
744+
typing.Callable[[AsyncManagedTransaction, P], typing.Awaitable[R]]
725745
:param args: additional arguments for the `transaction_function`
746+
:type args: P
726747
:param kwargs: key word arguments for the `transaction_function`
748+
:type kwargs: P
727749
728750
:returns: a result as returned by the given unit of work
751+
:rtype: R
729752
730753
:raises SessionError: if the session has been closed.
731754

src/neo4j/_async/work/transaction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ async def run(
127127
:class:`list` properties must be homogenous.
128128
129129
:param query: cypher query
130+
:type query: typing.LiteralString
130131
:param parameters: dictionary of parameters
132+
:type parameters: typing.Dict[str, typing.Any] | None
131133
:param kwparameters: additional keyword parameters.
132134
These take precedence over parameters passed as ``parameters``.
135+
:type kwparameters: typing.Any
133136
134137
:raise TransactionError: if the transaction is already closed
135138
@@ -255,7 +258,7 @@ def _closed(self):
255258
"""Indicate whether the transaction has been closed or cancelled.
256259
257260
:returns:
258-
:const:`True` if closed or cancelled, :const:`False` otherwise.
261+
:data:`True` if closed or cancelled, :data:`False` otherwise.
259262
:rtype: bool
260263
"""
261264
return self._closed_flag

0 commit comments

Comments
 (0)