Skip to content

Commit 9539a20

Browse files
authored
refactor: apply pep-484 (#3542)
* apply pep-484 * another implicit optional * apply pep-484 on rest_api and ui too
1 parent 43b24fd commit 9539a20

Some content is hidden

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

47 files changed

+269
-214
lines changed

haystack/document_stores/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def add_eval_data(
376376
label_index: str = "label",
377377
batch_size: Optional[int] = None,
378378
preprocessor: Optional[PreProcessor] = None,
379-
max_docs: Union[int, bool] = None,
379+
max_docs: Optional[Union[int, bool]] = None,
380380
open_domain: bool = False,
381381
headers: Optional[Dict[str, str]] = None,
382382
):
@@ -568,7 +568,7 @@ def get_documents_by_id(
568568
pass
569569

570570
@abstractmethod
571-
def update_document_meta(self, id: str, meta: Dict[str, Any], index: str = None):
571+
def update_document_meta(self, id: str, meta: Dict[str, Any], index: Optional[str] = None):
572572
pass
573573

574574
def _drop_duplicate_documents(self, documents: List[Document], index: Optional[str] = None) -> List[Document]:
@@ -633,7 +633,7 @@ def _handle_duplicate_documents(
633633
return documents
634634

635635
def _get_duplicate_labels(
636-
self, labels: list, index: str = None, headers: Optional[Dict[str, str]] = None
636+
self, labels: list, index: Optional[str] = None, headers: Optional[Dict[str, str]] = None
637637
) -> List[Label]:
638638
"""
639639
Return all duplicate labels

haystack/document_stores/deepsetcloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def wrapper(self, *args, **kwargs):
3737
class DeepsetCloudDocumentStore(KeywordDocumentStore):
3838
def __init__(
3939
self,
40-
api_key: str = None,
40+
api_key: Optional[str] = None,
4141
workspace: str = "default",
4242
index: Optional[str] = None,
4343
duplicate_documents: str = "overwrite",
@@ -603,7 +603,7 @@ def write_documents(
603603
pass
604604

605605
@disable_and_log
606-
def update_document_meta(self, id: str, meta: Dict[str, Any], index: str = None):
606+
def update_document_meta(self, id: str, meta: Dict[str, Any], index: Optional[str] = None):
607607
"""
608608
Update the metadata dictionary of a document by specifying its string id.
609609

haystack/document_stores/faiss.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FAISSDocumentStore(SQLDocumentStore):
4242
def __init__(
4343
self,
4444
sql_url: str = "sqlite:///faiss_document_store.db",
45-
vector_dim: int = None,
45+
vector_dim: Optional[int] = None,
4646
embedding_dim: int = 768,
4747
faiss_index_factory_str: str = "Flat",
4848
faiss_index: Optional[faiss.swigfaiss.Index] = None,
@@ -52,9 +52,9 @@ def __init__(
5252
embedding_field: str = "embedding",
5353
progress_bar: bool = True,
5454
duplicate_documents: str = "overwrite",
55-
faiss_index_path: Union[str, Path] = None,
56-
faiss_config_path: Union[str, Path] = None,
57-
isolation_level: str = None,
55+
faiss_index_path: Optional[Union[str, Path]] = None,
56+
faiss_config_path: Optional[Union[str, Path]] = None,
57+
isolation_level: Optional[str] = None,
5858
n_links: int = 64,
5959
ef_search: int = 20,
6060
ef_construction: int = 80,

haystack/document_stores/memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def get_document_count(
485485
)
486486
return len(documents)
487487

488-
def update_document_meta(self, id: str, meta: Dict[str, Any], index: str = None):
488+
def update_document_meta(self, id: str, meta: Dict[str, Any], index: Optional[str] = None):
489489
"""
490490
Update the metadata dictionary of a document by specifying its string id.
491491
@@ -639,7 +639,7 @@ def get_all_documents_generator(
639639

640640
def get_all_labels(
641641
self,
642-
index: str = None,
642+
index: Optional[str] = None,
643643
filters: Optional[Dict[str, Any]] = None, # TODO: Adapt type once we allow extended filters in InMemoryDocStore
644644
headers: Optional[Dict[str, str]] = None,
645645
) -> List[Label]:

haystack/document_stores/milvus1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(
4646
milvus_url: str = "tcp://localhost:19530",
4747
connection_pool: str = "SingletonThread",
4848
index: str = "document",
49-
vector_dim: int = None,
49+
vector_dim: Optional[int] = None,
5050
embedding_dim: int = 768,
5151
index_file_size: int = 1024,
5252
similarity: str = "dot_product",
@@ -57,7 +57,7 @@ def __init__(
5757
embedding_field: str = "embedding",
5858
progress_bar: bool = True,
5959
duplicate_documents: str = "overwrite",
60-
isolation_level: str = None,
60+
isolation_level: Optional[str] = None,
6161
):
6262
"""
6363
**WARNING:** Milvus1DocumentStore is deprecated and will be removed in a future version. Please switch to Milvus2

haystack/document_stores/milvus2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def __init__(
6161
port: str = "19530",
6262
connection_pool: str = "SingletonThread",
6363
index: str = "document",
64-
vector_dim: int = None,
64+
vector_dim: Optional[int] = None,
6565
embedding_dim: int = 768,
6666
index_file_size: int = 1024,
6767
similarity: str = "dot_product",
@@ -74,7 +74,7 @@ def __init__(
7474
custom_fields: Optional[List[Any]] = None,
7575
progress_bar: bool = True,
7676
duplicate_documents: str = "overwrite",
77-
isolation_level: str = None,
77+
isolation_level: Optional[str] = None,
7878
consistency_level: int = 0,
7979
recreate_index: bool = False,
8080
):

haystack/document_stores/pinecone.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def get_documents_by_id(
762762
batch_size: int = 32,
763763
headers: Optional[Dict[str, str]] = None,
764764
return_embedding: Optional[bool] = None,
765-
namespace: str = None,
765+
namespace: Optional[str] = None,
766766
) -> List[Document]:
767767
"""
768768
Retrieves all documents in the index using their IDs.
@@ -826,7 +826,7 @@ def get_document_by_id(
826826
index: Optional[str] = None,
827827
headers: Optional[Dict[str, str]] = None,
828828
return_embedding: Optional[bool] = None,
829-
namespace: str = None,
829+
namespace: Optional[str] = None,
830830
) -> Document:
831831
"""
832832
Returns a single Document retrieved using an ID.
@@ -869,7 +869,7 @@ def get_embedding_count(
869869
count = 0
870870
return count
871871

872-
def update_document_meta(self, id: str, meta: Dict[str, str], namespace: str = None, index: str = None): # type: ignore
872+
def update_document_meta(self, id: str, meta: Dict[str, str], namespace: Optional[str] = None, index: Optional[str] = None): # type: ignore
873873
"""
874874
Update the metadata dictionary of a document by specifying its string ID.
875875

haystack/document_stores/search_engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def write_labels(
487487
self._bulk(labels_to_index, request_timeout=300, refresh=self.refresh_type, headers=headers)
488488

489489
def update_document_meta(
490-
self, id: str, meta: Dict[str, str], index: str = None, headers: Optional[Dict[str, str]] = None
490+
self, id: str, meta: Dict[str, str], index: Optional[str] = None, headers: Optional[Dict[str, str]] = None
491491
):
492492
"""
493493
Update the metadata dictionary of a document by specifying its string id

haystack/document_stores/sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(
129129
label_index: str = "label",
130130
duplicate_documents: str = "overwrite",
131131
check_same_thread: bool = False,
132-
isolation_level: str = None,
132+
isolation_level: Optional[str] = None,
133133
):
134134
"""
135135
An SQL backed DocumentStore. Currently supports SQLite, PostgreSQL and MySQL backends.
@@ -524,7 +524,7 @@ def reset_vector_ids(self, index: Optional[str] = None):
524524
self.session.query(DocumentORM).filter_by(index=index).update({DocumentORM.vector_id: null()})
525525
self.session.commit()
526526

527-
def update_document_meta(self, id: str, meta: Dict[str, str], index: str = None):
527+
def update_document_meta(self, id: str, meta: Dict[str, str], index: Optional[str] = None):
528528
"""
529529
Update the metadata dictionary of a document by specifying its string id
530530
"""

haystack/document_stores/utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818

1919
def eval_data_from_json(
20-
filename: str, max_docs: Union[int, bool] = None, preprocessor: PreProcessor = None, open_domain: bool = False
20+
filename: str,
21+
max_docs: Optional[Union[int, bool]] = None,
22+
preprocessor: Optional[PreProcessor] = None,
23+
open_domain: bool = False,
2124
) -> Tuple[List[Document], List[Label]]:
2225
"""
2326
Read Documents + Labels from a SQuAD-style file.
@@ -58,8 +61,8 @@ def eval_data_from_json(
5861
def eval_data_from_jsonl(
5962
filename: str,
6063
batch_size: Optional[int] = None,
61-
max_docs: Union[int, bool] = None,
62-
preprocessor: PreProcessor = None,
64+
max_docs: Optional[Union[int, bool]] = None,
65+
preprocessor: Optional[PreProcessor] = None,
6366
open_domain: bool = False,
6467
) -> Generator[Tuple[List[Document], List[Label]], None, None]:
6568
"""
@@ -123,7 +126,7 @@ def squad_json_to_jsonl(squad_file: str, output_file: str):
123126

124127

125128
def _extract_docs_and_labels_from_dict(
126-
document_dict: Dict, preprocessor: PreProcessor = None, open_domain: bool = False
129+
document_dict: Dict, preprocessor: Optional[PreProcessor] = None, open_domain: bool = False
127130
):
128131
"""
129132
Set open_domain to True if you are trying to load open_domain labels (i.e. labels without doc id or start idx)

0 commit comments

Comments
 (0)