22
33import logging
44import types
5+ from collections import UserDict
56from typing import TYPE_CHECKING , TypeVar
67
78from .._abc import Instrument
@@ -21,7 +22,7 @@ def _public(fn: T) -> T:
2122 return fn
2223
2324
24- class Instruments (dict [str , dict [Instrument , None ]]):
25+ class Instruments (UserDict [str , dict [Instrument , None ]]):
2526 """A collection of `trio.abc.Instrument` organized by hook.
2627
2728 Instrumentation calls are rather expensive, and we don't want a
@@ -34,7 +35,7 @@ class Instruments(dict[str, dict[Instrument, None]]):
3435 __slots__ = ()
3536
3637 def __init__ (self , incoming : Sequence [Instrument ]) -> None :
37- self [ "_all" ] = {}
38+ super (). __init__ ({ "_all" : {}})
3839 for instrument in incoming :
3940 self .add_instrument (instrument )
4041
@@ -48,9 +49,9 @@ def add_instrument(self, instrument: Instrument) -> None:
4849 If ``instrument`` is already active, does nothing.
4950
5051 """
51- if instrument in self ["_all" ]:
52+ if instrument in self . data ["_all" ]:
5253 return
53- self ["_all" ][instrument ] = None
54+ self . data ["_all" ][instrument ] = None
5455 try :
5556 for name in dir (instrument ):
5657 if name .startswith ("_" ):
@@ -63,7 +64,7 @@ def add_instrument(self, instrument: Instrument) -> None:
6364 if isinstance (impl , types .MethodType ) and impl .__func__ is prototype :
6465 # Inherited unchanged from _abc.Instrument
6566 continue
66- self .setdefault (name , {})[instrument ] = None
67+ self .data . setdefault (name , {})[instrument ] = None
6768 except :
6869 self .remove_instrument (instrument )
6970 raise
@@ -83,12 +84,12 @@ def remove_instrument(self, instrument: Instrument) -> None:
8384
8485 """
8586 # If instrument isn't present, the KeyError propagates out
86- self ["_all" ].pop (instrument )
87- for hookname , instruments in list (self .items ()):
87+ self . data ["_all" ].pop (instrument )
88+ for hookname , instruments in list (self .data . items ()):
8889 if instrument in instruments :
8990 del instruments [instrument ]
9091 if not instruments :
91- del self [hookname ]
92+ del self . data [hookname ]
9293
9394 def call (
9495 self ,
@@ -103,7 +104,7 @@ def call(
103104 if "before_task_step" in instruments:
104105 instruments.call("before_task_step", task)
105106 """
106- for instrument in list (self [hookname ]):
107+ for instrument in list (self . data [hookname ]):
107108 try :
108109 getattr (instrument , hookname )(* args )
109110 except BaseException :
0 commit comments