Skip to content

Commit b839708

Browse files
committed
Fix no-implicit-optional change in mypy/pep484
python/typing#275
1 parent 23b83cc commit b839708

File tree

8 files changed

+36
-18
lines changed

8 files changed

+36
-18
lines changed

narrow_down/_minhash.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __init__(
106106
self.rows_per_band = lsh_config.rows_per_band
107107
self._hashfunc = hash_.murmur3_32bit
108108

109-
def _hash(self, arr: npt.NDArray, exact_part: str = None) -> int:
109+
def _hash(self, arr: npt.NDArray, exact_part: Optional[str] = None) -> int:
110110
"""Merge multiple hashes together to one hash."""
111111
if arr.dtype != np.uint32:
112112
# Other data types like the standard int64 have a different binary representation
@@ -176,7 +176,7 @@ async def remove_by_id(self, document_id: int, check_if_exists: bool = False) ->
176176
await self._storage.remove_document(document_id=document_id)
177177

178178
async def query(
179-
self, fingerprint: Fingerprint, *, exact_part: str = None
179+
self, fingerprint: Fingerprint, *, exact_part: Optional[str] = None
180180
) -> Collection[StoredDocument]:
181181
"""Find all similar documents."""
182182
tasks = []
@@ -192,7 +192,7 @@ async def query(
192192
return await self._query_documents(list(candidates))
193193

194194
async def query_top_n(
195-
self, n, fingerprint: Fingerprint, *, exact_part: str = None
195+
self, n, fingerprint: Fingerprint, *, exact_part: Optional[str] = None
196196
) -> Collection[StoredDocument]:
197197
"""Find n most similar documents."""
198198
tasks = []

narrow_down/_rust.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RustMemoryStore:
1717
def from_file(cls, file_path: str) -> "RustMemoryStore": ...
1818
def insert_setting(self, key: str, value: str): ...
1919
def query_setting(self, key: str) -> Optional[str]: ...
20-
def insert_document(self, document: bytes, document_id: int = None) -> int: ...
20+
def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int: ...
2121
def query_document(self, document_id: int) -> bytes: ...
2222
def remove_document(self, document_id: int): ...
2323
def add_document_to_bucket(self, bucket_id: int, document_hash: int, document_id: int): ...

narrow_down/scylladb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(
4949
self,
5050
cluster_or_session: Union[cassandra.cluster.Cluster, cassandra.cluster.Session],
5151
keyspace: str,
52-
table_prefix: str = None,
52+
table_prefix: Optional[str] = None,
5353
) -> None:
5454
"""Create a new empty or connect to an existing SQLite database.
5555
@@ -198,7 +198,7 @@ async def query_setting(self, key: str) -> Optional[str]:
198198
return None
199199
raise # Don't swallow unknown errors
200200

201-
async def insert_document(self, document: bytes, document_id: int = None) -> int:
201+
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
202202
"""Add the data of a document to the storage and return its ID."""
203203
with self._session() as session:
204204
if document_id:

narrow_down/similarity_store.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""High-level API for indexing and retrieval of documents."""
22
import re
33
import warnings
4-
from typing import Callable, Collection, Iterable, List, Union
4+
from typing import Callable, Collection, Iterable, List, Optional, Union
55

66
from narrow_down import _minhash, _tokenize
77
from narrow_down._minhash import MinhashLshConfig
@@ -47,9 +47,9 @@ def __init__(self): # noqa: D107 # Not meant to be called, therefore omitting
4747
async def create(
4848
cls,
4949
*,
50-
storage: StorageBackend = None,
50+
storage: Optional[StorageBackend] = None,
5151
storage_level: StorageLevel = StorageLevel.Minimal,
52-
tokenize: Union[str, Callable[[str], Collection[str]]] = None,
52+
tokenize: Optional[Union[str, Callable[[str], Collection[str]]]] = None,
5353
max_false_negative_proba: float = 0.05,
5454
max_false_positive_proba: float = 0.05,
5555
similarity_threshold: float = 0.75,
@@ -108,7 +108,9 @@ async def create(
108108

109109
@classmethod
110110
async def load_from_storage(
111-
cls, storage: StorageBackend, tokenize: Union[str, Callable[[str], Collection[str]]] = None
111+
cls,
112+
storage: StorageBackend,
113+
tokenize: Optional[Union[str, Callable[[str], Collection[str]]]] = None,
112114
) -> "SimilarityStore":
113115
"""Load a SimilarityStore object from already initialized storage.
114116
@@ -213,13 +215,18 @@ async def _initialize_storage(self):
213215
await self._storage.initialize()
214216
await self._storage.insert_setting("similarity_threshold", str(self._similarity_threshold))
215217
await self._storage.insert_setting("storage_level", str(self._storage_level.value))
216-
await self._storage.insert_setting("tokenize", self._tokenize)
218+
await self._storage.insert_setting("tokenize", self._tokenize) # type: ignore
217219
await self._storage.insert_setting("lsh_config", self._lsh_config.to_json())
218220
self._minhasher = _minhash.MinHasher(n_hashes=self._lsh_config.n_hashes)
219221
self._lsh = _minhash.LSH(self._lsh_config, storage=self._storage)
220222

221223
async def insert(
222-
self, document: str, *, document_id: int = None, exact_part: str = None, data: str = None
224+
self,
225+
document: str,
226+
*,
227+
document_id: Optional[int] = None,
228+
exact_part: Optional[str] = None,
229+
data: Optional[str] = None,
223230
) -> int:
224231
"""Index a new document.
225232
@@ -282,7 +289,7 @@ def _filter_candidates(self, candidates, tokens, exact_part) -> List[StoredDocum
282289
return candidates
283290

284291
async def query(
285-
self, document: str, *, exact_part: str = None, validate: bool = None
292+
self, document: str, *, exact_part: Optional[str] = None, validate: Optional[bool] = None
286293
) -> Collection[StoredDocument]:
287294
"""Query all similar documents.
288295
@@ -305,7 +312,12 @@ async def query(
305312
return candidates
306313

307314
async def query_top_n(
308-
self, n: int, document: str, *, exact_part: str = None, validate: bool = None
315+
self,
316+
n: int,
317+
document: str,
318+
*,
319+
exact_part: Optional[str] = None,
320+
validate: Optional[bool] = None,
309321
) -> Collection[StoredDocument]:
310322
"""Query the top n similar documents.
311323

narrow_down/sqlite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _query_setting_sync(self, key: str) -> Optional[str]:
8585
return None
8686
raise
8787

88-
async def insert_document(self, document: bytes, document_id: int = None) -> int:
88+
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
8989
"""Add the data of a document to the storage and return its ID."""
9090
with self._connection as conn:
9191
if document_id:

narrow_down/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ async def query_setting(self, key: str) -> Optional[str]:
136136
raise NotImplementedError
137137

138138
@abstractmethod
139-
async def insert_document(self, document: bytes, document_id: int = None) -> int:
139+
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
140140
"""Add the data of a document to the storage and return its ID."""
141141
raise NotImplementedError()
142142

@@ -228,7 +228,7 @@ async def query_setting(self, key: str) -> Optional[str]:
228228
"""Query a setting with the given key."""
229229
return self.rms.query_setting(key)
230230

231-
async def insert_document(self, document: bytes, document_id: int = None) -> int:
231+
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
232232
"""Add the data of a document to the storage and return its ID."""
233233
return self.rms.insert_document(document, document_id)
234234

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ disable = [
249249
"abstract-class-instantiated",
250250
]
251251

252+
[tool.mypy]
253+
follow_imports = "silent"
254+
python_version = "3.8"
255+
256+
check_untyped_defs = true
257+
252258
[[tool.mypy.overrides]]
253259
module = [
254260
"pytest",

tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def lint(c):
194194
def mypy(c):
195195
# type: (Context) -> None
196196
"""Run mypy."""
197-
_run(c, f"mypy --follow-imports silent --python-version 3.8 {PYTHON_TARGETS_STR}")
197+
_run(c, f"mypy --config-file pyproject.toml {SOURCE_DIR}")
198198

199199

200200
@task()

0 commit comments

Comments
 (0)