Skip to content

Commit 92707b1

Browse files
committed
Eliminate old-fashioned type annotations in index_service
1 parent f7c3bee commit 92707b1

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/azul/indexer/index_service.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717
)
1818
from typing import (
1919
Any,
20-
MutableSet,
21-
Optional,
2220
TYPE_CHECKING,
23-
Type,
24-
Union,
2521
cast,
2622
)
2723

@@ -267,7 +263,7 @@ def transform(self,
267263
partition: BundlePartition = BundlePartition.root,
268264
*,
269265
delete: bool,
270-
) -> Union[list[BundlePartition], tuple[list[Contribution], list[Replica]]]:
266+
) -> list[BundlePartition] | tuple[list[Contribution], list[Replica]]:
271267
"""
272268
Return a list of contributions and a list of replicas for the entities
273269
in the given partition of the specified bundle, or a set of divisions of
@@ -353,7 +349,7 @@ def stringify(value: AnyJSON) -> AnyJSON:
353349
)
354350

355351
def setify(value: CompositeJSON
356-
) -> Union[set[tuple[str, AnyJSON]], set[AnyJSON]]:
352+
) -> set[tuple[str, AnyJSON]] | set[AnyJSON]:
357353
value = freeze(value)
358354
return set(
359355
value.items()
@@ -578,7 +574,7 @@ def _read_contributions(self,
578574
) -> list[CataloguedContribution]:
579575
es_client = ESClientFactory.get()
580576

581-
entity_ids_by_index: dict[str, MutableSet[str]] = defaultdict(set)
577+
entity_ids_by_index: dict[str, set[str]] = defaultdict(set)
582578
for entity in tallies.keys():
583579
index = str(IndexName.create(catalog=entity.catalog,
584580
qualifier=entity.entity_type,
@@ -696,7 +692,7 @@ def _aggregate(self,
696692
)
697693

698694
# Create lookup for transformer by entity type
699-
transformers: dict[tuple[CatalogName, str], Type[Transformer]] = {
695+
transformers: dict[tuple[CatalogName, str], type[Transformer]] = {
700696
(catalog, transformer_cls.entity_type()): transformer_cls
701697
for catalog in config.catalogs
702698
for transformer_cls in self.transformer_types(catalog)
@@ -734,7 +730,7 @@ def _aggregate(self,
734730
return aggregates
735731

736732
def _aggregate_entity(self,
737-
transformer: Type[Transformer],
733+
transformer: type[Transformer],
738734
contributions: list[Contribution]
739735
) -> JSON:
740736
contents = self._reconcile(transformer, contributions)
@@ -756,7 +752,7 @@ def _aggregate_entity(self,
756752
return aggregate_contents
757753

758754
def _reconcile(self,
759-
transformer: Type[Transformer],
755+
transformer: type[Transformer],
760756
contributions: Sequence[Contribution],
761757
) -> Mapping[EntityType, Entities]:
762758
"""
@@ -791,7 +787,7 @@ def _reconcile(self,
791787

792788
def _create_writer(self,
793789
doc_type: DocumentType,
794-
catalog: Optional[CatalogName]
790+
catalog: CatalogName | None
795791
) -> 'IndexWriter':
796792
# We allow one conflict retry in the case of duplicate notifications and
797793
# switch from 'add' to 'update'. After that, there should be no
@@ -814,9 +810,9 @@ def _create_writer(self,
814810
class IndexWriter:
815811

816812
def __init__(self,
817-
catalog: Optional[CatalogName],
813+
catalog: CatalogName | None,
818814
field_types: CataloguedFieldTypes,
819-
refresh: Union[bool, str],
815+
refresh: bool | str,
820816
conflict_retry_limit: int,
821817
error_retry_limit: int) -> None:
822818
"""
@@ -843,7 +839,7 @@ def __init__(self,
843839
self.es_client = ESClientFactory.get()
844840
self.errors: dict[DocumentCoordinates, int] = defaultdict(int)
845841
self.conflicts: dict[DocumentCoordinates, int] = defaultdict(int)
846-
self.retries: Optional[MutableSet[DocumentCoordinates]] = None
842+
self.retries: set[DocumentCoordinates] | None = None
847843

848844
bulk_threshold = 32
849845

@@ -945,7 +941,7 @@ def _on_success(self, doc: Document):
945941
else:
946942
log.debug('Successfully wrote %s.', coordinates)
947943

948-
def _on_error(self, doc: Document, e: Union[Exception, JSON]):
944+
def _on_error(self, doc: Document, e: Exception | JSON):
949945
self.errors[doc.coordinates] += 1
950946
if self.error_retry_limit is None or self.errors[doc.coordinates] <= self.error_retry_limit:
951947
action = 'retrying'
@@ -956,7 +952,7 @@ def _on_error(self, doc: Document, e: Union[Exception, JSON]):
956952
doc.coordinates, e, self.errors[doc.coordinates], action,
957953
exc_info=isinstance(e, Exception))
958954

959-
def _on_conflict(self, doc: Document, e: Union[Exception, JSON]):
955+
def _on_conflict(self, doc: Document, e: Exception | JSON):
960956
self.conflicts[doc.coordinates] += 1
961957
self.errors.pop(doc.coordinates, None) # a conflict resets the error count
962958
if self.conflict_retry_limit is None or self.conflicts[doc.coordinates] <= self.conflict_retry_limit:

0 commit comments

Comments
 (0)