Skip to content

Commit 64112ff

Browse files
authored
Make code compatible with mypy 0.990 (#854)
* remove implicit optional parameters (PEP 484 received an update) * mute metaclass inference limitations of mypy * fix mypy rightfully complaining about empty method bodies * fixed bug in (deprecated) `session.last_bookmark` that mypy uncovered
1 parent b42e7f1 commit 64112ff

21 files changed

+109
-84
lines changed

neo4j/_async/bookmark_manager.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def _bookmarks_to_set(
4545
class AsyncNeo4jBookmarkManager(AsyncBookmarkManager):
4646
def __init__(
4747
self,
48-
initial_bookmarks: t.Mapping[str, t.Union[Bookmarks,
49-
t.Iterable[str]]] = None,
50-
bookmarks_supplier: T_BmSupplier = None,
51-
bookmarks_consumer: T_BmConsumer = None
48+
initial_bookmarks: t.Optional[t.Mapping[str, t.Union[Bookmarks,
49+
t.Iterable[str]]]] = None,
50+
bookmarks_supplier: t.Optional[T_BmSupplier] = None,
51+
bookmarks_consumer: t.Optional[T_BmConsumer] = None
5252
) -> None:
5353
super().__init__()
5454
self._bookmarks_supplier = bookmarks_supplier

neo4j/_async/driver.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import ssl
2828

29+
30+
2931
from .._async_compat.util import AsyncUtil
3032
from .._conf import (
3133
Config,
@@ -218,10 +220,10 @@ def driver(cls, uri, *, auth=None, **config) -> AsyncDriver:
218220
)
219221
def bookmark_manager(
220222
cls,
221-
initial_bookmarks: t.Mapping[str, t.Union[Bookmarks,
222-
t.Iterable[str]]] = None,
223-
bookmarks_supplier: _T_BmSupplier = None,
224-
bookmarks_consumer: _T_BmConsumer = None
223+
initial_bookmarks: t.Optional[t.Mapping[str, t.Union[Bookmarks,
224+
t.Iterable[str]]]] = None,
225+
bookmarks_supplier: t.Optional[_T_BmSupplier] = None,
226+
bookmarks_consumer: t.Optional[_T_BmConsumer] = None
225227
) -> AsyncBookmarkManager:
226228
"""Create a :class:`.AsyncBookmarkManager` with default implementation.
227229

neo4j/_async/work/result.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
if t.TYPE_CHECKING:
2727
import typing_extensions as te
2828

29+
30+
2931
from ..._async_compat.util import AsyncUtil
3032
from ..._codec.hydration import BrokenHydrationObject
3133
from ..._data import (
@@ -517,7 +519,7 @@ async def graph(self) -> Graph:
517519
return self._hydration_scope.get_graph()
518520

519521
async def value(
520-
self, key: _T_ResultKey = 0, default: object = None
522+
self, key: _T_ResultKey = 0, default: t.Optional[object] = None
521523
) -> t.List[t.Any]:
522524
"""Helper function that return the remainder of the result as a list of values.
523525

neo4j/_async/work/session.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
_R = t.TypeVar("_R")
3434
_P = te.ParamSpec("_P")
3535

36+
37+
3638
from ..._async_compat import async_sleep
3739
from ..._async_compat.util import AsyncUtil
3840
from ..._conf import SessionConfig
@@ -236,7 +238,7 @@ def cancel(self) -> None:
236238
async def run(
237239
self,
238240
query: t.Union[str, Query],
239-
parameters: t.Dict[str, t.Any] = None,
241+
parameters: t.Optional[t.Dict[str, t.Any]] = None,
240242
**kwargs: t.Any
241243
) -> AsyncResult:
242244
"""Run a Cypher query within an auto-commit transaction.
@@ -321,7 +323,7 @@ async def last_bookmark(self) -> t.Optional[str]:
321323
if self._auto_result:
322324
await self._auto_result.consume()
323325

324-
if self._transaction and self._transaction._closed:
326+
if self._transaction and self._transaction._closed():
325327
await self._update_bookmark(self._transaction._database,
326328
self._transaction._bookmark)
327329
self._transaction = None
@@ -405,8 +407,8 @@ async def _open_transaction(
405407

406408
async def begin_transaction(
407409
self,
408-
metadata: t.Dict[str, t.Any] = None,
409-
timeout: float = None
410+
metadata: t.Optional[t.Dict[str, t.Any]] = None,
411+
timeout: t.Optional[float] = None
410412
) -> AsyncTransaction:
411413
""" Begin a new unmanaged transaction. Creates a new :class:`.AsyncTransaction` within this session.
412414
At most one transaction may exist in a session at any point in time.

neo4j/_async/work/transaction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def _consume_results(self):
9696
async def run(
9797
self,
9898
query: str,
99-
parameters: t.Dict[str, t.Any] = None,
99+
parameters: t.Optional[t.Dict[str, t.Any]] = None,
100100
**kwparameters: t.Any
101101
) -> AsyncResult:
102102
""" Run a Cypher query within the context of this transaction.

neo4j/_data.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def __getslice__(self, start, stop):
140140
values = tuple(self)[key]
141141
return self.__class__(zip(keys, values))
142142

143-
def get(self, key: str, default: object = None) -> t.Any:
143+
def get(self, key: str, default: t.Optional[object] = None) -> t.Any:
144144
""" Obtain a value from the record by key, returning a default
145145
value if the key does not exist.
146146
@@ -177,7 +177,9 @@ def index(self, key: _T_K) -> int: # type: ignore[override]
177177
else:
178178
raise TypeError(key)
179179

180-
def value(self, key: _T_K = 0, default: object = None) -> t.Any:
180+
def value(
181+
self, key: _T_K = 0, default: t.Optional[object] = None
182+
) -> t.Any:
181183
""" Obtain a single value from the record by index or key. If no
182184
index or key is specified, the first value is returned. If the
183185
specified item does not exist, the default value is returned.

neo4j/_spatial/__init__.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,15 @@ class Point(t.Tuple[float, ...]):
4949
#: be interpreted in.
5050
srid: t.Optional[int]
5151

52-
@property
53-
def x(self) -> float:
54-
...
52+
if t.TYPE_CHECKING:
53+
@property
54+
def x(self) -> float: ...
5555

56-
@property
57-
def y(self) -> float:
58-
...
56+
@property
57+
def y(self) -> float: ...
5958

60-
@property
61-
def z(self) -> float:
62-
...
59+
@property
60+
def z(self) -> float: ...
6361

6462
def __new__(cls, iterable: t.Iterable[float]) -> Point:
6563
return tuple.__new__(cls, map(float, iterable))

neo4j/_sync/bookmark_manager.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ def _bookmarks_to_set(
4545
class Neo4jBookmarkManager(BookmarkManager):
4646
def __init__(
4747
self,
48-
initial_bookmarks: t.Mapping[str, t.Union[Bookmarks,
49-
t.Iterable[str]]] = None,
50-
bookmarks_supplier: T_BmSupplier = None,
51-
bookmarks_consumer: T_BmConsumer = None
48+
initial_bookmarks: t.Optional[t.Mapping[str, t.Union[Bookmarks,
49+
t.Iterable[str]]]] = None,
50+
bookmarks_supplier: t.Optional[T_BmSupplier] = None,
51+
bookmarks_consumer: t.Optional[T_BmConsumer] = None
5252
) -> None:
5353
super().__init__()
5454
self._bookmarks_supplier = bookmarks_supplier

neo4j/_sync/driver.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ def driver(cls, uri, *, auth=None, **config) -> Driver:
217217
)
218218
def bookmark_manager(
219219
cls,
220-
initial_bookmarks: t.Mapping[str, t.Union[Bookmarks,
221-
t.Iterable[str]]] = None,
222-
bookmarks_supplier: _T_BmSupplier = None,
223-
bookmarks_consumer: _T_BmConsumer = None
220+
initial_bookmarks: t.Optional[t.Mapping[str, t.Union[Bookmarks,
221+
t.Iterable[str]]]] = None,
222+
bookmarks_supplier: t.Optional[_T_BmSupplier] = None,
223+
bookmarks_consumer: t.Optional[_T_BmConsumer] = None
224224
) -> BookmarkManager:
225225
"""Create a :class:`.BookmarkManager` with default implementation.
226226

neo4j/_sync/work/result.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
if t.TYPE_CHECKING:
2727
import typing_extensions as te
2828

29+
30+
2931
from ..._async_compat.util import Util
3032
from ..._codec.hydration import BrokenHydrationObject
3133
from ..._data import (
@@ -517,7 +519,7 @@ def graph(self) -> Graph:
517519
return self._hydration_scope.get_graph()
518520

519521
def value(
520-
self, key: _T_ResultKey = 0, default: object = None
522+
self, key: _T_ResultKey = 0, default: t.Optional[object] = None
521523
) -> t.List[t.Any]:
522524
"""Helper function that return the remainder of the result as a list of values.
523525

neo4j/_sync/work/session.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
_R = t.TypeVar("_R")
3434
_P = te.ParamSpec("_P")
3535

36+
37+
3638
from ..._async_compat import sleep
3739
from ..._async_compat.util import Util
3840
from ..._conf import SessionConfig
@@ -236,7 +238,7 @@ def cancel(self) -> None:
236238
def run(
237239
self,
238240
query: t.Union[str, Query],
239-
parameters: t.Dict[str, t.Any] = None,
241+
parameters: t.Optional[t.Dict[str, t.Any]] = None,
240242
**kwargs: t.Any
241243
) -> Result:
242244
"""Run a Cypher query within an auto-commit transaction.
@@ -321,7 +323,7 @@ def last_bookmark(self) -> t.Optional[str]:
321323
if self._auto_result:
322324
self._auto_result.consume()
323325

324-
if self._transaction and self._transaction._closed:
326+
if self._transaction and self._transaction._closed():
325327
self._update_bookmark(self._transaction._database,
326328
self._transaction._bookmark)
327329
self._transaction = None
@@ -405,8 +407,8 @@ def _open_transaction(
405407

406408
def begin_transaction(
407409
self,
408-
metadata: t.Dict[str, t.Any] = None,
409-
timeout: float = None
410+
metadata: t.Optional[t.Dict[str, t.Any]] = None,
411+
timeout: t.Optional[float] = None
410412
) -> Transaction:
411413
""" Begin a new unmanaged transaction. Creates a new :class:`.Transaction` within this session.
412414
At most one transaction may exist in a session at any point in time.

neo4j/_sync/work/transaction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def _consume_results(self):
9696
def run(
9797
self,
9898
query: str,
99-
parameters: t.Dict[str, t.Any] = None,
99+
parameters: t.Optional[t.Dict[str, t.Any]] = None,
100100
**kwparameters: t.Any
101101
) -> Result:
102102
""" Run a Cypher query within the context of this transaction.

neo4j/addressing.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ class _WithPeerName(te.Protocol):
4444
def getpeername(self) -> tuple: ...
4545

4646

47-
assert type(tuple) is type
48-
49-
50-
class _AddressMeta(type):
47+
class _AddressMeta(type(tuple)): # type: ignore[misc]
5148

5249
def __init__(cls, *args, **kwargs):
5350
super().__init__(*args, **kwargs)
@@ -95,8 +92,8 @@ def from_socket(
9592
def parse(
9693
cls: t.Type[_T_Address],
9794
s: str,
98-
default_host: str = None,
99-
default_port: int = None
95+
default_host: t.Optional[str] = None,
96+
default_port: t.Optional[int] = None
10097
) -> _T_Address:
10198
if not isinstance(s, str):
10299
raise TypeError("Address.parse requires a string argument")
@@ -125,8 +122,8 @@ def parse(
125122
def parse_list(
126123
cls: t.Type[_T_Address],
127124
*s: str,
128-
default_host: str = None,
129-
default_port: int = None
125+
default_host: t.Optional[str] = None,
126+
default_port: t.Optional[int] = None
130127
) -> t.List[_T_Address]:
131128
""" Parse a string containing one or more socket addresses, each
132129
separated by whitespace.

neo4j/api.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import typing_extensions as te
3333
from .addressing import Address
3434

35+
36+
3537
from ._meta import deprecated
3638
from .exceptions import ConfigurationError
3739

@@ -90,7 +92,7 @@ def __init__(
9092
scheme: t.Optional[str],
9193
principal: t.Optional[str],
9294
credentials: t.Optional[str],
93-
realm: str = None,
95+
realm: t.Optional[str] = None,
9496
**parameters: t.Any
9597
) -> None:
9698
self.scheme = scheme
@@ -110,7 +112,9 @@ def __init__(
110112
AuthToken = Auth
111113

112114

113-
def basic_auth(user: str, password: str, realm: str = None) -> Auth:
115+
def basic_auth(
116+
user: str, password: str, realm: t.Optional[str] = None
117+
) -> Auth:
114118
"""Generate a basic auth token for a given user and password.
115119
116120
This will set the scheme to "basic" for the auth token.

neo4j/debug.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ def __exit__(self, exc_type, exc_val, exc_tb):
113113
"""Disable logging for all loggers."""
114114
self.stop()
115115

116-
def watch(self, level: int = None, out: t.TextIO = None):
116+
def watch(
117+
self, level: t.Optional[int] = None, out: t.Optional[t.TextIO] = None
118+
) -> None:
117119
"""Enable logging for all loggers.
118120
119121
:param level: Minimum log level to show.

neo4j/exceptions.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
_T_Session = t.Union["AsyncSession", "Session"]
101101

102102

103+
104+
103105
from ._meta import deprecated
104106

105107

@@ -148,7 +150,10 @@ class Neo4jError(Exception):
148150

149151
@classmethod
150152
def hydrate(
151-
cls, message: str = None, code: str = None, **metadata: t.Any
153+
cls,
154+
message: t.Optional[str] = None,
155+
code: t.Optional[str] = None,
156+
**metadata: t.Any
152157
) -> Neo4jError:
153158
message = message or "An unknown error occurred"
154159
code = code or "Neo.DatabaseError.General.UnknownError"

neo4j/graph/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def element_id(self) -> str:
166166
"""
167167
return self._element_id
168168

169-
def get(self, name: str, default: object = None) -> t.Any:
169+
def get(self, name: str, default: t.Optional[object] = None) -> t.Any:
170170
""" Get a property value by name, optionally with a default.
171171
"""
172172
return self._properties.get(name, default)
@@ -225,8 +225,8 @@ def __init__(
225225
graph: Graph,
226226
element_id: str,
227227
id_: int,
228-
n_labels: t.Iterable[str] = None,
229-
properties: t.Dict[str, t.Any] = None
228+
n_labels: t.Optional[t.Iterable[str]] = None,
229+
properties: t.Optional[t.Dict[str, t.Any]] = None
230230
) -> None:
231231
Entity.__init__(self, graph, element_id, id_, properties)
232232
self._labels = frozenset(n_labels or ())

0 commit comments

Comments
 (0)