14
14
15
15
from guidellm .utils .auto_importer import AutoImporterMixin
16
16
17
- __all__ = ["RegistryMixin" , "RegistryObjT" ]
17
+ __all__ = ["RegisterT" , " RegistryMixin" , "RegistryObjT" ]
18
18
19
19
20
20
RegistryObjT = TypeVar ("RegistryObjT" , bound = Any )
21
- """
22
- Generic type variable for objects managed by the registry system.
23
- """
21
+ """Generic type variable for objects managed by the registry system."""
22
+ RegisterT = TypeVar ( "RegisterT" , bound = RegistryObjT )
23
+ """Generic type variable for the args and return values within the registry."""
24
24
25
25
26
26
class RegistryMixin (Generic [RegistryObjT ], AutoImporterMixin ):
27
27
"""
28
28
Generic mixin for creating object registries with optional auto-discovery.
29
29
30
- Enables classes to maintain separate registries of objects that can be
31
- dynamically discovered and instantiated through decorators and module imports.
32
- Supports both manual registration via decorators and automatic discovery
33
- through package scanning for extensible plugin architectures.
30
+ Enables classes to maintain separate registries of objects that can be dynamically
31
+ discovered and instantiated through decorators and module imports. Supports both
32
+ manual registration via decorators and automatic discovery through package scanning
33
+ for extensible plugin architectures.
34
34
35
35
Example:
36
36
::
@@ -69,14 +69,14 @@ class TokenProposal(RegistryMixin):
69
69
@classmethod
70
70
def register (
71
71
cls , name : str | list [str ] | None = None
72
- ) -> Callable [[RegistryObjT ], RegistryObjT ]:
72
+ ) -> Callable [[RegisterT ], RegisterT ]:
73
73
"""
74
- Decorator that registers an object with the registry.
74
+ Decorator for registering objects with the registry.
75
75
76
76
:param name: Optional name(s) to register the object under.
77
- If None, the object name is used as the registry key.
78
- :return: A decorator function that registers the decorated object.
79
- :raises ValueError: If name is provided but is not a string or list of strings.
77
+ If None, uses the object's __name__ attribute
78
+ :return: Decorator function that registers the decorated object
79
+ :raises ValueError: If name is not a string, list of strings, or None
80
80
"""
81
81
if name is not None and not isinstance (name , (str , list )):
82
82
raise ValueError (
@@ -88,19 +88,19 @@ def register(
88
88
89
89
@classmethod
90
90
def register_decorator (
91
- cls , obj : RegistryObjT , name : str | list [str ] | None = None
92
- ) -> RegistryObjT :
91
+ cls , obj : RegisterT , name : str | list [str ] | None = None
92
+ ) -> RegisterT :
93
93
"""
94
- Direct decorator that registers an object with the registry.
94
+ Register an object directly with the registry.
95
95
96
- :param obj: The object to register.
96
+ :param obj: The object to register
97
97
:param name: Optional name(s) to register the object under.
98
- If None, the object name is used as the registry key.
99
- :return: The registered object.
100
- :raises ValueError: If the object is already registered or if name is invalid.
98
+ If None, uses the object's __name__ attribute
99
+ :return: The registered object
100
+ :raises ValueError: If the object is already registered or name is invalid
101
101
"""
102
102
103
- if not name :
103
+ if name is None :
104
104
name = obj .__name__
105
105
elif not isinstance (name , (str , list )):
106
106
raise ValueError (
@@ -127,20 +127,20 @@ def register_decorator(
127
127
"registered."
128
128
)
129
129
130
- cls .registry [register_name . lower () ] = obj
130
+ cls .registry [register_name ] = obj
131
131
132
132
return obj
133
133
134
134
@classmethod
135
135
def auto_populate_registry (cls ) -> bool :
136
136
"""
137
- Import and register all modules from the specified auto_package.
137
+ Import and register all modules from the auto_package.
138
138
139
139
Automatically called by registered_objects when registry_auto_discovery is True
140
- to ensure all available implementations are discovered before returning results .
140
+ to ensure all available implementations are discovered.
141
141
142
- :return: True if the registry was populated, False if already populated.
143
- :raises ValueError: If called when registry_auto_discovery is False.
142
+ :return: True if registry was populated, False if already populated
143
+ :raises ValueError: If called when registry_auto_discovery is False
144
144
"""
145
145
if not cls .registry_auto_discovery :
146
146
raise ValueError (
@@ -165,8 +165,8 @@ def registered_objects(cls) -> tuple[RegistryObjT, ...]:
165
165
Automatically triggers auto-discovery if registry_auto_discovery is enabled
166
166
to ensure all available implementations are included.
167
167
168
- :return: Tuple of all registered objects including auto-discovered ones.
169
- :raises ValueError: If called before any objects have been registered.
168
+ :return: Tuple of all registered objects including auto-discovered ones
169
+ :raises ValueError: If called before any objects have been registered
170
170
"""
171
171
if cls .registry_auto_discovery :
172
172
cls .auto_populate_registry ()
@@ -183,24 +183,33 @@ def registered_objects(cls) -> tuple[RegistryObjT, ...]:
183
183
def is_registered (cls , name : str ) -> bool :
184
184
"""
185
185
Check if an object is registered under the given name.
186
+ It matches first by exact name, then by str.lower().
186
187
187
188
:param name: The name to check for registration.
188
189
:return: True if the object is registered, False otherwise.
189
190
"""
190
191
if cls .registry is None :
191
192
return False
192
193
193
- return name .lower () in cls .registry
194
+ return name in cls .registry or name .lower () in [
195
+ key .lower () for key in cls .registry
196
+ ]
194
197
195
198
@classmethod
196
199
def get_registered_object (cls , name : str ) -> RegistryObjT | None :
197
200
"""
198
- Get a registered object by its name.
201
+ Get a registered object by its name. It matches first by exact name,
202
+ then by str.lower().
199
203
200
204
:param name: The name of the registered object.
201
205
:return: The registered object if found, None otherwise.
202
206
"""
203
207
if cls .registry is None :
204
208
return None
205
209
206
- return cls .registry .get (name .lower ())
210
+ if name in cls .registry :
211
+ return cls .registry [name ]
212
+
213
+ lower_key_map = {key .lower (): key for key in cls .registry }
214
+
215
+ return cls .registry .get (lower_key_map .get (name .lower ()))
0 commit comments