Skip to content

Commit b1b66c4

Browse files
committed
Made changes to maje mypy happy.
1 parent 3be7d54 commit b1b66c4

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

optimade/server/entry_collections/mongo.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,24 @@ def count(self, **kwargs: Any) -> int:
102102
kwargs["filter"] = {}
103103
return len(self.collection.find(**kwargs))
104104

105-
def insert(self, content: bytes, filename: str, metadata: dict = {}) -> None:
105+
def insert(self, data: list) -> None:
106106
"""Add the given entries to the underlying database.
107107
108108
Warning:
109109
No validation is performed on the incoming data.
110110
111111
Arguments:
112-
content: The file content to add to gridfs.
113-
filename: The filename of the added content.
114-
metadata: extra metadata to add to the gridfs entry.
112+
data: a list of dictionaries. Each dictionary contains the data belonging to one file.
113+
These dictionaries contain the fields:
114+
data: The file content to add to gridfs.
115+
filename: The filename of the added content.
116+
metadata: extra metadata to add to the gridfs entry.
115117
"""
116-
self.collection.put(content, filename=filename, metadata=metadata)
118+
for entry in data: # todo check whether I can insert multiple files in one go.
119+
self.collection.put(**entry)
117120

118121
def handle_query_params(
119-
self, params: Union[SingleEntryQueryParams, PartialDataQueryParams]
122+
self, params: Union[SingleEntryQueryParams, PartialDataQueryParams] # type: ignore[override]
120123
) -> Dict[str, Any]:
121124
"""Parse and interpret the backend-agnostic query parameter models into a dictionary
122125
that can be used by MongoDB.
@@ -373,7 +376,7 @@ def insert(self, data: List[EntryResource]) -> None:
373376
self.collection.insert_many(data)
374377

375378
def handle_query_params(
376-
self, params: Union[EntryListingQueryParams, SingleEntryQueryParams]
379+
self, params: Union[EntryListingQueryParams, SingleEntryQueryParams] # type: ignore[override]
377380
) -> Dict[str, Any]:
378381
"""Parse and interpret the backend-agnostic query parameter models into a dictionary
379382
that can be used by MongoDB.

optimade/server/exception_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def general_exception_handler(request: Request, exc: Exception) -> JSONAPIRespon
230230
(OptimadeHTTPException, http_exception_handler),
231231
(RequestValidationError, request_validation_exception_handler),
232232
(ValidationError, validation_exception_handler),
233-
(VisitError, grammar_not_implemented_handler),
233+
(VisitError, grammar_not_implemented_handler), # type: ignore[list-item] # not entirely sure why this entry triggers mypy
234234
(NotImplementedError, not_implemented_handler), # type: ignore[list-item] # not entirely sure why this entry triggers mypy
235235
(Exception, general_exception_handler),
236236
]

optimade/server/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def read_array_header(fobj):
101101
"name": numpy_meta[2].name,
102102
"itemsize": numpy_meta[2].itemsize,
103103
}
104-
partial_data_coll.insert(f, filename=filename, metadata=metadata)
104+
partial_data_coll.insert([{"data": f, "filename": filename, "metadata": metadata}]) # type: ignore[list-item] # Todo : Perhaps this can be reduced to a single insert statement.
105105

106106
def load_entries(endpoint_name: str, endpoint_collection: EntryCollection):
107107
LOGGER.debug("Loading test %s...", endpoint_name)

optimade/server/routers/utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,13 @@ def get_partial_entry(
420420
)
421421

422422
array = np.frombuffer(
423-
results["attributes"]["data"],
424-
dtype=getattr(np, results["attributes"]["dtype"]["name"]),
425-
).reshape(results["attributes"]["shape"])
423+
results["attributes"]["data"], # type: ignore[call-overload]
424+
dtype=getattr(np, results["attributes"]["dtype"]["name"]), # type: ignore[call-overload]
425+
).reshape(
426+
results["attributes"]["shape"]
427+
) # type: ignore[call-overload]
426428
# slice array
427-
property_ranges = results["attributes"]["property_ranges"]
429+
property_ranges = results["attributes"]["property_ranges"] # type: ignore[call-overload]
428430
slice_ind = [
429431
slice(
430432
0,
@@ -455,14 +457,14 @@ def get_partial_entry(
455457
"has_references": False,
456458
} # Todo: add support for non_dense data
457459
if more_data_available:
458-
next_link = ["PARTIAL-DATA-NEXT", [results["attributes"].pop("next")]]
460+
next_link = ["PARTIAL-DATA-NEXT", [results["attributes"].pop("next")]] # type: ignore[call-overload]
459461

460462
if params.response_format == "json":
461463
for key in header:
462-
results["attributes"][key] = header[key]
463-
results["attributes"]["data"] = array.tolist()
464+
results["attributes"][key] = header[key] # type: ignore[call-overload]
465+
results["attributes"]["data"] = array.tolist() # type: ignore[call-overload]
464466
if more_data_available:
465-
results["attributes"]["next"] = next_link
467+
results["attributes"]["next"] = next_link # type: ignore[call-overload]
466468
return dict(
467469
links=links,
468470
data=[results] if results else None,

0 commit comments

Comments
 (0)