Skip to content

Commit 2f92617

Browse files
committed
Fix multiprocessing pickling error
1 parent 8c48d48 commit 2f92617

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

src/annotations/annotations.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def __hash__(self):
120120
return hash((self.term, self.modifiers))
121121

122122

123+
def _annotation_factory(a_str: str, *args):
124+
return Annotation(a_str)
125+
126+
123127
class AnnotationCollection(Set):
124128
"""Object that parses and processes localisation (or other) annotation
125129
strings of the form ".
@@ -130,8 +134,9 @@ def __init__(
130134
self,
131135
annotations_string: str,
132136
annotation_factory: Callable[[str], Annotation] = (
133-
lambda a_str: Annotation(a_str)
137+
_annotation_factory
134138
),
139+
annotation_factory_extra_args: tuple = (),
135140
):
136141
"""Creates a new `AnnotationCollection` from the given annotation
137142
string.
@@ -147,13 +152,14 @@ def __init__(
147152
"""
148153
if annotations_string != "":
149154
self._annotations = frozenset([
150-
annotation_factory(a_str)
155+
annotation_factory(a_str, *annotation_factory_extra_args)
151156
for a_str in ANNOT_SPLIT_RE.split(annotations_string)
152157
])
153158
else:
154159
self._annotations = frozenset([])
155160

156161
self._annotation_factory = annotation_factory
162+
self._annotation_factory_extra_args = annotation_factory_extra_args
157163

158164
def __contains__(self, item: str | Annotation):
159165
"""Returns whether the annotation collection contains the given
@@ -165,7 +171,8 @@ def __contains__(self, item: str | Annotation):
165171
converted to an `Annotation` object.
166172
"""
167173
if isinstance(item, str):
168-
annot = self._annotation_factory(item)
174+
annot = self._annotation_factory(
175+
item, *self._annotation_factory_extra_args)
169176
else:
170177
annot = item
171178
term_re = re.compile(annot.term)

src/annotations/ontology.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def _match_term(self, term, recursive: bool = True):
108108
return self.ontology_entry.match_term(term, recursive=recursive)
109109

110110

111+
def _ontology_annotation_factory(a_str: str, ontology: Ontology):
112+
return OntologyAnnotation(a_str, ontology)
113+
114+
111115
class OntologyAnnotationCollection(AnnotationCollection):
112116
"""Annotation collection that is connected to an annotation ontology.
113117
"""
@@ -122,7 +126,8 @@ def __init__(self, annotations_string: str, ontology: Ontology):
122126
self.ontology = ontology
123127
super().__init__(
124128
annotations_string,
125-
lambda a_str: OntologyAnnotation(a_str, ontology)
129+
_ontology_annotation_factory,
130+
(ontology,),
126131
)
127132

128133
def __and__(self, other: OntologyAnnotationCollection):

0 commit comments

Comments
 (0)