55import logging
66from os import linesep
77from pathlib import Path
8- from typing import Optional , Union
98
109from ase import Atoms
1110from ase .io import iread
3938class OpenSearchQuery (AbstractQuerySet ):
4039 """Class to parse and build queries for OpenSearch."""
4140
42- def __call__ (self , query : Optional [ Union [ dict , str , list ]] ) -> Optional [ dict ] :
41+ def __call__ (self , query : dict | str | list | None ) -> dict | None :
4342 """
4443 Parses and builds queries for OpenSearch.
4544
@@ -115,9 +114,9 @@ class AtomsModel(AbstractModel):
115114
116115 def __init__ (
117116 self ,
118- client : Optional [ OpenSearch ] = None ,
119- index_name : Optional [ str ] = None ,
120- dict : Optional [ dict ] = None ,
117+ client : OpenSearch | None = None ,
118+ index_name : str | None = None ,
119+ dict : dict | None = None ,
121120 ):
122121 """
123122 Initialises class.
@@ -142,7 +141,7 @@ def from_atoms(
142141 client : OpenSearch ,
143142 index_name : str ,
144143 atoms : Atoms ,
145- extra_info : Optional [ dict ] = None ,
144+ extra_info : dict | None = None ,
146145 store_calc : bool = True ,
147146 ) -> AtomsModel :
148147 """
@@ -173,7 +172,7 @@ def from_atoms(
173172 return obj
174173
175174 @property
176- def _id (self ) -> Optional [ str ] :
175+ def _id (self ) -> str | None :
177176 """
178177 Get the OpenSearch document ID stored in data.
179178
@@ -316,7 +315,7 @@ def info(self):
316315 "type" : "opensearch" ,
317316 }
318317
319- def delete (self , query : Optional [ Union [ dict , str ]] = None ):
318+ def delete (self , query : dict | str | None = None ):
320319 """
321320 Deletes documents from the database.
322321
@@ -375,8 +374,8 @@ def save_bulk(self, actions: Iterable[dict], **kwargs):
375374
376375 def push (
377376 self ,
378- atoms : Union [ Atoms , Iterable ] ,
379- extra_info : Optional [ Union [ dict , str , list ]] = None ,
377+ atoms : Atoms | Iterable ,
378+ extra_info : dict | str | list | None = None ,
380379 store_calc : bool = True ,
381380 ** kwargs ,
382381 ):
@@ -431,7 +430,7 @@ def push(
431430 def upload (
432431 self ,
433432 file : Path ,
434- extra_infos : Union [ Iterable , dict ] = (),
433+ extra_infos : Iterable | dict = (),
435434 store_calc : bool = True ,
436435 ):
437436 """
@@ -461,7 +460,7 @@ def upload(
461460 data = iread (str (file ))
462461 self .push (data , extra_info , store_calc = store_calc )
463462
464- def get_items (self , query : Optional [ Union [ dict , str ]] = None ) -> Iterator [dict ]:
463+ def get_items (self , query : dict | str | None = None ) -> Iterator [dict ]:
465464 """
466465 Get data as a dictionary from documents in the database.
467466
@@ -488,7 +487,7 @@ def get_items(self, query: Optional[Union[dict, str]] = None) -> Iterator[dict]:
488487 ):
489488 yield {"_id" : hit ["_id" ], ** hit ["_source" ]}
490489
491- def get_atoms (self , query : Optional [ Union [ dict , str ]] = None ) -> Iterator [Atoms ]:
490+ def get_atoms (self , query : dict | str | None = None ) -> Iterator [Atoms ]:
492491 """
493492 Get data as Atoms object from documents in the database.
494493
@@ -515,7 +514,7 @@ def get_atoms(self, query: Optional[Union[dict, str]] = None) -> Iterator[Atoms]
515514 ):
516515 yield AtomsModel (dict = hit ["_source" ]).to_ase ()
517516
518- def count (self , query : Optional [ Union [ dict , str ]] = None , timeout = 30.0 ) -> int :
517+ def count (self , query : dict | str | None = None , timeout = 30.0 ) -> int :
519518 """
520519 Counts number of documents in the database.
521520
@@ -542,8 +541,8 @@ def count(self, query: Optional[Union[dict, str]] = None, timeout=30.0) -> int:
542541
543542 def _get_props_from_source (
544543 self ,
545- names : Union [ str , list [str ] ],
546- query : Optional [ Union [ dict , str ]] = None ,
544+ names : str | list [str ],
545+ query : dict | str | None = None ,
547546 ) -> dict :
548547 """
549548 Gets all values of specified properties using the original data from _source.
@@ -578,10 +577,10 @@ def _get_props_from_source(
578577
579578 def property (
580579 self ,
581- names : Union [ str , list [str ] ],
580+ names : str | list [str ],
582581 allow_flatten : bool = True ,
583- query : Optional [ Union [ dict , str ]] = None ,
584- ) -> Union [ dict , list ] :
582+ query : dict | str | None = None ,
583+ ) -> dict | list :
585584 """
586585 Gets all values of specified properties for matching documents in the database.
587586
@@ -645,7 +644,7 @@ def property(
645644 return props [names [0 ]]
646645 return props
647646
648- def count_property (self , name , query : Optional [ Union [ dict , str ]] = None ) -> dict :
647+ def count_property (self , name , query : dict | str | None = None ) -> dict :
649648 """
650649 Counts values of a specified property for matching documents in the
651650 database. This method much faster than performing a Count on the list
@@ -686,7 +685,7 @@ def count_property(self, name, query: Optional[Union[dict, str]] = None) -> dict
686685
687686 return prop
688687
689- def properties (self , query : Optional [ Union [ dict , str ]] = None ) -> dict :
688+ def properties (self , query : dict | str | None = None ) -> dict :
690689 """
691690 Gets lists of all properties from matching documents, separated into
692691 info, derived, and array properties.
@@ -778,7 +777,7 @@ def get_type_of_property(self, prop: str, category: str) -> str:
778777 return f"vector({ map_types [type (data [0 ])]} )"
779778 return f"scalar({ map_types [type (data )]} )"
780779
781- def count_properties (self , query : Optional [ Union [ dict , str ]] = None ) -> dict :
780+ def count_properties (self , query : dict | str | None = None ) -> dict :
782781 """
783782 Counts all properties from matching documents.
784783
@@ -838,7 +837,7 @@ def count_properties(self, query: Optional[Union[dict, str]] = None) -> dict:
838837
839838 return properties
840839
841- def add_property (self , data : dict , query : Optional [ Union [ dict , str ]] = None ):
840+ def add_property (self , data : dict , query : dict | str | None = None ):
842841 """
843842 Adds properties to matching documents.
844843
@@ -871,7 +870,7 @@ def add_property(self, data: dict, query: Optional[Union[dict, str]] = None):
871870 )
872871
873872 def rename_property (
874- self , name : str , new_name : str , query : Optional [ Union [ dict , str ]] = None
873+ self , name : str , new_name : str , query : dict | str | None = None
875874 ):
876875 """
877876 Renames property for all matching documents.
@@ -908,7 +907,7 @@ def rename_property(
908907
909908 self .client .update_by_query (index = self .index_name , body = body )
910909
911- def delete_property (self , name : str , query : Optional [ Union [ dict , str ]] = None ):
910+ def delete_property (self , name : str , query : dict | str | None = None ):
912911 """
913912 Deletes property from all matching documents.
914913
@@ -942,8 +941,8 @@ def delete_property(self, name: str, query: Optional[Union[dict, str]] = None):
942941 self .client .update_by_query (index = self .index_name , body = body )
943942
944943 def hist (
945- self , name : str , query : Optional [ Union [ dict , str ]] = None , ** kwargs
946- ) -> Optional [ dict ] :
944+ self , name : str , query : dict | str | None = None , ** kwargs
945+ ) -> dict | None :
947946 """
948947 Calculate histogram statistics for a property from all matching documents.
949948
0 commit comments