Skip to content

Commit

Permalink
Fix no-implicit-optional change in mypy/pep484
Browse files Browse the repository at this point in the history
  • Loading branch information
chr1st1ank committed Dec 20, 2022
1 parent 23b83cc commit b839708
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
6 changes: 3 additions & 3 deletions narrow_down/_minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(
self.rows_per_band = lsh_config.rows_per_band
self._hashfunc = hash_.murmur3_32bit

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

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

async def query_top_n(
self, n, fingerprint: Fingerprint, *, exact_part: str = None
self, n, fingerprint: Fingerprint, *, exact_part: Optional[str] = None
) -> Collection[StoredDocument]:
"""Find n most similar documents."""
tasks = []
Expand Down
2 changes: 1 addition & 1 deletion narrow_down/_rust.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RustMemoryStore:
def from_file(cls, file_path: str) -> "RustMemoryStore": ...
def insert_setting(self, key: str, value: str): ...
def query_setting(self, key: str) -> Optional[str]: ...
def insert_document(self, document: bytes, document_id: int = None) -> int: ...
def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int: ...
def query_document(self, document_id: int) -> bytes: ...
def remove_document(self, document_id: int): ...
def add_document_to_bucket(self, bucket_id: int, document_hash: int, document_id: int): ...
Expand Down
4 changes: 2 additions & 2 deletions narrow_down/scylladb.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
self,
cluster_or_session: Union[cassandra.cluster.Cluster, cassandra.cluster.Session],
keyspace: str,
table_prefix: str = None,
table_prefix: Optional[str] = None,
) -> None:
"""Create a new empty or connect to an existing SQLite database.
Expand Down Expand Up @@ -198,7 +198,7 @@ async def query_setting(self, key: str) -> Optional[str]:
return None
raise # Don't swallow unknown errors

async def insert_document(self, document: bytes, document_id: int = None) -> int:
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
"""Add the data of a document to the storage and return its ID."""
with self._session() as session:
if document_id:
Expand Down
28 changes: 20 additions & 8 deletions narrow_down/similarity_store.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""High-level API for indexing and retrieval of documents."""
import re
import warnings
from typing import Callable, Collection, Iterable, List, Union
from typing import Callable, Collection, Iterable, List, Optional, Union

from narrow_down import _minhash, _tokenize
from narrow_down._minhash import MinhashLshConfig
Expand Down Expand Up @@ -47,9 +47,9 @@ def __init__(self): # noqa: D107 # Not meant to be called, therefore omitting
async def create(
cls,
*,
storage: StorageBackend = None,
storage: Optional[StorageBackend] = None,
storage_level: StorageLevel = StorageLevel.Minimal,
tokenize: Union[str, Callable[[str], Collection[str]]] = None,
tokenize: Optional[Union[str, Callable[[str], Collection[str]]]] = None,
max_false_negative_proba: float = 0.05,
max_false_positive_proba: float = 0.05,
similarity_threshold: float = 0.75,
Expand Down Expand Up @@ -108,7 +108,9 @@ async def create(

@classmethod
async def load_from_storage(
cls, storage: StorageBackend, tokenize: Union[str, Callable[[str], Collection[str]]] = None
cls,
storage: StorageBackend,
tokenize: Optional[Union[str, Callable[[str], Collection[str]]]] = None,
) -> "SimilarityStore":
"""Load a SimilarityStore object from already initialized storage.
Expand Down Expand Up @@ -213,13 +215,18 @@ async def _initialize_storage(self):
await self._storage.initialize()
await self._storage.insert_setting("similarity_threshold", str(self._similarity_threshold))
await self._storage.insert_setting("storage_level", str(self._storage_level.value))
await self._storage.insert_setting("tokenize", self._tokenize)
await self._storage.insert_setting("tokenize", self._tokenize) # type: ignore
await self._storage.insert_setting("lsh_config", self._lsh_config.to_json())
self._minhasher = _minhash.MinHasher(n_hashes=self._lsh_config.n_hashes)
self._lsh = _minhash.LSH(self._lsh_config, storage=self._storage)

async def insert(
self, document: str, *, document_id: int = None, exact_part: str = None, data: str = None
self,
document: str,
*,
document_id: Optional[int] = None,
exact_part: Optional[str] = None,
data: Optional[str] = None,
) -> int:
"""Index a new document.
Expand Down Expand Up @@ -282,7 +289,7 @@ def _filter_candidates(self, candidates, tokens, exact_part) -> List[StoredDocum
return candidates

async def query(
self, document: str, *, exact_part: str = None, validate: bool = None
self, document: str, *, exact_part: Optional[str] = None, validate: Optional[bool] = None
) -> Collection[StoredDocument]:
"""Query all similar documents.
Expand All @@ -305,7 +312,12 @@ async def query(
return candidates

async def query_top_n(
self, n: int, document: str, *, exact_part: str = None, validate: bool = None
self,
n: int,
document: str,
*,
exact_part: Optional[str] = None,
validate: Optional[bool] = None,
) -> Collection[StoredDocument]:
"""Query the top n similar documents.
Expand Down
2 changes: 1 addition & 1 deletion narrow_down/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def _query_setting_sync(self, key: str) -> Optional[str]:
return None
raise

async def insert_document(self, document: bytes, document_id: int = None) -> int:
async def insert_document(self, document: bytes, document_id: Optional[int] = None) -> int:
"""Add the data of a document to the storage and return its ID."""
with self._connection as conn:
if document_id:
Expand Down
4 changes: 2 additions & 2 deletions narrow_down/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ async def query_setting(self, key: str) -> Optional[str]:
raise NotImplementedError

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

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

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

Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ disable = [
"abstract-class-instantiated",
]

[tool.mypy]
follow_imports = "silent"
python_version = "3.8"

check_untyped_defs = true

[[tool.mypy.overrides]]
module = [
"pytest",
Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def lint(c):
def mypy(c):
# type: (Context) -> None
"""Run mypy."""
_run(c, f"mypy --follow-imports silent --python-version 3.8 {PYTHON_TARGETS_STR}")
_run(c, f"mypy --config-file pyproject.toml {SOURCE_DIR}")


@task()
Expand Down

0 comments on commit b839708

Please sign in to comment.