oversampleqa.plugin_system¶
oversampleqa.plugin_system
¶
Simple plugin management for metrics and validators.
PluginManager
¶
Runtime registry for pluggable components.
Source code in src/oversampleqa/plugin_system.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
register_metric(name, metric_cls, *, domain='real', check_axioms=True, metric_kwargs=None)
¶
Register a metric class by name, after checking it behaves like one.
Registration used to be a bare dictionary assignment: any object could be registered under any name, silently replacing a built-in, and a metric that violated the axioms would be discovered only by whoever eventually distrusted its numbers.
Three checks now run, each raising :class:~oversampleqa.PluginError:
- Name collision. A name already taken by a built-in or another plugin is refused rather than overwritten.
- Signature. The callable must accept two positional arguments.
- Axioms.
d(x, x) == 0,d(x, y) > 0for distinct points, symmetry, non-negativity and finiteness on random input.
The third is not hypothetical. The built-in hassanat shipped for
this project's entire history scoring [-5] and [5] as distance
zero, because it compared absolute values -- it was not a metric, and
nothing checked. This is that check.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric identifier. |
required |
metric_cls
|
type[DistanceMetricProtocol]
|
Metric callable or class implementing |
required |
domain
|
MetricDomain
|
Input the metric is defined on. See
:data: |
'real'
|
check_axioms
|
bool
|
Run the axiom smoke check. Disable only for a metric you have verified another way, and say why. |
True
|
metric_kwargs
|
dict[str, Any] | None
|
Extra arguments the metric needs, such as
|
None
|
Raises:
| Type | Description |
|---|---|
PluginError
|
On collision, bad signature, or axiom failure. |
Source code in src/oversampleqa/plugin_system.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
unregister_metric(name)
¶
Remove a registered metric plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric identifier. |
required |
Raises:
| Type | Description |
|---|---|
PluginError
|
If no plugin is registered under that name. |
Source code in src/oversampleqa/plugin_system.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
register_validator(name, validator_cls)
¶
Register a validator class by name, refusing collisions.
This used to be a bare dictionary assignment, so two plugins claiming the same name left whichever loaded last in place and said nothing -- and with entry-point discovery the load order is not something either author controls. The metric path already refused collisions; this makes the two consistent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Validator identifier. |
required |
validator_cls
|
type[ValidatorProtocol]
|
Validator class implementing |
required |
Raises:
| Type | Description |
|---|---|
PluginError
|
If the name is taken, or the class has no callable
|
Source code in src/oversampleqa/plugin_system.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
unregister_validator(name)
¶
Remove a registered validator plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Validator identifier. |
required |
Raises:
| Type | Description |
|---|---|
PluginError
|
If no plugin is registered under that name. |
Source code in src/oversampleqa/plugin_system.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
get_metric(name)
¶
Retrieve a registered metric class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric identifier. |
required |
Returns:
| Type | Description |
|---|---|
type[DistanceMetricProtocol]
|
Registered metric class. |
Source code in src/oversampleqa/plugin_system.py
174 175 176 177 178 179 180 181 182 183 184 185 | |
get_validator(name)
¶
Retrieve a registered validator class by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Validator identifier. |
required |
Returns:
| Type | Description |
|---|---|
type[ValidatorProtocol]
|
Registered validator class. |
Source code in src/oversampleqa/plugin_system.py
187 188 189 190 191 192 193 194 195 196 197 198 | |
discover_plugins(package='oversampleqa_plugins')
¶
Discover plugins within a namespace package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package
|
str
|
Namespace package to scan. |
'oversampleqa_plugins'
|
Source code in src/oversampleqa/plugin_system.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
discover_entry_points(*, metric_group=METRIC_ENTRY_POINT_GROUP, validator_group=VALIDATOR_ENTRY_POINT_GROUP, strict=False)
¶
Register metrics and validators advertised by installed packages.
This is the discovery mechanism a third-party package should use: it
needs no import of a magic namespace package and no scan of the
filesystem, only an entry point in its own metadata. See
examples/plugins/ for a worked example.
A plugin that fails to import, or that fails the axiom check, does not prevent the others from loading. Each failure raises a warning naming the entry point and the reason, because a plugin that quietly fails to register looks exactly like a plugin that was never installed, and the difference is an afternoon of confusion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_group
|
str
|
Entry-point group scanned for metrics. |
METRIC_ENTRY_POINT_GROUP
|
validator_group
|
str
|
Entry-point group scanned for validators. |
VALIDATOR_ENTRY_POINT_GROUP
|
strict
|
bool
|
Raise on the first failure instead of warning and continuing. Use in tests, where a plugin that silently fails to load would make the suite pass for the wrong reason. |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Names successfully registered, metrics and validators together. |
Raises:
| Type | Description |
|---|---|
PluginError
|
If |
Source code in src/oversampleqa/plugin_system.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
CustomEuclideanMetric
¶
Example metric plugin.
Source code in src/oversampleqa/plugin_system.py
401 402 403 404 405 406 | |
register_metric(name)
¶
Decorator to register a metric plugin by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric identifier. |
required |
Source code in src/oversampleqa/plugin_system.py
373 374 375 376 377 378 379 380 381 382 383 384 | |
register_validator(name)
¶
Decorator to register a validator plugin by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Validator identifier. |
required |
Source code in src/oversampleqa/plugin_system.py
387 388 389 390 391 392 393 394 395 396 397 398 | |