arkindex-base-worker 0.4.0rc6__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. {arkindex_base_worker-0.4.0rc6.dist-info → arkindex_base_worker-0.5.0.dist-info}/METADATA +9 -12
  2. arkindex_base_worker-0.5.0.dist-info/RECORD +60 -0
  3. {arkindex_base_worker-0.4.0rc6.dist-info → arkindex_base_worker-0.5.0.dist-info}/WHEEL +1 -1
  4. {arkindex_base_worker-0.4.0rc6.dist-info → arkindex_base_worker-0.5.0.dist-info}/top_level.txt +1 -0
  5. arkindex_worker/__init__.py +3 -0
  6. arkindex_worker/cache.py +6 -25
  7. arkindex_worker/image.py +105 -66
  8. arkindex_worker/utils.py +2 -1
  9. arkindex_worker/worker/__init__.py +17 -31
  10. arkindex_worker/worker/base.py +16 -9
  11. arkindex_worker/worker/classification.py +36 -34
  12. arkindex_worker/worker/corpus.py +3 -3
  13. arkindex_worker/worker/dataset.py +9 -9
  14. arkindex_worker/worker/element.py +261 -231
  15. arkindex_worker/worker/entity.py +137 -206
  16. arkindex_worker/worker/image.py +3 -3
  17. arkindex_worker/worker/metadata.py +27 -38
  18. arkindex_worker/worker/task.py +9 -9
  19. arkindex_worker/worker/training.py +15 -11
  20. arkindex_worker/worker/transcription.py +77 -71
  21. examples/standalone/python/worker.py +171 -0
  22. examples/tooled/python/worker.py +50 -0
  23. tests/conftest.py +22 -36
  24. tests/test_base_worker.py +1 -1
  25. tests/test_cache.py +1 -2
  26. tests/test_dataset_worker.py +1 -1
  27. tests/test_elements_worker/test_element.py +200 -26
  28. tests/test_elements_worker/{test_entity_create.py → test_entity.py} +220 -227
  29. tests/test_elements_worker/test_metadata.py +0 -47
  30. tests/test_elements_worker/test_training.py +8 -8
  31. tests/test_elements_worker/test_worker.py +15 -14
  32. tests/test_image.py +244 -126
  33. tests/test_merge.py +0 -7
  34. tests/test_utils.py +37 -0
  35. arkindex_base_worker-0.4.0rc6.dist-info/RECORD +0 -61
  36. arkindex_worker/worker/version.py +0 -58
  37. tests/test_elements_worker/test_entity_list_and_check.py +0 -160
  38. tests/test_elements_worker/test_version.py +0 -60
  39. {arkindex_base_worker-0.4.0rc6.dist-info → arkindex_base_worker-0.5.0.dist-info/licenses}/LICENSE +0 -0
@@ -5,12 +5,12 @@ ElementsWorker methods for elements and element types.
5
5
  import os
6
6
  from collections.abc import Iterable
7
7
  from operator import attrgetter
8
- from typing import NamedTuple
9
8
  from uuid import UUID
10
9
  from warnings import warn
11
10
 
12
11
  from peewee import IntegrityError
13
12
 
13
+ from arkindex.exceptions import ErrorResponse
14
14
  from arkindex_worker import logger
15
15
  from arkindex_worker.cache import CachedElement, CachedImage, unsupported_cache
16
16
  from arkindex_worker.models import Element
@@ -22,19 +22,10 @@ from arkindex_worker.utils import (
22
22
  )
23
23
 
24
24
 
25
- class ElementType(NamedTuple):
25
+ class MissingElementType(Exception):
26
26
  """
27
- Arkindex Type of an element
28
- """
29
-
30
- name: str
31
- slug: str
32
- is_folder: bool
33
-
34
-
35
- class MissingTypeError(Exception):
36
- """
37
- A required element type was not found in a corpus.
27
+ Raised when the specified element type was not found in the corpus and
28
+ the worker cannot create it.
38
29
  """
39
30
 
40
31
 
@@ -67,61 +58,96 @@ class ElementMixin:
67
58
  }
68
59
  count = len(self.corpus_types)
69
60
  logger.info(
70
- f'Loaded {count} element {pluralize("type", count)} in corpus ({self.corpus_id}).'
61
+ f"Loaded {count} element {pluralize('type', count)} in corpus ({self.corpus_id})."
71
62
  )
72
63
 
73
64
  @unsupported_cache
74
- def create_required_types(self, element_types: list[ElementType]):
75
- """Creates given element types in the corpus.
65
+ def create_element_type(
66
+ self, slug: str, name: str, is_folder: bool = False
67
+ ) -> None:
68
+ """
69
+ Create an element type on the given corpus.
76
70
 
77
- :param element_types: The missing element types to create.
71
+ :param slug: Slug of the element type.
72
+ :param name: Name of the element type.
73
+ :param is_folder: Whether an element with this type can contain other elements or not.
78
74
  """
79
- for element_type in element_types:
80
- self.api_client.request(
75
+ assert slug and isinstance(slug, str), (
76
+ "slug shouldn't be null and should be of type str"
77
+ )
78
+ assert name and isinstance(name, str), (
79
+ "name shouldn't be null and should be of type str"
80
+ )
81
+ assert is_folder is not None and isinstance(is_folder, bool), (
82
+ "is_folder shouldn't be null and should be of type bool"
83
+ )
84
+
85
+ try:
86
+ element_type = self.api_client.request(
81
87
  "CreateElementType",
82
88
  body={
83
- "slug": element_type.slug,
84
- "display_name": element_type.name,
85
- "folder": element_type.is_folder,
89
+ "slug": slug,
90
+ "display_name": name,
91
+ "folder": is_folder,
86
92
  "corpus": self.corpus_id,
87
93
  },
88
94
  )
89
- logger.info(f"Created a new element type with slug {element_type.slug}")
95
+ self.corpus_types[slug] = element_type
96
+ logger.info(f"Created a new element type with slug `{slug}`.")
97
+ except ErrorResponse as e:
98
+ # Only reload for 400 errors
99
+ if e.status_code != 400:
100
+ raise
101
+
102
+ # Reload and make sure we have the element type now
103
+ logger.warning(
104
+ f"Unable to create the element type `{slug}`. Refreshing corpus element types cache."
105
+ )
106
+ self.list_corpus_types()
107
+ assert slug in self.corpus_types, (
108
+ f"Missing element type `{slug}` even after refreshing."
109
+ )
90
110
 
91
111
  def check_required_types(
92
- self, *type_slugs: str, create_missing: bool = False
93
- ) -> bool:
112
+ self, type_slugs: list[str], create_missing: bool = False
113
+ ) -> None:
94
114
  """
95
- Check that a corpus has a list of required element types,
96
- and raise an exception if any of them are missing.
115
+ Check that every element type needed is available in the corpus.
116
+ Missing ones may be created automatically if needed.
97
117
 
98
- :param *type_slugs: Type slugs to look for.
99
- :param create_missing: Whether missing types should be created.
100
- :returns: Whether all of the specified type slugs have been found.
101
- :raises MissingTypeError: If any of the specified type slugs were not found.
118
+ :param type_slugs: Element type slugs to search.
119
+ :param create_missing: Whether the missing types should be created. Defaults to False.
120
+ :raises MissingElementType: When an entity type is missing and cannot be created.
102
121
  """
103
- assert len(type_slugs), "At least one element type slug is required."
104
- assert all(
105
- isinstance(slug, str) for slug in type_slugs
106
- ), "Element type slugs must be strings."
122
+ assert type_slugs and isinstance(type_slugs, list), (
123
+ "type_slugs shouldn't be null and should be of type list"
124
+ )
125
+
126
+ for index, slug in enumerate(type_slugs):
127
+ assert isinstance(slug, str), (
128
+ f"Element type at index {index} in type_slugs: Should be of type str"
129
+ )
130
+
131
+ assert create_missing is not None and isinstance(create_missing, bool), (
132
+ "create_missing shouldn't be null and should be of type bool"
133
+ )
107
134
 
108
135
  if not self.corpus_types:
109
136
  self.list_corpus_types()
110
137
 
111
- missing_slugs = set(type_slugs) - set(self.corpus_types)
112
- if missing_slugs:
113
- if create_missing:
114
- self.create_required_types(
115
- element_types=[
116
- ElementType(slug, slug, False) for slug in missing_slugs
117
- ],
118
- )
119
- else:
120
- raise MissingTypeError(
121
- f'Element {pluralize("type", len(missing_slugs))} {", ".join(sorted(missing_slugs))} were not found in corpus ({self.corpus_id}).'
138
+ for slug in type_slugs:
139
+ # Do nothing if the type already exists
140
+ if slug in self.corpus_types:
141
+ continue
142
+
143
+ # Do not create missing if not requested
144
+ if not create_missing:
145
+ raise MissingElementType(
146
+ f"Element type `{slug}` was not in the corpus."
122
147
  )
123
148
 
124
- return True
149
+ # Create the type if non-existent
150
+ self.create_element_type(slug=slug, name=slug)
125
151
 
126
152
  @unsupported_cache
127
153
  def create_sub_element(
@@ -146,18 +172,18 @@ class ElementMixin:
146
172
  :param slim_output: Whether to return the child ID or the full child.
147
173
  :returns: UUID of the created element.
148
174
  """
149
- assert element and isinstance(
150
- element, Element
151
- ), "element shouldn't be null and should be of type Element"
152
- assert type and isinstance(
153
- type, str
154
- ), "type shouldn't be null and should be of type str"
155
- assert name and isinstance(
156
- name, str
157
- ), "name shouldn't be null and should be of type str"
158
- assert polygon is None or isinstance(
159
- polygon, list
160
- ), "polygon should be None or a list"
175
+ assert element and isinstance(element, Element), (
176
+ "element shouldn't be null and should be of type Element"
177
+ )
178
+ assert type and isinstance(type, str), (
179
+ "type shouldn't be null and should be of type str"
180
+ )
181
+ assert name and isinstance(name, str), (
182
+ "name shouldn't be null and should be of type str"
183
+ )
184
+ assert polygon is None or isinstance(polygon, list), (
185
+ "polygon should be None or a list"
186
+ )
161
187
  if polygon is not None:
162
188
  assert len(polygon) >= 3, "polygon should have at least three points"
163
189
  assert all(
@@ -177,7 +203,9 @@ class ElementMixin:
177
203
  except ValueError as e:
178
204
  raise ValueError("image is not a valid uuid.") from e
179
205
  if polygon and image is None:
180
- assert element.zone, "An image or a parent with an image is required to create an element with a polygon."
206
+ assert element.zone, (
207
+ "An image or a parent with an image is required to create an element with a polygon."
208
+ )
181
209
  assert isinstance(slim_output, bool), "slim_output should be of type bool"
182
210
 
183
211
  if self.is_read_only:
@@ -231,56 +259,61 @@ class ElementMixin:
231
259
  :return: List of dicts, with each dict having a single key, ``id``, holding the UUID of each created element.
232
260
  """
233
261
  if isinstance(parent, Element):
234
- assert parent.get(
235
- "zone"
236
- ), "create_elements cannot be used on parents without zones"
262
+ assert parent.get("zone"), (
263
+ "create_elements cannot be used on parents without zones"
264
+ )
237
265
  elif isinstance(parent, CachedElement):
238
- assert (
239
- parent.image_id
240
- ), "create_elements cannot be used on parents without images"
266
+ assert parent.image_id, (
267
+ "create_elements cannot be used on parents without images"
268
+ )
241
269
  else:
242
270
  raise TypeError(
243
271
  "Parent element should be an Element or CachedElement instance"
244
272
  )
245
273
 
246
- assert elements and isinstance(
247
- elements, list
248
- ), "elements shouldn't be null and should be of type list"
274
+ assert elements and isinstance(elements, list), (
275
+ "elements shouldn't be null and should be of type list"
276
+ )
249
277
 
250
278
  for index, element in enumerate(elements):
251
- assert isinstance(
252
- element, dict
253
- ), f"Element at index {index} in elements: Should be of type dict"
279
+ assert isinstance(element, dict), (
280
+ f"Element at index {index} in elements: Should be of type dict"
281
+ )
254
282
 
255
283
  name = element.get("name")
256
- assert (
257
- name and isinstance(name, str)
258
- ), f"Element at index {index} in elements: name shouldn't be null and should be of type str"
284
+ assert name and isinstance(name, str), (
285
+ f"Element at index {index} in elements: name shouldn't be null and should be of type str"
286
+ )
259
287
 
260
288
  type = element.get("type")
261
- assert (
262
- type and isinstance(type, str)
263
- ), f"Element at index {index} in elements: type shouldn't be null and should be of type str"
289
+ assert type and isinstance(type, str), (
290
+ f"Element at index {index} in elements: type shouldn't be null and should be of type str"
291
+ )
264
292
 
265
293
  polygon = element.get("polygon")
266
- assert (
267
- polygon and isinstance(polygon, list)
268
- ), f"Element at index {index} in elements: polygon shouldn't be null and should be of type list"
269
- assert (
270
- len(polygon) >= 3
271
- ), f"Element at index {index} in elements: polygon should have at least three points"
294
+ assert polygon and isinstance(polygon, list), (
295
+ f"Element at index {index} in elements: polygon shouldn't be null and should be of type list"
296
+ )
297
+ assert len(polygon) >= 3, (
298
+ f"Element at index {index} in elements: polygon should have at least three points"
299
+ )
272
300
  assert all(
273
301
  isinstance(point, list) and len(point) == 2 for point in polygon
274
- ), f"Element at index {index} in elements: polygon points should be lists of two items"
302
+ ), (
303
+ f"Element at index {index} in elements: polygon points should be lists of two items"
304
+ )
275
305
  assert all(
276
306
  isinstance(coord, int | float) for point in polygon for coord in point
277
- ), f"Element at index {index} in elements: polygon points should be lists of two numbers"
307
+ ), (
308
+ f"Element at index {index} in elements: polygon points should be lists of two numbers"
309
+ )
278
310
 
279
311
  confidence = element.get("confidence")
280
- assert (
281
- confidence is None
282
- or (isinstance(confidence, float) and 0 <= confidence <= 1)
283
- ), f"Element at index {index} in elements: confidence should be None or a float in [0..1] range"
312
+ assert confidence is None or (
313
+ isinstance(confidence, float) and 0 <= confidence <= 1
314
+ ), (
315
+ f"Element at index {index} in elements: confidence should be None or a float in [0..1] range"
316
+ )
284
317
 
285
318
  if self.is_read_only:
286
319
  logger.warning("Cannot create elements as this worker is in read-only mode")
@@ -347,12 +380,12 @@ class ElementMixin:
347
380
  :param child: Child element.
348
381
  :returns: A dict from the ``CreateElementParent`` API endpoint.
349
382
  """
350
- assert parent and isinstance(
351
- parent, Element
352
- ), "parent shouldn't be null and should be of type Element"
353
- assert child and isinstance(
354
- child, Element
355
- ), "child shouldn't be null and should be of type Element"
383
+ assert parent and isinstance(parent, Element), (
384
+ "parent shouldn't be null and should be of type Element"
385
+ )
386
+ assert child and isinstance(child, Element), (
387
+ "child shouldn't be null and should be of type Element"
388
+ )
356
389
 
357
390
  if self.is_read_only:
358
391
  logger.warning("Cannot link elements as this worker is in read-only mode")
@@ -381,18 +414,18 @@ class ElementMixin:
381
414
 
382
415
  :returns: A list containing the string UUID of each child linked to the parent.
383
416
  """
384
- assert parent and isinstance(
385
- parent, Element
386
- ), "parent shouldn't be null and should be of type Element"
417
+ assert parent and isinstance(parent, Element), (
418
+ "parent shouldn't be null and should be of type Element"
419
+ )
387
420
 
388
- assert children and isinstance(
389
- children, list
390
- ), "children shouldn't be null and should be of type list"
421
+ assert children and isinstance(children, list), (
422
+ "children shouldn't be null and should be of type list"
423
+ )
391
424
 
392
425
  for index, child in enumerate(children):
393
- assert isinstance(
394
- child, Element
395
- ), f"Child at index {index} in children: Should be of type Element"
426
+ assert isinstance(child, Element), (
427
+ f"Child at index {index} in children: Should be of type Element"
428
+ )
396
429
 
397
430
  if self.is_read_only:
398
431
  logger.warning("Cannot link elements as this worker is in read-only mode")
@@ -430,9 +463,9 @@ class ElementMixin:
430
463
 
431
464
  :returns: A dict from the ``PartialUpdateElement`` API endpoint.
432
465
  """
433
- assert element and isinstance(
434
- element, Element | CachedElement
435
- ), "element shouldn't be null and should be an Element or CachedElement"
466
+ assert element and isinstance(element, Element | CachedElement), (
467
+ "element shouldn't be null and should be an Element or CachedElement"
468
+ )
436
469
 
437
470
  if "type" in kwargs:
438
471
  assert isinstance(kwargs["type"], str), "type should be a str"
@@ -459,9 +492,9 @@ class ElementMixin:
459
492
 
460
493
  if "rotation_angle" in kwargs:
461
494
  rotation_angle = kwargs["rotation_angle"]
462
- assert (
463
- isinstance(rotation_angle, int) and rotation_angle >= 0
464
- ), "rotation_angle should be a positive integer"
495
+ assert isinstance(rotation_angle, int) and rotation_angle >= 0, (
496
+ "rotation_angle should be a positive integer"
497
+ )
465
498
 
466
499
  if "mirrored" in kwargs:
467
500
  assert isinstance(kwargs["mirrored"], bool), "mirrored should be a boolean"
@@ -571,22 +604,22 @@ class ElementMixin:
571
604
  DeprecationWarning,
572
605
  stacklevel=1,
573
606
  )
574
- assert isinstance(
575
- transcription_worker_version, str | bool
576
- ), "transcription_worker_version should be of type str or bool"
607
+ assert isinstance(transcription_worker_version, str | bool), (
608
+ "transcription_worker_version should be of type str or bool"
609
+ )
577
610
  if isinstance(transcription_worker_version, bool):
578
- assert (
579
- transcription_worker_version is False
580
- ), "if of type bool, transcription_worker_version can only be set to False"
611
+ assert transcription_worker_version is False, (
612
+ "if of type bool, transcription_worker_version can only be set to False"
613
+ )
581
614
  query_params["transcription_worker_version"] = transcription_worker_version
582
615
  if transcription_worker_run is not None:
583
- assert isinstance(
584
- transcription_worker_run, str | bool
585
- ), "transcription_worker_run should be of type str or bool"
616
+ assert isinstance(transcription_worker_run, str | bool), (
617
+ "transcription_worker_run should be of type str or bool"
618
+ )
586
619
  if isinstance(transcription_worker_run, bool):
587
- assert (
588
- transcription_worker_run is False
589
- ), "if of type bool, transcription_worker_run can only be set to False"
620
+ assert transcription_worker_run is False, (
621
+ "if of type bool, transcription_worker_run can only be set to False"
622
+ )
590
623
  query_params["transcription_worker_run"] = transcription_worker_run
591
624
  if type:
592
625
  assert isinstance(type, str), "type should be of type str"
@@ -598,14 +631,14 @@ class ElementMixin:
598
631
  assert isinstance(with_corpus, bool), "with_corpus should be of type bool"
599
632
  query_params["with_corpus"] = with_corpus
600
633
  if with_has_children is not None:
601
- assert isinstance(
602
- with_has_children, bool
603
- ), "with_has_children should be of type bool"
634
+ assert isinstance(with_has_children, bool), (
635
+ "with_has_children should be of type bool"
636
+ )
604
637
  query_params["with_has_children"] = with_has_children
605
638
  if with_metadata is not None:
606
- assert isinstance(
607
- with_metadata, bool
608
- ), "with_metadata should be of type bool"
639
+ assert isinstance(with_metadata, bool), (
640
+ "with_metadata should be of type bool"
641
+ )
609
642
  query_params["with_metadata"] = with_metadata
610
643
  if with_zone is not None:
611
644
  assert isinstance(with_zone, bool), "with_zone should be of type bool"
@@ -616,22 +649,22 @@ class ElementMixin:
616
649
  DeprecationWarning,
617
650
  stacklevel=1,
618
651
  )
619
- assert isinstance(
620
- worker_version, str | bool
621
- ), "worker_version should be of type str or bool"
652
+ assert isinstance(worker_version, str | bool), (
653
+ "worker_version should be of type str or bool"
654
+ )
622
655
  if isinstance(worker_version, bool):
623
- assert (
624
- worker_version is False
625
- ), "if of type bool, worker_version can only be set to False"
656
+ assert worker_version is False, (
657
+ "if of type bool, worker_version can only be set to False"
658
+ )
626
659
  query_params["worker_version"] = worker_version
627
660
  if worker_run is not None:
628
- assert isinstance(
629
- worker_run, str | bool
630
- ), "worker_run should be of type str or bool"
661
+ assert isinstance(worker_run, str | bool), (
662
+ "worker_run should be of type str or bool"
663
+ )
631
664
  if isinstance(worker_run, bool):
632
- assert (
633
- worker_run is False
634
- ), "if of type bool, worker_run can only be set to False"
665
+ assert worker_run is False, (
666
+ "if of type bool, worker_run can only be set to False"
667
+ )
635
668
  query_params["worker_run"] = worker_run
636
669
 
637
670
  if not self.use_cache:
@@ -640,14 +673,13 @@ class ElementMixin:
640
673
  )
641
674
 
642
675
  # Checking that we only received query_params handled by the cache
643
- assert (
644
- set(query_params.keys())
645
- <= {
646
- "type",
647
- "worker_version",
648
- "worker_run",
649
- }
650
- ), "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
676
+ assert set(query_params.keys()) <= {
677
+ "type",
678
+ "worker_version",
679
+ "worker_run",
680
+ }, (
681
+ "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
682
+ )
651
683
 
652
684
  query = CachedElement.select()
653
685
  if type:
@@ -729,9 +761,9 @@ class ElementMixin:
729
761
  :return: An iterable of dicts from the ``ListElementChildren`` API endpoint,
730
762
  or an iterable of [CachedElement][arkindex_worker.cache.CachedElement] when caching is enabled.
731
763
  """
732
- assert element and isinstance(
733
- element, Element | CachedElement
734
- ), "element shouldn't be null and should be an Element or CachedElement"
764
+ assert element and isinstance(element, Element | CachedElement), (
765
+ "element shouldn't be null and should be an Element or CachedElement"
766
+ )
735
767
  query_params = {}
736
768
  if folder is not None:
737
769
  assert isinstance(folder, bool), "folder should be of type bool"
@@ -748,22 +780,22 @@ class ElementMixin:
748
780
  DeprecationWarning,
749
781
  stacklevel=1,
750
782
  )
751
- assert isinstance(
752
- transcription_worker_version, str | bool
753
- ), "transcription_worker_version should be of type str or bool"
783
+ assert isinstance(transcription_worker_version, str | bool), (
784
+ "transcription_worker_version should be of type str or bool"
785
+ )
754
786
  if isinstance(transcription_worker_version, bool):
755
- assert (
756
- transcription_worker_version is False
757
- ), "if of type bool, transcription_worker_version can only be set to False"
787
+ assert transcription_worker_version is False, (
788
+ "if of type bool, transcription_worker_version can only be set to False"
789
+ )
758
790
  query_params["transcription_worker_version"] = transcription_worker_version
759
791
  if transcription_worker_run is not None:
760
- assert isinstance(
761
- transcription_worker_run, str | bool
762
- ), "transcription_worker_run should be of type str or bool"
792
+ assert isinstance(transcription_worker_run, str | bool), (
793
+ "transcription_worker_run should be of type str or bool"
794
+ )
763
795
  if isinstance(transcription_worker_run, bool):
764
- assert (
765
- transcription_worker_run is False
766
- ), "if of type bool, transcription_worker_run can only be set to False"
796
+ assert transcription_worker_run is False, (
797
+ "if of type bool, transcription_worker_run can only be set to False"
798
+ )
767
799
  query_params["transcription_worker_run"] = transcription_worker_run
768
800
  if type:
769
801
  assert isinstance(type, str), "type should be of type str"
@@ -775,14 +807,14 @@ class ElementMixin:
775
807
  assert isinstance(with_corpus, bool), "with_corpus should be of type bool"
776
808
  query_params["with_corpus"] = with_corpus
777
809
  if with_has_children is not None:
778
- assert isinstance(
779
- with_has_children, bool
780
- ), "with_has_children should be of type bool"
810
+ assert isinstance(with_has_children, bool), (
811
+ "with_has_children should be of type bool"
812
+ )
781
813
  query_params["with_has_children"] = with_has_children
782
814
  if with_metadata is not None:
783
- assert isinstance(
784
- with_metadata, bool
785
- ), "with_metadata should be of type bool"
815
+ assert isinstance(with_metadata, bool), (
816
+ "with_metadata should be of type bool"
817
+ )
786
818
  query_params["with_metadata"] = with_metadata
787
819
  if with_zone is not None:
788
820
  assert isinstance(with_zone, bool), "with_zone should be of type bool"
@@ -793,22 +825,22 @@ class ElementMixin:
793
825
  DeprecationWarning,
794
826
  stacklevel=1,
795
827
  )
796
- assert isinstance(
797
- worker_version, str | bool
798
- ), "worker_version should be of type str or bool"
828
+ assert isinstance(worker_version, str | bool), (
829
+ "worker_version should be of type str or bool"
830
+ )
799
831
  if isinstance(worker_version, bool):
800
- assert (
801
- worker_version is False
802
- ), "if of type bool, worker_version can only be set to False"
832
+ assert worker_version is False, (
833
+ "if of type bool, worker_version can only be set to False"
834
+ )
803
835
  query_params["worker_version"] = worker_version
804
836
  if worker_run is not None:
805
- assert isinstance(
806
- worker_run, str | bool
807
- ), "worker_run should be of type str or bool"
837
+ assert isinstance(worker_run, str | bool), (
838
+ "worker_run should be of type str or bool"
839
+ )
808
840
  if isinstance(worker_run, bool):
809
- assert (
810
- worker_run is False
811
- ), "if of type bool, worker_run can only be set to False"
841
+ assert worker_run is False, (
842
+ "if of type bool, worker_run can only be set to False"
843
+ )
812
844
  query_params["worker_run"] = worker_run
813
845
 
814
846
  if not self.use_cache:
@@ -817,14 +849,13 @@ class ElementMixin:
817
849
  )
818
850
 
819
851
  # Checking that we only received query_params handled by the cache
820
- assert (
821
- set(query_params.keys())
822
- <= {
823
- "type",
824
- "worker_version",
825
- "worker_run",
826
- }
827
- ), "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
852
+ assert set(query_params.keys()) <= {
853
+ "type",
854
+ "worker_version",
855
+ "worker_run",
856
+ }, (
857
+ "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
858
+ )
828
859
 
829
860
  query = CachedElement.select().where(CachedElement.parent_id == element.id)
830
861
  if type:
@@ -906,9 +937,9 @@ class ElementMixin:
906
937
  :return: An iterable of dicts from the ``ListElementParents`` API endpoint,
907
938
  or an iterable of [CachedElement][arkindex_worker.cache.CachedElement] when caching is enabled.
908
939
  """
909
- assert element and isinstance(
910
- element, Element | CachedElement
911
- ), "element shouldn't be null and should be an Element or CachedElement"
940
+ assert element and isinstance(element, Element | CachedElement), (
941
+ "element shouldn't be null and should be an Element or CachedElement"
942
+ )
912
943
  query_params = {}
913
944
  if folder is not None:
914
945
  assert isinstance(folder, bool), "folder should be of type bool"
@@ -925,22 +956,22 @@ class ElementMixin:
925
956
  DeprecationWarning,
926
957
  stacklevel=1,
927
958
  )
928
- assert isinstance(
929
- transcription_worker_version, str | bool
930
- ), "transcription_worker_version should be of type str or bool"
959
+ assert isinstance(transcription_worker_version, str | bool), (
960
+ "transcription_worker_version should be of type str or bool"
961
+ )
931
962
  if isinstance(transcription_worker_version, bool):
932
- assert (
933
- transcription_worker_version is False
934
- ), "if of type bool, transcription_worker_version can only be set to False"
963
+ assert transcription_worker_version is False, (
964
+ "if of type bool, transcription_worker_version can only be set to False"
965
+ )
935
966
  query_params["transcription_worker_version"] = transcription_worker_version
936
967
  if transcription_worker_run is not None:
937
- assert isinstance(
938
- transcription_worker_run, str | bool
939
- ), "transcription_worker_run should be of type str or bool"
968
+ assert isinstance(transcription_worker_run, str | bool), (
969
+ "transcription_worker_run should be of type str or bool"
970
+ )
940
971
  if isinstance(transcription_worker_run, bool):
941
- assert (
942
- transcription_worker_run is False
943
- ), "if of type bool, transcription_worker_run can only be set to False"
972
+ assert transcription_worker_run is False, (
973
+ "if of type bool, transcription_worker_run can only be set to False"
974
+ )
944
975
  query_params["transcription_worker_run"] = transcription_worker_run
945
976
  if type:
946
977
  assert isinstance(type, str), "type should be of type str"
@@ -952,14 +983,14 @@ class ElementMixin:
952
983
  assert isinstance(with_corpus, bool), "with_corpus should be of type bool"
953
984
  query_params["with_corpus"] = with_corpus
954
985
  if with_has_children is not None:
955
- assert isinstance(
956
- with_has_children, bool
957
- ), "with_has_children should be of type bool"
986
+ assert isinstance(with_has_children, bool), (
987
+ "with_has_children should be of type bool"
988
+ )
958
989
  query_params["with_has_children"] = with_has_children
959
990
  if with_metadata is not None:
960
- assert isinstance(
961
- with_metadata, bool
962
- ), "with_metadata should be of type bool"
991
+ assert isinstance(with_metadata, bool), (
992
+ "with_metadata should be of type bool"
993
+ )
963
994
  query_params["with_metadata"] = with_metadata
964
995
  if with_zone is not None:
965
996
  assert isinstance(with_zone, bool), "with_zone should be of type bool"
@@ -970,22 +1001,22 @@ class ElementMixin:
970
1001
  DeprecationWarning,
971
1002
  stacklevel=1,
972
1003
  )
973
- assert isinstance(
974
- worker_version, str | bool
975
- ), "worker_version should be of type str or bool"
1004
+ assert isinstance(worker_version, str | bool), (
1005
+ "worker_version should be of type str or bool"
1006
+ )
976
1007
  if isinstance(worker_version, bool):
977
- assert (
978
- worker_version is False
979
- ), "if of type bool, worker_version can only be set to False"
1008
+ assert worker_version is False, (
1009
+ "if of type bool, worker_version can only be set to False"
1010
+ )
980
1011
  query_params["worker_version"] = worker_version
981
1012
  if worker_run is not None:
982
- assert isinstance(
983
- worker_run, str | bool
984
- ), "worker_run should be of type str or bool"
1013
+ assert isinstance(worker_run, str | bool), (
1014
+ "worker_run should be of type str or bool"
1015
+ )
985
1016
  if isinstance(worker_run, bool):
986
- assert (
987
- worker_run is False
988
- ), "if of type bool, worker_run can only be set to False"
1017
+ assert worker_run is False, (
1018
+ "if of type bool, worker_run can only be set to False"
1019
+ )
989
1020
  query_params["worker_run"] = worker_run
990
1021
 
991
1022
  if not self.use_cache:
@@ -994,14 +1025,13 @@ class ElementMixin:
994
1025
  )
995
1026
 
996
1027
  # Checking that we only received query_params handled by the cache
997
- assert (
998
- set(query_params.keys())
999
- <= {
1000
- "type",
1001
- "worker_version",
1002
- "worker_run",
1003
- }
1004
- ), "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
1028
+ assert set(query_params.keys()) <= {
1029
+ "type",
1030
+ "worker_version",
1031
+ "worker_run",
1032
+ }, (
1033
+ "When using the local cache, you can only filter by 'type' and/or 'worker_version' and/or 'worker_run'"
1034
+ )
1005
1035
 
1006
1036
  parent_ids = CachedElement.select(CachedElement.parent_id).where(
1007
1037
  CachedElement.id == element.id