disdantic.registry¶
Provides isolated registry mixins for dynamic polymorphic schemas.
The module implements registry spaces using RegistryMixin and
PydanticClassRegistryMixin which prevent namespace conflicts between
distinct domain models. Subclass tracking is decoupled across registrations
to enforce safe, independent type lookup structures. The RegistryManager
class orchestrates global discovery, query operations, and class indexing.
Through integration with Pydantic's core validation layer, this architecture enables automatic sub-package scanning and schema rebuilding. Registered models are seamlessly resolved during JSON validation using a customizable discriminator key, handling case-insensitive fallbacks and dynamic type dispatching safely.
PydanticClassRegistryMixin
¶
Bases: ReloadableBaseModel, RegistryMixin[type[BaseModelT]], ABC, Generic[BaseModelT]
Polymorphic serialization wrapper using dynamic tagged unions.
This class enables dynamic Pydantic model registration and builds tagged union schemas. It automatically routes JSON validation to the correct subclass using a discriminator key.
Example
.. code-block:: python
from disdantic.registry import PydanticClassRegistryMixin
from pydantic import BaseModel
class BaseMessage(PydanticClassRegistryMixin):
pass
@BaseMessage.register("text")
class TextMessage(BaseMessage):
content: str
:var schema_discriminator: The serialized tag field name used to identify the target model type.
Source code in src/disdantic/registry.py
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
schema_discriminator = 'model_type'
class-attribute
¶
The serialized tag field name used to identify the target model type.
get_schema_discriminator()
classmethod
¶
Retrieve the active schema discriminator key.
Resolves to the class-level 'schema_discriminator' or falls back to the global settings default.
:returns: The string key name of the discriminator.
Source code in src/disdantic/registry.py
registered_classes()
classmethod
¶
Return all registered BaseModel subclasses in this registry.
Triggers auto-discovery on access if enabled.
Example
.. code-block:: python
classes = BaseMessage.registered_classes()
:raises ValueError: If no classes are registered in the registry. :returns: A tuple of registered subclass types.
Source code in src/disdantic/registry.py
RegistryManager
¶
Orchestrate tracking and listing active registry namespaces globally.
This class scans the runtime subclass tree of RegistryMixin, discovers registered classes, and generates maps linking registries to their registered components.
Example
.. code-block:: python
from disdantic.registry import RegistryManager
registries = RegistryManager.list_registries()
Source code in src/disdantic/registry.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | |
list_registries()
classmethod
¶
Generate a nested map of all active registries and their contents.
Scans the runtime subclass tree of RegistryMixin, discovers registered classes, and generates maps linking registries to their registered components.
Example
.. code-block:: python
mapping = RegistryManager.list_registries()
:returns: A dictionary mapping registry class names to a nested dict of registered keys and target class paths.
Source code in src/disdantic/registry.py
RegistryMixin
¶
Bases: Generic[RegistryObjT], AutoImporterMixin
Isolated registry namespace tracking across distinct base class domains.
This mixin provides base class structures with automated tracking mappings, enabling subclasses to map dynamic components and discover submodules recursively. Each registry is fully isolated to avoid leakage between unrelated interfaces. It supports registration decorators, lookup checks, and programmatic unregistration.
Example
.. code-block:: python
from disdantic.registry import RegistryMixin
class ComponentRegistry(RegistryMixin[type]):
pass
@ComponentRegistry.register("service")
class MyService:
pass
:var registry: The canonical tracking directory mapping identifiers to registered objects. :var registry_auto_discovery: Controls whether sub-module scanning is automatically triggered on access. :var registry_populated: Tracks if the auto-population routine has completed for this namespace.
Source code in src/disdantic/registry.py
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
registry
class-attribute
¶
The canonical tracking directory mapping identifiers to registered objects.
registry_auto_discovery = False
class-attribute
¶
Controls whether sub-module scanning is automatically triggered on access.
registry_populated = False
class-attribute
¶
Tracks if the auto-population routine has completed for this namespace.
auto_populate_registry()
classmethod
¶
Scan packages and automatically populate the registry namespace.
Discovers and imports modules dynamically if auto-discovery is enabled and the registry has not been populated yet.
Example
.. code-block:: python
MyRegistry.auto_populate_registry()
:raises ValueError: If auto-discovery is disabled on the registry class. :returns: True if the registry was newly populated, False if it was already populated.
Source code in src/disdantic/registry.py
clear_registry()
classmethod
¶
Clear all active registrations and reset import states.
Clears all mappings in canonical and lowercase registries, and resets flags so that subsequent operations can re-populate the registry.
Example
.. code-block:: python
MyRegistry.clear_registry()
:returns: None.
Source code in src/disdantic/registry.py
get_registered_object(name)
classmethod
¶
Look up a registered object by its identifier.
Looks up the given name directly in the canonical registry, falling back to a case-insensitive lookup in the lowercase registry.
Example
.. code-block:: python
obj = MyRegistry.get_registered_object("custom_name")
:param name: The identifier key of the registered object. :returns: The registered object if found, or None.
Source code in src/disdantic/registry.py
is_auto_discovery_enabled()
classmethod
¶
Determine if automatic module discovery is active for this registry.
Checks both the class-level configuration option and the fallback global settings to decide if submodules should be scanned.
:returns: True if auto-discovery is enabled, False otherwise.
Source code in src/disdantic/registry.py
is_registered(name)
classmethod
¶
Verify presence of an identifier in the registry.
Performs case-insensitive checks against the registered keys.
Example
.. code-block:: python
exists = MyRegistry.is_registered("custom_name")
:param name: The identifier key to verify. :returns: True if the identifier is registered, False otherwise.
Source code in src/disdantic/registry.py
register(name=None)
classmethod
¶
Decorate subclass implementations to register them under name keys.
Registers the decorated class or object under the specified name or list of names. If no name is provided, uses the class name.
Example
.. code-block:: python
@MyRegistry.register("custom_name")
class SubComponent:
pass
:param name: Optional registration keys. Can be a single string, a sequence of strings, or None to default to the target's name. :returns: A decorator function that registers the target object.
Source code in src/disdantic/registry.py
register_decorator(target_object, name=None)
classmethod
¶
Index target objects directly into namespace mappings.
Performs collision checking to prevent duplicate registration within the same namespace.
Example
.. code-block:: python
MyRegistry.register_decorator(MyService, name="service")
:param target_object: The class or object instance to register. :param name: Optional registration keys. Can be a single string, a sequence of strings, or None to default to the target's name. :raises ValueError: If the naming format is unsupported or if a key is not a string. :raises RegistryCollisionError: If a key is already registered. :returns: The original target object after successful registration.
Source code in src/disdantic/registry.py
registered_objects()
classmethod
¶
Retrieve all registered objects in the registry tracking frame.
Triggers auto-population if auto-discovery is enabled before returning the objects.
Example
.. code-block:: python
objects = MyRegistry.registered_objects()
:returns: A tuple of all registered objects.
Source code in src/disdantic/registry.py
unregister(name)
classmethod
¶
Remove a registered identifier from both tracking mappings.
Removes from canonical and case-insensitive mapping caches. If it is a PydanticClassRegistryMixin, triggers a core schema rebuild of the base registry class and its registered hierarchy.
Example
.. code-block:: python
MyRegistry.unregister("custom_name")
:param name: The registered identifier/token to remove. :raises ValueError: If the token is not present in the registry. :returns: None.