Skip to content

Commit 33c337c

Browse files
authored
Refactor type annotations (#1197)
* Move all imports for typing into a central file. This will make upgrading easier when bumping the minimum Python version in the future. It also enables a more consistent code style. * Upgrade typing imports to be in line with the new Python 3.10 base-line. E.g., import `collections.abc.Mapping` instead of `typing.Mapping`. * Moved some imports out of `if TYPE_CHECKING` blocks to help Sphinx produce useful type annotations in the docs again.
1 parent 67f4508 commit 33c337c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+483
-468
lines changed

benchkit/app.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from contextlib import contextmanager
2121
from multiprocessing import Semaphore
2222

23-
import typing_extensions as te
2423
from sanic import Sanic
2524
from sanic.exceptions import (
2625
BadRequest,
@@ -43,7 +42,7 @@
4342
from .workloads import Workload
4443

4544

46-
T_App: te.TypeAlias = "Sanic[Config, BenchKitContext]"
45+
T_App: t.TypeAlias = "Sanic[Config, BenchKitContext]"
4746

4847

4948
def create_app() -> T_App:

benchkit/workloads.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919
import asyncio
2020
import enum
2121
import typing as t
22+
from collections.abc import (
23+
Iterable,
24+
Iterator,
25+
Mapping,
26+
)
2227
from dataclasses import dataclass
2328

24-
import typing_extensions as te
25-
2629

2730
if t.TYPE_CHECKING:
28-
from collections.abc import Iterator
31+
import typing_extensions as te
2932

3033
from neo4j import (
3134
AsyncDriver,
@@ -40,7 +43,7 @@
4043
]
4144

4245

43-
class Workloads(t.Mapping):
46+
class Workloads(Mapping):
4447
def __init__(self) -> None:
4548
self._workloads: dict[str, Workload] = {}
4649
self._current_id: int = 0
@@ -517,7 +520,7 @@ class _WorkloadQuery:
517520

518521
@classmethod
519522
def parse_multiple(cls, queries: t.Any) -> list[te.Self]:
520-
if not isinstance(queries, t.Iterable):
523+
if not isinstance(queries, Iterable):
521524
raise TypeError("Workload queries must be a list")
522525
return [cls.parse(query) for query in queries]
523526

@@ -544,7 +547,7 @@ def parse(cls, query: t.Any) -> te.Self:
544547
@dataclass
545548
class _WorkloadConfig:
546549
database: str | None
547-
routing: te.Literal["r", "w"]
550+
routing: t.Literal["r", "w"]
548551

549552
@classmethod
550553
def parse(cls, data: t.Any) -> te.Self:
@@ -554,7 +557,7 @@ def parse(cls, data: t.Any) -> te.Self:
554557
if not isinstance(database, str):
555558
raise TypeError("Workload database must be a string")
556559

557-
routing: te.Literal["r", "w"] = "w"
560+
routing: t.Literal["r", "w"] = "w"
558561
if "routing" in data:
559562
raw_routing = data["routing"]
560563
if not isinstance(routing, str):

docs/source/conf.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import os
1919
import sys
20-
import typing
2120

2221

2322
# If extensions (or modules to document with autodoc) are in another directory,
@@ -123,17 +122,7 @@
123122
# Don't include type hints in function signatures
124123
autodoc_typehints = "description"
125124

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

138127
# -- Options for HTML output ----------------------------------------------
139128
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ extend-exclude = [
139139

140140
[tool.ruff.lint]
141141
preview = true # to get CPY lints
142+
typing-modules = ["neo4j._typing"]
142143
extend-ignore = [
143144
"RUF002", # allow ’ (RIGHT SINGLE QUOTATION MARK) to be used as an apostrophe (e.g. "it’s")
144145

src/neo4j/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
# limitations under the License.
1515

1616

17-
import typing as _t
18-
17+
from . import _typing as _t
1918
from ._api import ( # noqa: F401 dynamic attributes
2019
NotificationCategory,
2120
NotificationDisabledCategory,

src/neo4j/_addressing.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from __future__ import annotations
1818

19-
import typing as t
2019
from contextlib import suppress as _suppress
2120
from socket import (
2221
AddressFamily,
@@ -25,17 +24,15 @@
2524
getservbyname,
2625
)
2726

28-
29-
if t.TYPE_CHECKING:
30-
import typing_extensions as te
27+
from . import _typing as t
3128

3229

3330
_T = t.TypeVar("_T")
3431

3532

3633
if t.TYPE_CHECKING:
3734

38-
class _WithPeerName(te.Protocol):
35+
class _WithPeerName(t.Protocol):
3936
def getpeername(self) -> tuple: ...
4037

4138

src/neo4j/_api.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@
1616

1717
from __future__ import annotations
1818

19-
import typing as t
2019
from enum import Enum
2120
from urllib.parse import (
2221
parse_qs,
2322
urlparse,
2423
)
2524

26-
from . import api
25+
from . import (
26+
_typing as t,
27+
api,
28+
)
2729
from .exceptions import ConfigurationError
2830

2931

30-
if t.TYPE_CHECKING:
31-
import typing_extensions as te
32-
33-
3432
__all__ = [
3533
"DRIVER_BOLT",
3634
"DRIVER_NEO4J",
@@ -51,14 +49,14 @@
5149
]
5250

5351

54-
DRIVER_BOLT: te.Final[str] = "DRIVER_BOLT"
55-
DRIVER_NEO4J: te.Final[str] = "DRIVER_NEO4J"
52+
DRIVER_BOLT: t.Final[str] = "DRIVER_BOLT"
53+
DRIVER_NEO4J: t.Final[str] = "DRIVER_NEO4J"
5654

57-
SECURITY_TYPE_NOT_SECURE: te.Final[str] = "SECURITY_TYPE_NOT_SECURE"
58-
SECURITY_TYPE_SELF_SIGNED_CERTIFICATE: te.Final[str] = (
55+
SECURITY_TYPE_NOT_SECURE: t.Final[str] = "SECURITY_TYPE_NOT_SECURE"
56+
SECURITY_TYPE_SELF_SIGNED_CERTIFICATE: t.Final[str] = (
5957
"SECURITY_TYPE_SELF_SIGNED_CERTIFICATE"
6058
)
61-
SECURITY_TYPE_SECURE: te.Final[str] = "SECURITY_TYPE_SECURE"
59+
SECURITY_TYPE_SECURE: t.Final[str] = "SECURITY_TYPE_SECURE"
6260

6361

6462
def parse_neo4j_uri(uri):
@@ -184,7 +182,7 @@ class NotificationMinimumSeverity(str, Enum):
184182
if t.TYPE_CHECKING:
185183
T_NotificationMinimumSeverity = (
186184
NotificationMinimumSeverity
187-
| te.Literal[
185+
| t.Literal[
188186
"OFF",
189187
"WARNING",
190188
"INFORMATION",
@@ -331,7 +329,7 @@ class NotificationDisabledClassification(str, Enum):
331329
T_NotificationDisabledCategory = (
332330
NotificationDisabledCategory
333331
| NotificationDisabledClassification
334-
| te.Literal[
332+
| t.Literal[
335333
"HINT",
336334
"UNRECOGNIZED",
337335
"UNSUPPORTED",
@@ -458,5 +456,5 @@ class TelemetryAPI(int, Enum):
458456

459457

460458
if t.TYPE_CHECKING:
461-
T_RoutingControl = RoutingControl | te.Literal["r", "w"]
459+
T_RoutingControl = RoutingControl | t.Literal["r", "w"]
462460
__all__.append("T_RoutingControl")

src/neo4j/_async/_debug/_concurrency_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import inspect
2020
import traceback
21-
import typing as t
2221
from copy import deepcopy
2322
from functools import wraps
2423

24+
from ... import _typing as t
2525
from ..._async_compat.concurrency import (
2626
AsyncLock,
2727
AsyncRLock,

src/neo4j/_async/auth_management.py

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

1919
import abc
20-
import typing as t
2120
from logging import getLogger
2221

22+
from .. import _typing as t
2323
from .._async_compat.concurrency import (
2424
AsyncCooperativeLock,
2525
AsyncLock,
@@ -32,9 +32,11 @@
3232
ExpiringAuth,
3333
)
3434

35+
# ignore TCH001 to make sphinx not completely drop the ball
36+
from ..api import _TAuth # noqa: TCH001
37+
3538

3639
if t.TYPE_CHECKING:
37-
from ..api import _TAuth
3840
from ..exceptions import Neo4jError
3941

4042

src/neo4j/_async/bookmark_manager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
from __future__ import annotations
1818

19-
import typing as t
20-
19+
from .. import _typing as t
2120
from .._async_compat.concurrency import AsyncCooperativeLock
2221
from .._async_compat.util import AsyncUtil
2322
from ..api import (

src/neo4j/_async/config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
from __future__ import annotations
1818

19-
import typing as t
20-
19+
from .. import _typing as t
2120
from .._async_compat.concurrency import AsyncLock
2221
from .._conf import (
2322
Config,

src/neo4j/_async/driver.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,9 @@
1717
from __future__ import annotations
1818

1919
import asyncio
20-
import typing as t
2120
from types import NoneType
2221

23-
24-
if t.TYPE_CHECKING:
25-
import ssl
26-
import typing_extensions as te
27-
28-
from .._api import (
29-
T_NotificationDisabledCategory,
30-
T_NotificationMinimumSeverity,
31-
)
32-
22+
from .. import _typing as t
3323
from .._addressing import Address
3424
from .._api import (
3525
DRIVER_BOLT,
@@ -62,6 +52,7 @@
6252
unit_of_work,
6353
)
6454
from ..api import (
55+
_TAuth,
6556
AsyncBookmarkManager,
6657
Auth,
6758
BookmarkManager,
@@ -104,10 +95,11 @@
10495
import ssl
10596
from enum import Enum
10697

107-
import typing_extensions as te
108-
109-
from .._api import T_RoutingControl
110-
from ..api import _TAuth
98+
from .._api import (
99+
T_NotificationDisabledCategory,
100+
T_NotificationMinimumSeverity,
101+
T_RoutingControl,
102+
)
111103

112104
class _DefaultEnum(Enum):
113105
default = "default"
@@ -635,7 +627,7 @@ async def close(self) -> None:
635627
@t.overload
636628
async def execute_query(
637629
self,
638-
query_: te.LiteralString | Query,
630+
query_: t.LiteralString | Query,
639631
parameters_: dict[str, t.Any] | None = None,
640632
routing_: T_RoutingControl = RoutingControl.WRITE,
641633
database_: str | None = None,
@@ -653,7 +645,7 @@ async def execute_query(
653645
@t.overload
654646
async def execute_query(
655647
self,
656-
query_: te.LiteralString | Query,
648+
query_: t.LiteralString | Query,
657649
parameters_: dict[str, t.Any] | None = None,
658650
routing_: T_RoutingControl = RoutingControl.WRITE,
659651
database_: str | None = None,
@@ -668,7 +660,7 @@ async def execute_query(
668660

669661
async def execute_query(
670662
self,
671-
query_: te.LiteralString | Query,
663+
query_: t.LiteralString | Query,
672664
parameters_: dict[str, t.Any] | None = None,
673665
routing_: T_RoutingControl = RoutingControl.WRITE,
674666
database_: str | None = None,
@@ -677,7 +669,7 @@ async def execute_query(
677669
AsyncBookmarkManager
678670
| BookmarkManager
679671
| None
680-
| te.Literal[_DefaultEnum.default]
672+
| t.Literal[_DefaultEnum.default]
681673
) = _default,
682674
auth_: _TAuth = None,
683675
result_transformer_: t.Callable[
@@ -1299,7 +1291,7 @@ async def _get_server_info(self, session_config) -> ServerInfo:
12991291

13001292
async def _work(
13011293
tx: AsyncManagedTransaction,
1302-
query: te.LiteralString,
1294+
query: t.LiteralString,
13031295
parameters: dict[str, t.Any],
13041296
transformer: t.Callable[[AsyncResult], t.Awaitable[_T]],
13051297
) -> _T:

src/neo4j/_async/home_db_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
from __future__ import annotations
2020

2121
import math
22-
import typing as t
2322
from time import monotonic
2423

24+
from .. import _typing as t
2525
from .._async_compat.concurrency import AsyncCooperativeLock
2626

2727

0 commit comments

Comments
 (0)