Skip to content

Commit a78fa77

Browse files
committed
Set mypy to a resonably strict mode and fix all errors detected by this
Bonus: F821 is now fixed by pinning a newer pyflakes version.
1 parent 80c6030 commit a78fa77

25 files changed

+421
-275
lines changed

datastore/adapter/_support.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
# * https://github.com/python/mypy/issues/7790 (Associated types)
1212
# * https://github.com/python/mypy/issues/7791 (Types of generic classes)
1313
DS = typing.TypeVar("DS", datastore.abc.BinaryDatastore,
14-
datastore.abc.ObjectDatastore[T_co]) # type: ignore[valid-type] # noqa: F821
14+
datastore.abc.ObjectDatastore[T_co]) # type: ignore[valid-type]
1515
DA = typing.TypeVar("DA", datastore.abc.BinaryAdapter,
16-
datastore.abc.ObjectAdapter[T_co, T_co]) # type: ignore[valid-type] # noqa: F821, E501
16+
datastore.abc.ObjectAdapter[T_co, T_co]) # type: ignore[valid-type]
1717
MD = typing.TypeVar("MD", datastore.util.StreamMetadata,
18-
datastore.util.ChannelMetadata) # type: ignore[valid-type] # noqa: F821
18+
datastore.util.ChannelMetadata)
1919
RT = typing.TypeVar("RT", datastore.abc.ReceiveStream,
20-
datastore.abc.ReceiveChannel[T_co]) # type: ignore[valid-type] # noqa: F821
21-
RV = typing.TypeVar("RV", bytes, typing.List[T_co]) # type: ignore[valid-type] # noqa: F821
20+
datastore.abc.ReceiveChannel[T_co]) # type: ignore[valid-type]
21+
RV = typing.TypeVar("RV", bytes, typing.List[T_co]) # type: ignore[valid-type]
2222

2323

2424
# Workaround for https://github.com/python/mypy/issues/708

datastore/adapter/directory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,4 @@ async def query(self, query: datastore.Query) -> datastore.Cursor:
188188
"""Returns objects matching criteria expressed in `query`.
189189
DirectoryTreeDatastore uses directory entries.
190190
"""
191-
return query(super().directory_read(query.key))
191+
return query(super().directory_read(query.key)) # type: ignore[no-any-return]

datastore/adapter/keytransform.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,48 @@ class _Adapter(typing.Generic[DS, MD, RT, RV]):
5454
key_transform_fn: _support.FunctionProperty[KEY_TRANSFORM_T]
5555

5656

57-
def __init__(self, *args, key_transform: KEY_TRANSFORM_T = (lambda k: k), **kwargs):
57+
def __init__(self, *args: typing.Any, key_transform: KEY_TRANSFORM_T = (lambda k: k),
58+
**kwargs: typing.Any):
5859
"""Initializes KeyTransformDatastore with `keytransform` function."""
5960
self.key_transform_fn = key_transform
60-
super().__init__(*args, **kwargs) # type: ignore[call-arg] # noqa: F821
61+
super().__init__(*args, **kwargs) # type: ignore[call-arg]
6162

6263

6364
async def get(self, key: datastore.Key) -> RT:
6465
"""Returns the object named by keytransform(key)."""
65-
return await super().get(self.key_transform_fn(key)) # type: ignore[misc] # noqa: F821
66+
return await super().get(self.key_transform_fn(key)) # type: ignore[misc, no-any-return]
6667

6768

6869
async def get_all(self, key: datastore.Key) -> RV:
6970
"""Returns the object named by keytransform(key)."""
70-
return await super().get_all(self.key_transform_fn(key)) # type: ignore[misc] # noqa: F821
71+
return await super().get_all(self.key_transform_fn(key)) # type: ignore[misc, no-any-return]
7172

7273

7374
async def _put(self, key: datastore.Key, value: RT) -> None:
7475
"""Stores the object names by keytransform(key)."""
75-
await super()._put(self.key_transform_fn(key), value) # type: ignore[misc] # noqa: F821
76+
await super()._put(self.key_transform_fn(key), value) # type: ignore[misc, no-any-return]
7677

7778

7879
async def delete(self, key: datastore.Key) -> None:
7980
"""Removes the object named by keytransform(key)."""
80-
await super().delete(self.key_transform_fn(key)) # type: ignore[misc] # noqa: F821
81+
await super().delete(self.key_transform_fn(key)) # type: ignore[misc]
8182

8283

8384
async def contains(self, key: datastore.Key) -> bool:
8485
"""Returns whether the object named by key is in this datastore."""
85-
return await super().contains(self.key_transform_fn(key)) # type: ignore[misc] # noqa: F821
86+
return await super().contains(self.key_transform_fn(key)) # type: ignore[misc, no-any-return]
8687

8788

8889
async def stat(self, key: datastore.Key) -> MD:
8990
"""Returns the metadata of the object named by keytransform(key)."""
90-
return await super().stat(self.key_transform_fn(key)) # type: ignore[misc] # noqa: F821
91+
return await super().stat(self.key_transform_fn(key)) # type: ignore[misc, no-any-return]
9192

9293

9394
async def query(self, query: datastore.Query) -> datastore.Cursor:
9495
"""Returns a sequence of objects matching criteria expressed in `query`"""
9596
query = query.copy()
9697
query.key = self.key_transform_fn(query.key)
97-
return await super().query(query) # type: ignore[misc] # noqa: F821
98+
return await super().query(query) # type: ignore[misc, no-any-return]
9899

99100

100101
class BinaryAdapter(
@@ -147,7 +148,7 @@ class _LowercaseKeyAdapter(_Adapter[DS, MD, RT, RV], typing.Generic[DS, MD, RT,
147148
"""
148149
__slots__ = ()
149150

150-
def __init__(self, *args, **kwargs):
151+
def __init__(self, *args: typing.Any, **kwargs: typing.Any):
151152
"""Initializes KeyTransformDatastore with `key_transform` function."""
152153
super().__init__(*args, key_transform=self.lowercase_key, **kwargs)
153154

@@ -209,7 +210,8 @@ class _NamespaceAdapter(_Adapter[DS, MD, RT, RV], typing.Generic[DS, MD, RT, RV]
209210

210211
namespace: datastore.Key
211212

212-
def __init__(self, namespace: typing.Union[str, datastore.Key], *args, **kwargs):
213+
def __init__(self, namespace: typing.Union[str, datastore.Key],
214+
*args: typing.Any, **kwargs: typing.Any):
213215
"""Initializes NamespaceDatastore with `key` namespace."""
214216
self.namespace = datastore.Key(namespace)
215217
super().__init__(*args, key_transform = self.namespace_key, **kwargs)
@@ -280,11 +282,11 @@ def _default_keyfn(key: datastore.Key) -> str:
280282
return key.name
281283

282284

283-
def __init__(self, *args,
285+
def __init__(self, *args: typing.Any,
284286
depth: int = None,
285287
length: int = None,
286288
key_fn: typing.Callable[[datastore.Key], str] = None,
287-
**kwargs):
289+
**kwargs: typing.Any):
288290
"""Initializes KeyTransformDatastore with keytransform function.
289291
290292
Arguments

datastore/adapter/logging.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ class _Adapter(typing.Generic[DS, MD, RT, RV]):
2323
logger: logging.Logger
2424

2525

26-
def __init__(self, *args, logger: logging.Logger = ROOT_LOGGER, **kwargs):
26+
def __init__(self, *args: typing.Any, logger: logging.Logger = ROOT_LOGGER,
27+
**kwargs: typing.Any):
2728
self.logger = logger
28-
super().__init__(*args, **kwargs) # type: ignore[call-arg] # noqa: F821
29+
super().__init__(*args, **kwargs) # type: ignore[call-arg]
2930

3031

3132
async def get(self, key: datastore.Key) -> RT:
3233
"""Return the object named by key or None if it does not exist.
3334
LoggingDatastore logs the access.
3435
"""
3536
self.logger.info('%s: get %s', self, key)
36-
value = await super().get(key) # type: ignore[misc] # noqa: F821
37+
value: RT = await super().get(key) # type: ignore[misc]
3738
self.logger.debug('%s: %s', self, value)
3839
return value
3940

@@ -43,7 +44,7 @@ async def get_all(self, key: datastore.Key) -> RV:
4344
LoggingDatastore logs the access.
4445
"""
4546
self.logger.info('%s: get %s', self, key)
46-
value = await super().get_all(key) # type: ignore[misc] # noqa: F821
47+
value: RV = await super().get_all(key) # type: ignore[misc]
4748
self.logger.debug('%s: %s', self, value)
4849
return value
4950

@@ -54,31 +55,31 @@ async def _put(self, key: datastore.Key, value: RT) -> None:
5455
"""
5556
self.logger.info('%s: put %s', self, key)
5657
self.logger.debug('%s: %s', self, value)
57-
await super()._put(key, value) # type: ignore[misc] # noqa: F821
58+
await super()._put(key, value) # type: ignore[misc]
5859

5960

6061
async def delete(self, key: datastore.Key) -> None:
6162
"""Removes the object named by `key`.
6263
LoggingDatastore logs the access.
6364
"""
6465
self.logger.info('%s: delete %s', self, key)
65-
await super().delete(key) # type: ignore[misc] # noqa: F821
66+
await super().delete(key) # type: ignore[misc]
6667

6768

6869
async def contains(self, key: datastore.Key) -> bool:
6970
"""Returns whether the object named by `key` exists.
7071
LoggingDatastore logs the access.
7172
"""
7273
self.logger.info('%s: contains %s', self, key)
73-
return await super().contains(key) # type: ignore[misc] # noqa: F821
74+
return await super().contains(key) # type: ignore[misc, no-any-return]
7475

7576

7677
async def stat(self, key: datastore.Key) -> MD:
7778
"""Returns the metadata of the object named by `key`.
7879
LoggingDatastore logs the access.
7980
"""
8081
self.logger.info('%s: stat %s', self, key)
81-
metadata = await super().stat(key) # type: ignore[misc] # noqa: F821
82+
metadata: MD = await super().stat(key) # type: ignore[misc]
8283
self.logger.debug('%s: %s', self, metadata)
8384
return metadata
8485

@@ -88,7 +89,7 @@ async def query(self, query: datastore.Query) -> datastore.Cursor:
8889
LoggingDatastore logs the access.
8990
"""
9091
self.logger.info('%s: query %s', self, query)
91-
return await super().query(query) # type: ignore[misc] # noqa: F821
92+
return await super().query(query) # type: ignore[misc, no-any-return]
9293

9394

9495
class BinaryAdapter(

datastore/adapter/mount.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212
]
1313

1414

15+
mount_item_t = typing.Tuple['mount_tree_t[DS]', typing.Optional[DS]]
16+
17+
18+
class mount_tree_t(typing.Dict[str, mount_item_t[DS]]):
19+
...
20+
1521

1622
class _Adapter(typing.Generic[DS, MD, RT, RV]):
1723
__slots__ = ()
1824

19-
# Cannot represent recursive types yet (https://github.com/python/mypy/issues/731)
20-
MOUNTS_T = typing.Dict[ # type: ignore[misc] # noqa: F821
21-
str,
22-
typing.Tuple[
23-
"_Adapter.MOUNTS_T", # type: ignore[misc] # noqa: F821
24-
typing.Optional[DS] # type: ignore[misc] # noqa: F821
25-
]
26-
]
27-
28-
mounts: MOUNTS_T
25+
mounts: mount_tree_t[DS]
2926

3027

31-
def __init__(self, *args, **kwargs):
32-
self.mounts = {}
28+
def __init__(self, *args: typing.Any, **kwargs: typing.Any):
29+
self.mounts = mount_tree_t()
3330

3431

3532
def _find_mountpoint(self, key: datastore.Key) \
@@ -56,21 +53,21 @@ async def get(self, key: datastore.Key) -> RT:
5653
ds, subkey = self._find_mountpoint(key)
5754
if ds is None:
5855
raise KeyError(key)
59-
return await ds.get(subkey) # type: ignore[return-value] # noqa: F723
56+
return await ds.get(subkey) # type: ignore[return-value]
6057

6158

6259
async def get_all(self, key: datastore.Key) -> RV:
6360
ds, subkey = self._find_mountpoint(key)
6461
if ds is None:
6562
raise KeyError(key)
66-
return await ds.get_all(subkey) # type: ignore[return-value] # noqa: F723
63+
return await ds.get_all(subkey) # type: ignore[return-value]
6764

6865

6966
async def _put(self, key: datastore.Key, value: RT) -> None:
7067
ds, subkey = self._find_mountpoint(key)
7168
if ds is None:
7269
raise RuntimeError(f"Cannot put key {key}: No datastore mounted at this path")
73-
await ds.put(subkey, value) # type: ignore[arg-type] # noqa: F821
70+
await ds.put(subkey, value)
7471

7572

7673
async def delete(self, key: datastore.Key) -> None:
@@ -91,7 +88,7 @@ async def stat(self, key: datastore.Key) -> MD:
9188
ds, subkey = self._find_mountpoint(key)
9289
if ds is None:
9390
raise KeyError(key)
94-
return await ds.stat(subkey) # type: ignore[return-value] # noqa: F723
91+
return await ds.stat(subkey) # type: ignore[return-value]
9592

9693

9794
def mount(self, prefix: datastore.Key, ds: DS) -> None:
@@ -100,13 +97,13 @@ def mount(self, prefix: datastore.Key, ds: DS) -> None:
10097
If a datastore is already mounted at the given key a :exc:`KeyError` is
10198
raised.
10299
"""
103-
current: _Adapter.MOUNTS_T = self.mounts
104-
previous: _Adapter.MOUNTS_T
100+
current: mount_tree_t[DS] = self.mounts
101+
previous: mount_tree_t[DS]
105102

106103
# Walk and create all parent key parts
107104
for part in map(str, prefix.list):
108105
if part not in current:
109-
current[part] = ({}, None)
106+
current[part] = (mount_tree_t(), None)
110107
previous = current
111108
current = current[part][0]
112109

@@ -126,10 +123,10 @@ def unmount(self, prefix: datastore.Key) -> DS:
126123
The returned datastore is not closed; it is the callers responsibility
127124
to ensure this by using ``await m.unmount(key).aclose()`` or similar.
128125
"""
129-
current: _Adapter.MOUNTS_T = self.mounts
126+
current: mount_tree_t[DS] = self.mounts
130127

131128
# Walk and create all parent key parts
132-
visited: typing.List[_Adapter.MOUNTS_T] = []
129+
visited: typing.List[mount_tree_t[DS]] = []
133130
for part in map(str, prefix.list):
134131
if part not in current:
135132
raise KeyError(prefix)
@@ -160,10 +157,10 @@ def unmount_all(self) -> typing.List[DS]:
160157
unmounted: typing.List[DS] = []
161158

162159
# Start at mount hierarchy root
163-
current: _Adapter.MOUNTS_T = self.mounts
160+
current: mount_tree_t[DS] = self.mounts
164161

165162
# Walk and create all parent key parts
166-
stack: typing.List[_Adapter.MOUNTS_T] = []
163+
stack: typing.List[mount_tree_t[DS]] = []
167164
while len(self.mounts) > 0 or len(stack) > 0:
168165
try:
169166
# Pop item from submounts map

datastore/adapter/sharded.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ def get_sharded_datastore(self, key: datastore.Key) -> DS:
4545

4646
async def get(self, key: datastore.Key) -> RT:
4747
"""Return the object named by key from the corresponding datastore."""
48-
return await self.get_sharded_datastore(key).get(key) # type: ignore[return-value] # noqa: F723
48+
return await self.get_sharded_datastore(key).get(key) # type: ignore[return-value]
4949

5050

5151
async def get_all(self, key: datastore.Key) -> RV:
5252
"""Return the object named by key from the corresponding datastore."""
53-
return await self.get_sharded_datastore(key).get_all(key) # type: ignore[return-value] # noqa: F723, E501
53+
return await self.get_sharded_datastore(key).get_all(key) # type: ignore[return-value]
5454

5555

5656
async def _put(self, key: datastore.Key, value: RT) -> None:
5757
"""Stores the object to the corresponding datastore."""
58-
await self.get_sharded_datastore(key).put(key, value) # type: ignore[arg-type] # noqa: F821
58+
await self.get_sharded_datastore(key).put(key, value)
5959

6060

6161
async def delete(self, key: datastore.Key) -> None:
@@ -70,7 +70,7 @@ async def contains(self, key: datastore.Key) -> bool:
7070

7171
async def stat(self, key: datastore.Key) -> MD:
7272
"""Return the metadata of the object named by key from the corresponding datastore."""
73-
return await self.get_sharded_datastore(key).stat(key) # type: ignore[return-value] # noqa: F723
73+
return await self.get_sharded_datastore(key).stat(key) # type: ignore[return-value]
7474

7575

7676
async def query(self, query: datastore.Query) -> datastore.Cursor:
@@ -80,7 +80,7 @@ async def query(self, query: datastore.Query) -> datastore.Cursor:
8080
return cursor
8181

8282

83-
def _shard_query_generator(self, query):
83+
def _shard_query_generator(self, query): # type: ignore #FIXME: broken
8484
"""A generator that queries each shard in sequence."""
8585
shard_query = query.copy()
8686

0 commit comments

Comments
 (0)