@@ -142,12 +142,16 @@ def register_event_schema(self, schema: SchemaType) -> None:
142
142
143
143
Get this registered schema using the EventLogger.schema.get() method.
144
144
"""
145
-
146
145
event_schema = self .schemas .register (schema ) # type:ignore[arg-type]
147
146
key = event_schema .id
148
- self ._modifiers [key ] = set ()
149
- self ._modified_listeners [key ] = set ()
150
- self ._unmodified_listeners [key ] = set ()
147
+ # It's possible that listeners and modifiers have been added for this
148
+ # schema before the schema is registered.
149
+ if key not in self ._modifiers :
150
+ self ._modifiers [key ] = set ()
151
+ if key not in self ._modified_listeners :
152
+ self ._modified_listeners [key ] = set ()
153
+ if key not in self ._unmodified_listeners :
154
+ self ._unmodified_listeners [key ] = set ()
151
155
152
156
def register_handler (self , handler : logging .Handler ) -> None :
153
157
"""Register a new logging handler to the Event Logger.
@@ -205,7 +209,11 @@ def add_modifier(
205
209
# If the schema ID and version is given, only add
206
210
# this modifier to that schema
207
211
if schema_id :
208
- self ._modifiers [schema_id ].add (modifier )
212
+ # If the schema hasn't been added yet,
213
+ # start a placeholder set.
214
+ modifiers = self ._modifiers .get (schema_id , set ())
215
+ modifiers .add (modifier )
216
+ self ._modifiers [schema_id ] = modifiers
209
217
return
210
218
for id_ in self ._modifiers :
211
219
if schema_id is None or id_ == schema_id :
@@ -264,9 +272,16 @@ def add_listener(
264
272
# this modifier to that schema
265
273
if schema_id :
266
274
if modified :
267
- self ._modified_listeners [schema_id ].add (listener )
275
+ # If the schema hasn't been added yet,
276
+ # start a placeholder set.
277
+ listeners = self ._modified_listeners .get (schema_id , set ())
278
+ listeners .add (listener )
279
+ self ._modified_listeners [schema_id ] = listeners
268
280
return
269
- self ._unmodified_listeners [schema_id ].add (listener )
281
+ listeners = self ._unmodified_listeners .get (schema_id , set ())
282
+ listeners .add (listener )
283
+ self ._unmodified_listeners [schema_id ] = listeners
284
+ return
270
285
for id_ in self .schemas .schema_ids :
271
286
if schema_id is None or id_ == schema_id :
272
287
if modified :
0 commit comments