Skip to content

Registry API Reference

Registry pattern for automatic class registration and retrieval.

Registry

Bases: ABC

Inheritance-based class registration system with hierarchical factory pattern.

Registry automatically registers classes through inheritance and provides sophisticated factory methods for instance creation. Classes automatically register themselves when they inherit from Registry subclasses, and the system provides hierarchical factory methods that respect class inheritance relationships.

Features
  • Automatic Registration: Classes auto-register when inheriting from Registry
  • Hierarchical Factory Pattern: Three-tier intelligent instantiation
  • Scoped Lookups: Each class can only instantiate its own subclasses
  • Class Aliases: Multiple names for the same class
  • Custom Registry Keys: Flexible key systems including tuples
  • String Normalization: Case-insensitive, flexible key matching
  • Registry Inspection: Query available classes and hierarchies
Hierarchical Factory Pattern

The Registry provides three intelligent instantiation modes:

  1. Direct Instantiation (Concrete Classes): Concrete classes can be instantiated directly without registry keys

    dog = Dog.of()  # No key needed for concrete classes
    

  2. Factory Method (Abstract Classes): Abstract classes require registry keys and search within their hierarchy

    animal = Animal.of("Dog", name="Buddy")  # Key required for abstract classes
    

  3. Hierarchical Scoping: Classes can only instantiate their own subclasses, enforcing hierarchy

    cat = Cat.of("TabbyCat")  # ✅ TabbyCat is subclass of Cat
    # dog = Cat.of("Dog")     # ❌ Dog is not subclass of Cat
    

Basic Usage
from morphic.registry import Registry
from abc import ABC, abstractmethod

# Base registry class
class Animal(Registry, ABC):
    @abstractmethod
    def speak(self) -> str:
        pass

# Classes automatically register when inheriting
class Dog(Animal):
    aliases = ["canine", "pup"]  # Multiple aliases supported

    def __init__(self, name: str = "Buddy"):
        self.name = name

    def speak(self) -> str:
        return f"{self.name} says Woof!"

class Cat(Animal):
    aliases = ["feline", "kitty"]

    def __init__(self, name: str = "Whiskers"):
        self.name = name

    def speak(self) -> str:
        return f"{self.name} says Meow!"

# Hierarchical factory usage examples
dog = Dog.of(name="Rex")                     # Direct concrete instantiation
cat = Animal.of("Cat", name="Fluffy")        # Factory with class name
feline = Animal.of("feline", name="Shadow")  # Using alias
pup = Animal.of("pup", name="Buddy")         # Case-insensitive fuzzy matching

# String normalization works automatically
canine = Animal.of("CANINE", name="Max")     # Case insensitive
dog2 = Animal.of("ca-nine", name="Rex")      # Handles dashes/spaces

print(dog.speak())    # "Rex says Woof!"
print(cat.speak())    # "Fluffy says Meow!"
Advanced Features
# Class aliases for flexible naming
class DatabaseConnection(Registry):
    aliases = ["db", "database", "connection"]

# Multiple ways to create the same object
db1 = DatabaseConnection.of()                    # Direct concrete instantiation
db2 = DatabaseConnection.of("db", host="remote") # Using alias
db3 = DatabaseConnection.of("DATABASE")          # Case insensitive

# Custom registry keys with _registry_keys method
class HTTPSService(Registry):
    @classmethod
    def _registry_keys(cls):
        return [
            ("protocol", "https"),          # Tuple key
            ("service", "web"),             # Another tuple key
            8443,                          # Numeric key
            "secure_web"                   # String key
        ]

# Create using different key types
service1 = HTTPSService.of(("protocol", "https"))  # Tuple key
service2 = HTTPSService.of(8443)                   # Numeric key
service3 = HTTPSService.of("secure_web")           # String key

# Registry inspection for discovery
available_classes = DatabaseConnection.subclasses()      # Get all concrete subclasses
class_by_key = DatabaseConnection.get_subclass("db")     # Get class by key
all_keys = list(DatabaseConnection._registry.keys())     # All registered keys

# Error handling with hierarchical scoping
try:
    service = WrongHierarchy.of("SomeClass")  # KeyError with available options
except KeyError as e:
    print(f"Available in hierarchy: {e}")
Hierarchical Architecture
class Service(Registry, ABC):
    pass

class DataService(Service, ABC):
    pass

class NotificationService(Service, ABC):
    pass

class FileService(DataService):
    pass

class EmailService(NotificationService):
    pass

# Hierarchical scoping enforced
file_svc = DataService.of("FileService")        # ✅ Works
email_svc = NotificationService.of("EmailService") # ✅ Works
any_svc = Service.of("EmailService")            # ✅ Works (common base)
# email_svc = DataService.of("EmailService")    # ❌ Wrong hierarchy
Configuration Options

Control registration behavior with class attributes:

  • aliases: List of alternative names for the class
  • _allow_multiple_subclasses: Allow multiple classes per key (default: False)
  • _allow_subclass_override: Allow overriding existing registrations (default: False)
  • _dont_register: Skip automatic registration (default: False)
class FlexibleService(Registry, ABC):
    _allow_multiple_subclasses = True
    _allow_subclass_override = True

class SkippedService(Registry):
    _dont_register = True  # Won't register itself
Registry Inspection

Query and explore the registry:

# Get all subclasses
animal_types = Animal.subclasses()              # Concrete classes only
all_animals = Animal.subclasses(keep_abstract=True) # Include abstract

# Get specific class by key
dog_class = Animal.get_subclass("Dog")          # Returns Dog class
cat_class = Animal.get_subclass("feline")       # Using alias

# Check what's available
available_keys = list(Animal._registry.keys())
Error Handling

The Registry provides clear error messages for common issues:

  • TypeError: Calling Registry.of() directly or abstract class without key
  • KeyError: Registry key not found in hierarchy
  • TypeError: Multiple subclasses registered to same key (when not allowed)
try:
    # Abstract class without key
    animal = Animal.of()
except TypeError as e:
    print(f"Need registry key: {e}")

try:
    # Key not in hierarchy
    dog = Cat.of("Dog")
except KeyError as e:
    print(f"Wrong hierarchy: {e}")
Performance Notes
  • Key lookups are O(1) hash table operations
  • String normalization happens once during registration
  • Hierarchical filtering is optimized for inheritance chains
  • Each registry hierarchy maintains its own lookup table
  • Minimal memory overhead per registered class
See Also
  • of(): Hierarchical factory method for instance creation
  • get_subclass(): Get class by key without creating instance
  • subclasses(): Get all registered subclasses
  • remove_subclass(): Remove class from registry
Source code in src/morphic/registry.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 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
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
370
371
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
541
542
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
class Registry(ABC):
    """
    Inheritance-based class registration system with hierarchical factory pattern.

    Registry automatically registers classes through inheritance and provides sophisticated factory methods
    for instance creation. Classes automatically register themselves when they inherit from Registry subclasses,
    and the system provides hierarchical factory methods that respect class inheritance relationships.

    Features:
        - **Automatic Registration**: Classes auto-register when inheriting from Registry
        - **Hierarchical Factory Pattern**: Three-tier intelligent instantiation
        - **Scoped Lookups**: Each class can only instantiate its own subclasses
        - **Class Aliases**: Multiple names for the same class
        - **Custom Registry Keys**: Flexible key systems including tuples
        - **String Normalization**: Case-insensitive, flexible key matching
        - **Registry Inspection**: Query available classes and hierarchies

    Hierarchical Factory Pattern:
        The Registry provides three intelligent instantiation modes:

        1. **Direct Instantiation (Concrete Classes)**:
           Concrete classes can be instantiated directly without registry keys
           ```python
           dog = Dog.of()  # No key needed for concrete classes
           ```

        2. **Factory Method (Abstract Classes)**:
           Abstract classes require registry keys and search within their hierarchy
           ```python
           animal = Animal.of("Dog", name="Buddy")  # Key required for abstract classes
           ```

        3. **Hierarchical Scoping**:
           Classes can only instantiate their own subclasses, enforcing hierarchy
           ```python
           cat = Cat.of("TabbyCat")  # ✅ TabbyCat is subclass of Cat
           # dog = Cat.of("Dog")     # ❌ Dog is not subclass of Cat
           ```

    Basic Usage:
        ```python
        from morphic.registry import Registry
        from abc import ABC, abstractmethod

        # Base registry class
        class Animal(Registry, ABC):
            @abstractmethod
            def speak(self) -> str:
                pass

        # Classes automatically register when inheriting
        class Dog(Animal):
            aliases = ["canine", "pup"]  # Multiple aliases supported

            def __init__(self, name: str = "Buddy"):
                self.name = name

            def speak(self) -> str:
                return f"{self.name} says Woof!"

        class Cat(Animal):
            aliases = ["feline", "kitty"]

            def __init__(self, name: str = "Whiskers"):
                self.name = name

            def speak(self) -> str:
                return f"{self.name} says Meow!"

        # Hierarchical factory usage examples
        dog = Dog.of(name="Rex")                     # Direct concrete instantiation
        cat = Animal.of("Cat", name="Fluffy")        # Factory with class name
        feline = Animal.of("feline", name="Shadow")  # Using alias
        pup = Animal.of("pup", name="Buddy")         # Case-insensitive fuzzy matching

        # String normalization works automatically
        canine = Animal.of("CANINE", name="Max")     # Case insensitive
        dog2 = Animal.of("ca-nine", name="Rex")      # Handles dashes/spaces

        print(dog.speak())    # "Rex says Woof!"
        print(cat.speak())    # "Fluffy says Meow!"
        ```

    Advanced Features:
        ```python
        # Class aliases for flexible naming
        class DatabaseConnection(Registry):
            aliases = ["db", "database", "connection"]

        # Multiple ways to create the same object
        db1 = DatabaseConnection.of()                    # Direct concrete instantiation
        db2 = DatabaseConnection.of("db", host="remote") # Using alias
        db3 = DatabaseConnection.of("DATABASE")          # Case insensitive

        # Custom registry keys with _registry_keys method
        class HTTPSService(Registry):
            @classmethod
            def _registry_keys(cls):
                return [
                    ("protocol", "https"),          # Tuple key
                    ("service", "web"),             # Another tuple key
                    8443,                          # Numeric key
                    "secure_web"                   # String key
                ]

        # Create using different key types
        service1 = HTTPSService.of(("protocol", "https"))  # Tuple key
        service2 = HTTPSService.of(8443)                   # Numeric key
        service3 = HTTPSService.of("secure_web")           # String key

        # Registry inspection for discovery
        available_classes = DatabaseConnection.subclasses()      # Get all concrete subclasses
        class_by_key = DatabaseConnection.get_subclass("db")     # Get class by key
        all_keys = list(DatabaseConnection._registry.keys())     # All registered keys

        # Error handling with hierarchical scoping
        try:
            service = WrongHierarchy.of("SomeClass")  # KeyError with available options
        except KeyError as e:
            print(f"Available in hierarchy: {e}")
        ```

    Hierarchical Architecture:
        ```python
        class Service(Registry, ABC):
            pass

        class DataService(Service, ABC):
            pass

        class NotificationService(Service, ABC):
            pass

        class FileService(DataService):
            pass

        class EmailService(NotificationService):
            pass

        # Hierarchical scoping enforced
        file_svc = DataService.of("FileService")        # ✅ Works
        email_svc = NotificationService.of("EmailService") # ✅ Works
        any_svc = Service.of("EmailService")            # ✅ Works (common base)
        # email_svc = DataService.of("EmailService")    # ❌ Wrong hierarchy
        ```

    Configuration Options:
        Control registration behavior with class attributes:

        - `aliases`: List of alternative names for the class
        - `_allow_multiple_subclasses`: Allow multiple classes per key (default: False)
        - `_allow_subclass_override`: Allow overriding existing registrations (default: False)
        - `_dont_register`: Skip automatic registration (default: False)

        ```python
        class FlexibleService(Registry, ABC):
            _allow_multiple_subclasses = True
            _allow_subclass_override = True

        class SkippedService(Registry):
            _dont_register = True  # Won't register itself
        ```

    Registry Inspection:
        Query and explore the registry:

        ```python
        # Get all subclasses
        animal_types = Animal.subclasses()              # Concrete classes only
        all_animals = Animal.subclasses(keep_abstract=True) # Include abstract

        # Get specific class by key
        dog_class = Animal.get_subclass("Dog")          # Returns Dog class
        cat_class = Animal.get_subclass("feline")       # Using alias

        # Check what's available
        available_keys = list(Animal._registry.keys())
        ```

    Error Handling:
        The Registry provides clear error messages for common issues:

        - `TypeError`: Calling Registry.of() directly or abstract class without key
        - `KeyError`: Registry key not found in hierarchy
        - `TypeError`: Multiple subclasses registered to same key (when not allowed)

        ```python
        try:
            # Abstract class without key
            animal = Animal.of()
        except TypeError as e:
            print(f"Need registry key: {e}")

        try:
            # Key not in hierarchy
            dog = Cat.of("Dog")
        except KeyError as e:
            print(f"Wrong hierarchy: {e}")
        ```

    Performance Notes:
        - Key lookups are O(1) hash table operations
        - String normalization happens once during registration
        - Hierarchical filtering is optimized for inheritance chains
        - Each registry hierarchy maintains its own lookup table
        - Minimal memory overhead per registered class

    See Also:
        - `of()`: Hierarchical factory method for instance creation
        - `get_subclass()`: Get class by key without creating instance
        - `subclasses()`: Get all registered subclasses
        - `remove_subclass()`: Remove class from registry
    """

    _registry: ClassVar[Dict[Any, Dict[str, Type]]] = {}
    _registry_base_class: ClassVar[Optional[Type]] = None
    _allow_multiple_subclasses: ClassVar[bool] = False
    _allow_subclass_override: ClassVar[bool] = False
    _dont_register: ClassVar[bool] = False
    aliases: ClassVar[Tuple[str, ...]] = tuple()

    def __init_subclass__(cls, **kwargs):
        """Register any subclass with the base class."""
        # Import here to avoid circular imports.
        from .function import is_abstract

        super().__init_subclass__(**kwargs)

        if cls in Registry.__subclasses__():
            # Current class is a direct subclass of Registry (base class of hierarchy)
            cls._registry = {}
            cls._registry_base_class = cls
        else:
            # Current class is a subclass of a Registry-subclass
            if not is_abstract(cls) and not cls._dont_register:
                cls._register_subclass()

    @classmethod
    def _register_subclass(cls):
        """Register this subclass in the registry."""
        keys_to_register = []

        # Add class name and aliases
        for key in [cls.__name__] + as_list(cls.aliases) + as_list(cls._registry_keys()):
            if key is None:
                continue
            elif isinstance(key, (str, AutoEnum)):
                # Case-insensitive matching for strings and AutoEnum
                key = normalize_string(key)
            elif isinstance(key, tuple):
                key = tuple(
                    normalize_string(key_part) if isinstance(key_part, (str, AutoEnum)) else key_part
                    for key_part in key
                )
            keys_to_register.append(key)

        cls._add_to_registry(keys_to_register, cls)

    @classmethod
    def _add_to_registry(cls, keys_to_register: List[Any], subclass: Type):
        """Add subclass to registry under specified keys."""
        subclass_name = subclass.__name__

        for k in as_set(keys_to_register):  # Drop duplicates
            if k not in cls._registry:
                cls._registry[k] = {subclass_name: subclass}
                continue

            # Key already exists in registry
            registered = cls._registry[k]
            registered_names = set(registered.keys())

            if subclass_name in registered_names and not cls._allow_subclass_override:
                raise KeyError(
                    f"A subclass with name '{subclass_name}' is already registered "
                    f"against key '{k}' for registry under '{cls._registry_base_class}'; "
                    f"overriding subclasses is not permitted."
                )
            elif subclass_name not in registered_names and not cls._allow_multiple_subclasses:
                if len(registered_names) == 0:
                    raise ValueError(f"Invalid state: key '{k}' is registered to an empty dict")
                if len(registered_names) > 1:
                    raise ValueError(
                        f"Invalid state: _allow_multiple_subclasses is False but multiple subclasses "
                        f"are registered against key {k}"
                    )
                existing_subclass = next(iter(registered_names))
                raise KeyError(
                    f"Key {k} is already registered to subclass {existing_subclass}; "
                    f"registering multiple subclasses to the same key is not permitted."
                )

            cls._registry[k] = {
                **registered,
                subclass_name: subclass,
            }

    @classmethod
    def get_subclass(
        cls,
        key: Any,
        raise_error: bool = True,
    ) -> Optional[Union[Type, List[Type]]]:
        """
        Get registered subclass(es) by key without creating instances.

        This method performs a global registry lookup (not hierarchical) to find all subclasses
        registered under the given key. Unlike the hierarchical `of()` method, this searches
        the entire registry without scoping restrictions.

        Args:
            key (Any): Key to look up subclass. Can be:
                - str: Class name or alias (case-insensitive, normalized)
                - tuple: Custom composite key from _registry_keys()
                - Any: Custom key type from _registry_keys()
            raise_error (bool): Whether to raise KeyError if key not found.
                Defaults to True.

        Returns:
            - **Type**: Single subclass if only one is registered under the key
            - **List[Type]**: List of subclasses if multiple are registered (when _allow_multiple_subclasses=True)
            - **None**: If key not found and raise_error=False

        Raises:
            KeyError: If key not found and raise_error=True

        Examples:
            ```python
            class Animal(Registry, ABC):
                pass

            class Dog(Animal):
                aliases = ["canine", "pup"]

            class Cat(Animal):
                aliases = ["feline"]

            # Get class by name
            DogClass = Animal.get_subclass("Dog")
            assert DogClass is Dog

            # Get class by alias
            CanineClass = Animal.get_subclass("canine")
            assert CanineClass is Dog

            # Case insensitive
            CatClass = Animal.get_subclass("FELINE")
            assert CatClass is Cat

            # Handle missing keys
            UnknownClass = Animal.get_subclass("Robot", raise_error=False)
            assert UnknownClass is None

            try:
                Animal.get_subclass("Robot")  # raise_error=True by default
            except KeyError:
                print("Robot not found")
            ```

        Note:
            This method searches the entire registry and does not enforce hierarchical
            scoping like the `of()` method. Use `of()` for hierarchy-aware instantiation.
        """
        if isinstance(key, (str, AutoEnum)):
            subclasses = cls._registry.get(normalize_string(key))
        elif isinstance(key, tuple):
            # Normalize tuple keys the same way as during registration
            normalized_key = tuple(
                normalize_string(key_part) if isinstance(key_part, (str, AutoEnum)) else key_part
                for key_part in key
            )
            subclasses = cls._registry.get(normalized_key)
        else:
            subclasses = cls._registry.get(key)

        if subclasses is None:
            if raise_error:
                available_keys = "\n".join(sorted(str(k) for k in cls._registry.keys()))
                raise KeyError(
                    f'Could not find subclass of {cls} using key: "{key}" (type={type(key)}). '
                    f"Available keys are:\n{available_keys}"
                )
            return None

        if len(subclasses) == 1:
            return next(iter(subclasses.values()))
        return list(subclasses.values())

    @classmethod
    def subclasses(cls, keep_abstract: bool = False) -> Set[Type]:
        """
        Get all registered subclasses of this Registry class.

        Returns all classes that have been registered as subclasses of the calling class.
        This provides a way to discover what implementations are available without needing
        to know their keys in advance.

        Args:
            keep_abstract (bool): Whether to include abstract classes in the result.
                Defaults to False (only concrete classes returned).

        Returns:
            Set[Type]: Set of registered subclass types. Abstract classes are excluded
            unless keep_abstract=True.

        Examples:
            ```python
            class Animal(Registry, ABC):
                pass

            class Mammal(Animal, ABC):  # Abstract intermediate
                pass

            class Dog(Mammal):
                pass

            class Cat(Mammal):
                pass

            class Bird(Animal, ABC):  # Abstract intermediate
                pass

            class Parrot(Bird):
                pass

            # Get concrete subclasses only (default)
            concrete_animals = Animal.subclasses()
            assert Dog in concrete_animals
            assert Cat in concrete_animals
            assert Parrot in concrete_animals
            assert Mammal not in concrete_animals  # Abstract
            assert Bird not in concrete_animals    # Abstract

            # Include abstract classes
            all_animals = Animal.subclasses(keep_abstract=True)
            assert Mammal in all_animals
            assert Bird in all_animals
            assert Dog in all_animals

            # Hierarchical subclasses
            mammals = Mammal.subclasses()  # Only mammal subclasses
            assert Dog in mammals
            assert Cat in mammals
            assert Parrot not in mammals  # Not a mammal
            ```

        Use Cases:
            - **Plugin Discovery**: Find all available plugin implementations
            - **Validation**: Check what implementations are registered
            - **Dynamic UI**: Build menus or lists of available options
            - **Testing**: Verify all expected subclasses are registered
            - **Documentation**: Generate lists of available classes

            ```python
            # Plugin discovery example
            available_processors = DataProcessor.subclasses()
            for processor_cls in available_processors:
                print(f"Available: {processor_cls.__name__}")
                if hasattr(processor_cls, 'aliases'):
                    print(f"  Aliases: {processor_cls.aliases}")

            # Dynamic factory with validation
            def create_safe_processor(processor_type: str, **kwargs):
                available = {cls.__name__.lower(): cls for cls in DataProcessor.subclasses()}
                if processor_type.lower() not in available:
                    raise ValueError(f"Unknown processor: {processor_type}. "
                                   f"Available: {list(available.keys())}")
                return available[processor_type.lower()](**kwargs)
            ```

        Note:
            This method respects the registry hierarchy - it only returns subclasses of the
            calling class, not subclasses of sibling classes.
        """
        # Import here to avoid circular imports.
        from .function import is_abstract

        available_subclasses = set()

        for registered_dict in cls._registry.values():
            for subclass in registered_dict.values():
                if subclass == cls._registry_base_class:
                    continue
                if is_abstract(subclass) and not keep_abstract:
                    continue
                if isinstance(subclass, type) and issubclass(subclass, cls):
                    available_subclasses.add(subclass)

        return available_subclasses

    @classmethod
    def _get_hierarchical_subclass(cls, registry_key: Any) -> Optional[Union[Type, List[Type]]]:
        """
        Get subclass by registry_key, but only search within the hierarchy of the calling class.

        For concrete classes, this can return the class itself if the registry_key matches.
        For abstract classes, this searches only within direct and indirect subclasses.
        """
        # Import here to avoid circular imports.
        from .function import is_abstract

        # If the class is concrete (not abstract) and registry_key matches the class name or aliases
        if not is_abstract(cls):
            # Check if registry_key matches this concrete class
            class_keys = [cls.__name__] + as_list(cls.aliases) + as_list(cls._registry_keys())

            for class_key in class_keys:
                if class_key is None:
                    continue
                elif isinstance(class_key, (str, AutoEnum)):
                    if (
                        normalize_string(class_key) == normalize_string(registry_key)
                        if isinstance(registry_key, (str, AutoEnum))
                        else False
                    ):
                        return cls
                elif isinstance(class_key, tuple) and isinstance(registry_key, tuple):
                    normalized_class_key = tuple(
                        normalize_string(k) if isinstance(k, (str, AutoEnum)) else k for k in class_key
                    )
                    normalized_registry_key = tuple(
                        normalize_string(k) if isinstance(k, (str, AutoEnum)) else k for k in registry_key
                    )
                    if normalized_class_key == normalized_registry_key:
                        return cls
                elif class_key == registry_key:
                    return cls

        # Search in registry but filter to only subclasses of cls
        matching_subclasses = {}

        # Normalize the search key
        if isinstance(registry_key, (str, AutoEnum)):
            search_key = normalize_string(registry_key)
        elif isinstance(registry_key, tuple):
            search_key = tuple(
                normalize_string(key_part) if isinstance(key_part, (str, AutoEnum)) else key_part
                for key_part in registry_key
            )
        else:
            search_key = registry_key

        # Look through registry for matching keys
        registry_entry = cls._registry.get(search_key)
        if registry_entry:
            # Filter to only include subclasses of cls
            for subclass_name, subclass in registry_entry.items():
                if isinstance(subclass, type) and issubclass(subclass, cls) and subclass != cls:
                    matching_subclasses[subclass_name] = subclass

        if not matching_subclasses:
            return None

        if len(matching_subclasses) == 1:
            return next(iter(matching_subclasses.values()))
        return list(matching_subclasses.values())

    @classmethod
    def remove_subclass(cls, subclass: Union[Type, str]):
        """Remove a subclass from the registry."""
        name = subclass if isinstance(subclass, str) else subclass.__name__

        # Remove from all registry entries and clean up empty dictionaries
        keys_to_remove = []
        for key, registered_dict in cls._registry.items():
            for subclass_name in list(registered_dict.keys()):
                if normalize_string(subclass_name) == normalize_string(name):
                    registered_dict.pop(subclass_name, None)
            # Mark empty dictionaries for removal
            if not registered_dict:
                keys_to_remove.append(key)

        # Remove empty registry entries
        for key in keys_to_remove:
            cls._registry.pop(key, None)

    @classmethod
    def _registry_keys(cls) -> Optional[Union[List[Any], Any]]:
        """
        Override in subclasses to provide additional registry keys.

        This method allows classes to register themselves under custom keys beyond their
        class name and aliases. Useful for creating semantic keys, composite keys, or
        domain-specific identifiers.

        Returns:
            - **None**: No additional keys (default)
            - **Any**: Single additional key of any type
            - **List[Any]**: Multiple additional keys of any type

        Key Types:
            - **str**: Additional string identifiers
            - **tuple**: Composite keys for hierarchical or multi-dimensional lookups
            - **int/float**: Numeric identifiers
            - **Any**: Custom types for domain-specific keys

        Examples:
            ```python
            # String-based semantic keys
            class EmailService(Service):
                @classmethod
                def _registry_keys(cls):
                    return ["mail", "smtp", "email-sender"]

            # Tuple-based composite keys
            class HTTPSService(Service):
                @classmethod
                def _registry_keys(cls):
                    return [
                        ("protocol", "https"),
                        ("service", "web"),
                        ("port", 443)
                    ]

            # Mixed key types
            class DatabaseService(Service):
                @classmethod
                def _registry_keys(cls):
                    return [
                        "database",                    # String key
                        ("type", "database"),          # Tuple key
                        5432,                          # Port number key
                        ("database", "postgresql")     # Specific database type
                    ]

            # Dynamic key generation
            class APIService(Service):
                version = "v2"
                endpoint = "users"

                @classmethod
                def _registry_keys(cls):
                    return [
                        f"api-{cls.version}",
                        f"{cls.endpoint}-service",
                        ("api", cls.version, cls.endpoint)
                    ]

            # Usage with different key types
            email = Service.of("smtp")                      # String key
            https = Service.of(("protocol", "https"))       # Tuple key
            db = Service.of(5432)                          # Numeric key
            api = Service.of(("api", "v2", "users"))       # Complex tuple key
            ```

        Best Practices:
            ```python
            # Use semantic, domain-appropriate keys
            class PaymentProcessor(Registry):
                @classmethod
                def _registry_keys(cls):
                    return [
                        "payment",
                        ("service", "financial"),
                        ("type", "processor")
                    ]

            # Avoid conflicts with likely aliases
            class LoggingService(Registry):
                aliases = ["logger", "log"]  # Common aliases

                @classmethod
                def _registry_keys(cls):
                    # Avoid "log" here as it's already in aliases
                    return [
                        ("service", "logging"),
                        ("type", "audit"),
                        "audit-logger"  # Specific, unlikely to conflict
                    ]

            # Use tuples for hierarchical organization
            class DatabaseConnection(Registry):
                @classmethod
                def _registry_keys(cls):
                    return [
                        ("database", "connection"),
                        ("storage", "persistent"),
                        ("type", "relational")
                    ]
            ```

        Note:
            - Keys are automatically normalized for strings (case-insensitive, flexible spacing)
            - Tuple keys have their string elements normalized individually
            - All keys must be hashable (usable as dictionary keys)
            - Keys are registered in addition to class name and aliases
            - Duplicate keys across classes will trigger registration conflicts unless
              `_allow_multiple_subclasses` is True
        """
        return None

    @classmethod
    def of(cls, registry_key: Optional[Any] = None, *args, **kwargs):
        """
        Hierarchical factory method for creating instances of registered subclasses.

        This is the core factory method that provides intelligent, hierarchy-aware instance creation.
        The behavior depends on whether the calling class is abstract or concrete, and whether a
        registry_key is provided. The method enforces hierarchical scoping - classes can only
        instantiate their own subclasses.

        Hierarchical Factory Modes:

            **1. Direct Instantiation (Concrete Classes):**
            Concrete classes can create instances directly without registry keys:
            ```python
            class Dog(Animal):  # Concrete class
                def __init__(self, name="Buddy"):
                    self.name = name

            dog = Dog.of()                    # ✅ Direct instantiation
            dog = Dog.of(name="Rex")          # ✅ With constructor args
            dog = Dog.of("Dog", name="Max")   # ✅ Also works with matching key
            ```

            **2. Factory Method (Abstract Classes):**
            Abstract classes require registry keys to specify which subclass to create:
            ```python
            class Animal(Registry, ABC):  # Abstract base
                pass

            dog = Animal.of("Dog", name="Buddy")    # ✅ Key required
            cat = Animal.of("Cat", name="Whiskers") # ✅ Key required
            # animal = Animal.of()                  # ❌ TypeError: key required
            ```

            **3. Hierarchical Scoping:**
            Classes can only instantiate their own subclasses, enforcing architecture:
            ```python
            class Mammal(Animal, ABC):
                pass

            class Cat(Mammal, ABC):
                pass

            class Dog(Mammal):
                pass

            class TabbyCat(Cat):
                pass

            # Hierarchical restrictions enforced
            tabby = Cat.of("TabbyCat")         # ✅ TabbyCat is Cat subclass
            dog = Mammal.of("Dog")             # ✅ Dog is Mammal subclass
            any_animal = Animal.of("Dog")      # ✅ Dog is Animal subclass

            # dog = Cat.of("Dog")              # ❌ KeyError: Dog not Cat subclass
            ```

        Args:
            registry_key (Any, optional): Key to look up subclass. Can be:
                - **None**: For concrete classes, instantiates the class directly
                - **str**: Class name or alias (case-insensitive, normalized)
                - **tuple**: Custom composite key from _registry_keys()
                - **Any**: Custom key type from _registry_keys()

                If None and the class is concrete, instantiates that class directly.
                If None and the class is abstract, raises TypeError.

            *args: Positional arguments passed to the subclass constructor
            **kwargs: Keyword arguments passed to the subclass constructor

        Returns:
            Instance of the found subclass or the class itself (for direct instantiation)

        Raises:
            TypeError: If called on Registry base class directly, or if abstract class
                      called without registry_key, or multiple subclasses match key
            KeyError: If registry_key not found in the class's hierarchy

        Registry Key Types:
            ```python
            # String keys (normalized: case-insensitive, space/dash/underscore flexible)
            service = Service.of("EmailService")
            service = Service.of("email-service")      # Same as above
            service = Service.of("EMAIL_SERVICE")      # Same as above

            # Alias keys
            class EmailService(Service):
                aliases = ["email", "mail", "smtp"]

            service = Service.of("email")              # Using alias

            # Tuple keys from _registry_keys()
            class HTTPService(Service):
                @classmethod
                def _registry_keys(cls):
                    return [("protocol", "http"), ("service", "web")]

            service = Service.of(("protocol", "http"))
            service = Service.of(("service", "web"))

            # Direct instantiation (concrete classes only)
            service = EmailService.of()                # No key needed
            ```

        Hierarchical Architecture Example:
            ```python
            class Service(Registry, ABC):
                @abstractmethod
                def process(self, data): pass

            class DataService(Service, ABC):
                '''Base for data processing services'''
                pass

            class NotificationService(Service, ABC):
                '''Base for notification services'''
                pass

            class FileService(DataService):
                def process(self, data):
                    return f"Processing file: {data}"

            class EmailService(NotificationService):
                aliases = ["email", "mail"]

                def process(self, data):
                    return f"Sending email: {data}"

            class SMSService(NotificationService):
                def process(self, data):
                    return f"Sending SMS: {data}"

            # Hierarchical scoping in action
            file_svc = DataService.of("FileService")           # ✅ FileService ∈ DataService
            email_svc = NotificationService.of("EmailService") # ✅ EmailService ∈ NotificationService
            mail_svc = NotificationService.of("mail")          # ✅ Using alias
            any_svc = Service.of("SMSService")                 # ✅ SMSService ∈ Service

            # These fail due to hierarchical restrictions
            # email_svc = DataService.of("EmailService")       # ❌ EmailService ∉ DataService
            # file_svc = NotificationService.of("FileService") # ❌ FileService ∉ NotificationService

            # Direct instantiation for concrete classes
            file_svc = FileService.of()                        # ✅ Direct concrete instantiation
            email_svc = EmailService.of(smtp_host="smtp.com")  # ✅ With constructor args
            ```

        Error Handling:
            ```python
            # Registry base class usage
            try:
                Registry.of("SomeClass")
            except TypeError as e:
                # "The 'of' factory method cannot be called directly on Registry class"

            # Abstract class without key
            try:
                Service.of()  # Abstract class, no key
            except TypeError as e:
                # "Cannot instantiate abstract class 'Service' without specifying a registry_key"

            # Key not found in hierarchy
            try:
                DataService.of("EmailService")  # EmailService not in DataService hierarchy
            except KeyError as e:
                # "Could not find subclass of DataService using registry_key: 'EmailService'"
                # Shows available keys in DataService hierarchy

            # Multiple subclasses (when _allow_multiple_subclasses=True)
            try:
                Service.of("shared_alias")  # Multiple classes have same alias
            except TypeError as e:
                # "Cannot instantiate using registry_key 'shared_alias' because multiple subclasses"
            ```

        Performance Notes:
            - Registry lookups are O(1) hash table operations
            - Hierarchical filtering is optimized for inheritance chains
            - String normalization is cached during registration
            - Direct instantiation (concrete classes) bypasses registry lookup entirely

        See Also:
            get_subclass(): Get class by key without creating instance
            subclasses(): Get all registered subclasses in hierarchy
            _get_hierarchical_subclass(): Internal hierarchical lookup method
        """
        # Import here to avoid circular imports.
        from .function import is_abstract

        # Prevent calling 'of' directly on Registry class
        if cls is Registry:
            raise TypeError(
                "The 'of' factory method cannot be called directly on Registry class. "
                "It must be called on a subclass of Registry."
            )

        # Ensure this is called on a Registry subclass
        if not issubclass(cls, Registry):
            raise TypeError(
                f"The 'of' method can only be called on Registry subclasses, "
                f"but {cls.__name__} is not a Registry subclass."
            )

        # Handle case where no registry_key is provided
        if registry_key is None:
            if not is_abstract(cls):
                # Concrete class without registry_key - instantiate directly
                return cls(*args, **kwargs)
            else:
                # Abstract class without registry_key - cannot instantiate
                raise TypeError(
                    f"Cannot instantiate abstract class '{cls.__name__}' without specifying "
                    f"a registry_key to identify which subclass to create."
                )

        # Use hierarchical lookup to find the subclass
        subclass = cls._get_hierarchical_subclass(registry_key)

        if subclass is None:
            # Build error message showing available keys in this hierarchy
            available_classes = set()

            # If concrete, the class itself is available
            if not is_abstract(cls):
                available_classes.add(cls.__name__)

            # Add subclasses
            for sub in cls.subclasses(keep_abstract=True):
                available_classes.add(sub.__name__)
                if hasattr(sub, "aliases"):
                    available_classes.update(as_list(sub.aliases))

            available_keys = sorted(available_classes)
            raise KeyError(
                f'Could not find subclass of {cls.__name__} using registry_key: "{registry_key}" (type={type(registry_key)}). '
                f"Available keys in this hierarchy are: {available_keys}"
            )

        # Handle case where multiple subclasses are registered to the same registry_key
        if isinstance(subclass, list):
            if len(subclass) == 1:
                subclass = subclass[0]
            else:
                raise TypeError(
                    f"Cannot instantiate using registry_key '{registry_key}' because multiple subclasses "
                    f"are registered: {[sc.__name__ for sc in subclass]}. "
                    f"Use a more specific registry_key to select a single subclass."
                )

        # Create and return instance
        return subclass(*args, **kwargs)

get_subclass(key: Any, raise_error: bool = True) -> Optional[Union[Type, List[Type]]] classmethod

Get registered subclass(es) by key without creating instances.

This method performs a global registry lookup (not hierarchical) to find all subclasses registered under the given key. Unlike the hierarchical of() method, this searches the entire registry without scoping restrictions.

Parameters:

Name Type Description Default
key Any

Key to look up subclass. Can be: - str: Class name or alias (case-insensitive, normalized) - tuple: Custom composite key from _registry_keys() - Any: Custom key type from _registry_keys()

required
raise_error bool

Whether to raise KeyError if key not found. Defaults to True.

True

Returns:

Type Description
Optional[Union[Type, List[Type]]]
  • Type: Single subclass if only one is registered under the key
Optional[Union[Type, List[Type]]]
  • List[Type]: List of subclasses if multiple are registered (when _allow_multiple_subclasses=True)
Optional[Union[Type, List[Type]]]
  • None: If key not found and raise_error=False

Raises:

Type Description
KeyError

If key not found and raise_error=True

Examples:

class Animal(Registry, ABC):
    pass

class Dog(Animal):
    aliases = ["canine", "pup"]

class Cat(Animal):
    aliases = ["feline"]

# Get class by name
DogClass = Animal.get_subclass("Dog")
assert DogClass is Dog

# Get class by alias
CanineClass = Animal.get_subclass("canine")
assert CanineClass is Dog

# Case insensitive
CatClass = Animal.get_subclass("FELINE")
assert CatClass is Cat

# Handle missing keys
UnknownClass = Animal.get_subclass("Robot", raise_error=False)
assert UnknownClass is None

try:
    Animal.get_subclass("Robot")  # raise_error=True by default
except KeyError:
    print("Robot not found")
Note

This method searches the entire registry and does not enforce hierarchical scoping like the of() method. Use of() for hierarchy-aware instantiation.

Source code in src/morphic/registry.py
@classmethod
def get_subclass(
    cls,
    key: Any,
    raise_error: bool = True,
) -> Optional[Union[Type, List[Type]]]:
    """
    Get registered subclass(es) by key without creating instances.

    This method performs a global registry lookup (not hierarchical) to find all subclasses
    registered under the given key. Unlike the hierarchical `of()` method, this searches
    the entire registry without scoping restrictions.

    Args:
        key (Any): Key to look up subclass. Can be:
            - str: Class name or alias (case-insensitive, normalized)
            - tuple: Custom composite key from _registry_keys()
            - Any: Custom key type from _registry_keys()
        raise_error (bool): Whether to raise KeyError if key not found.
            Defaults to True.

    Returns:
        - **Type**: Single subclass if only one is registered under the key
        - **List[Type]**: List of subclasses if multiple are registered (when _allow_multiple_subclasses=True)
        - **None**: If key not found and raise_error=False

    Raises:
        KeyError: If key not found and raise_error=True

    Examples:
        ```python
        class Animal(Registry, ABC):
            pass

        class Dog(Animal):
            aliases = ["canine", "pup"]

        class Cat(Animal):
            aliases = ["feline"]

        # Get class by name
        DogClass = Animal.get_subclass("Dog")
        assert DogClass is Dog

        # Get class by alias
        CanineClass = Animal.get_subclass("canine")
        assert CanineClass is Dog

        # Case insensitive
        CatClass = Animal.get_subclass("FELINE")
        assert CatClass is Cat

        # Handle missing keys
        UnknownClass = Animal.get_subclass("Robot", raise_error=False)
        assert UnknownClass is None

        try:
            Animal.get_subclass("Robot")  # raise_error=True by default
        except KeyError:
            print("Robot not found")
        ```

    Note:
        This method searches the entire registry and does not enforce hierarchical
        scoping like the `of()` method. Use `of()` for hierarchy-aware instantiation.
    """
    if isinstance(key, (str, AutoEnum)):
        subclasses = cls._registry.get(normalize_string(key))
    elif isinstance(key, tuple):
        # Normalize tuple keys the same way as during registration
        normalized_key = tuple(
            normalize_string(key_part) if isinstance(key_part, (str, AutoEnum)) else key_part
            for key_part in key
        )
        subclasses = cls._registry.get(normalized_key)
    else:
        subclasses = cls._registry.get(key)

    if subclasses is None:
        if raise_error:
            available_keys = "\n".join(sorted(str(k) for k in cls._registry.keys()))
            raise KeyError(
                f'Could not find subclass of {cls} using key: "{key}" (type={type(key)}). '
                f"Available keys are:\n{available_keys}"
            )
        return None

    if len(subclasses) == 1:
        return next(iter(subclasses.values()))
    return list(subclasses.values())

subclasses(keep_abstract: bool = False) -> Set[Type] classmethod

Get all registered subclasses of this Registry class.

Returns all classes that have been registered as subclasses of the calling class. This provides a way to discover what implementations are available without needing to know their keys in advance.

Parameters:

Name Type Description Default
keep_abstract bool

Whether to include abstract classes in the result. Defaults to False (only concrete classes returned).

False

Returns:

Type Description
Set[Type]

Set[Type]: Set of registered subclass types. Abstract classes are excluded

Set[Type]

unless keep_abstract=True.

Examples:

class Animal(Registry, ABC):
    pass

class Mammal(Animal, ABC):  # Abstract intermediate
    pass

class Dog(Mammal):
    pass

class Cat(Mammal):
    pass

class Bird(Animal, ABC):  # Abstract intermediate
    pass

class Parrot(Bird):
    pass

# Get concrete subclasses only (default)
concrete_animals = Animal.subclasses()
assert Dog in concrete_animals
assert Cat in concrete_animals
assert Parrot in concrete_animals
assert Mammal not in concrete_animals  # Abstract
assert Bird not in concrete_animals    # Abstract

# Include abstract classes
all_animals = Animal.subclasses(keep_abstract=True)
assert Mammal in all_animals
assert Bird in all_animals
assert Dog in all_animals

# Hierarchical subclasses
mammals = Mammal.subclasses()  # Only mammal subclasses
assert Dog in mammals
assert Cat in mammals
assert Parrot not in mammals  # Not a mammal
Use Cases
  • Plugin Discovery: Find all available plugin implementations
  • Validation: Check what implementations are registered
  • Dynamic UI: Build menus or lists of available options
  • Testing: Verify all expected subclasses are registered
  • Documentation: Generate lists of available classes
# Plugin discovery example
available_processors = DataProcessor.subclasses()
for processor_cls in available_processors:
    print(f"Available: {processor_cls.__name__}")
    if hasattr(processor_cls, 'aliases'):
        print(f"  Aliases: {processor_cls.aliases}")

# Dynamic factory with validation
def create_safe_processor(processor_type: str, **kwargs):
    available = {cls.__name__.lower(): cls for cls in DataProcessor.subclasses()}
    if processor_type.lower() not in available:
        raise ValueError(f"Unknown processor: {processor_type}. "
                       f"Available: {list(available.keys())}")
    return available[processor_type.lower()](**kwargs)
Note

This method respects the registry hierarchy - it only returns subclasses of the calling class, not subclasses of sibling classes.

Source code in src/morphic/registry.py
@classmethod
def subclasses(cls, keep_abstract: bool = False) -> Set[Type]:
    """
    Get all registered subclasses of this Registry class.

    Returns all classes that have been registered as subclasses of the calling class.
    This provides a way to discover what implementations are available without needing
    to know their keys in advance.

    Args:
        keep_abstract (bool): Whether to include abstract classes in the result.
            Defaults to False (only concrete classes returned).

    Returns:
        Set[Type]: Set of registered subclass types. Abstract classes are excluded
        unless keep_abstract=True.

    Examples:
        ```python
        class Animal(Registry, ABC):
            pass

        class Mammal(Animal, ABC):  # Abstract intermediate
            pass

        class Dog(Mammal):
            pass

        class Cat(Mammal):
            pass

        class Bird(Animal, ABC):  # Abstract intermediate
            pass

        class Parrot(Bird):
            pass

        # Get concrete subclasses only (default)
        concrete_animals = Animal.subclasses()
        assert Dog in concrete_animals
        assert Cat in concrete_animals
        assert Parrot in concrete_animals
        assert Mammal not in concrete_animals  # Abstract
        assert Bird not in concrete_animals    # Abstract

        # Include abstract classes
        all_animals = Animal.subclasses(keep_abstract=True)
        assert Mammal in all_animals
        assert Bird in all_animals
        assert Dog in all_animals

        # Hierarchical subclasses
        mammals = Mammal.subclasses()  # Only mammal subclasses
        assert Dog in mammals
        assert Cat in mammals
        assert Parrot not in mammals  # Not a mammal
        ```

    Use Cases:
        - **Plugin Discovery**: Find all available plugin implementations
        - **Validation**: Check what implementations are registered
        - **Dynamic UI**: Build menus or lists of available options
        - **Testing**: Verify all expected subclasses are registered
        - **Documentation**: Generate lists of available classes

        ```python
        # Plugin discovery example
        available_processors = DataProcessor.subclasses()
        for processor_cls in available_processors:
            print(f"Available: {processor_cls.__name__}")
            if hasattr(processor_cls, 'aliases'):
                print(f"  Aliases: {processor_cls.aliases}")

        # Dynamic factory with validation
        def create_safe_processor(processor_type: str, **kwargs):
            available = {cls.__name__.lower(): cls for cls in DataProcessor.subclasses()}
            if processor_type.lower() not in available:
                raise ValueError(f"Unknown processor: {processor_type}. "
                               f"Available: {list(available.keys())}")
            return available[processor_type.lower()](**kwargs)
        ```

    Note:
        This method respects the registry hierarchy - it only returns subclasses of the
        calling class, not subclasses of sibling classes.
    """
    # Import here to avoid circular imports.
    from .function import is_abstract

    available_subclasses = set()

    for registered_dict in cls._registry.values():
        for subclass in registered_dict.values():
            if subclass == cls._registry_base_class:
                continue
            if is_abstract(subclass) and not keep_abstract:
                continue
            if isinstance(subclass, type) and issubclass(subclass, cls):
                available_subclasses.add(subclass)

    return available_subclasses

remove_subclass(subclass: Union[Type, str]) classmethod

Remove a subclass from the registry.

Source code in src/morphic/registry.py
@classmethod
def remove_subclass(cls, subclass: Union[Type, str]):
    """Remove a subclass from the registry."""
    name = subclass if isinstance(subclass, str) else subclass.__name__

    # Remove from all registry entries and clean up empty dictionaries
    keys_to_remove = []
    for key, registered_dict in cls._registry.items():
        for subclass_name in list(registered_dict.keys()):
            if normalize_string(subclass_name) == normalize_string(name):
                registered_dict.pop(subclass_name, None)
        # Mark empty dictionaries for removal
        if not registered_dict:
            keys_to_remove.append(key)

    # Remove empty registry entries
    for key in keys_to_remove:
        cls._registry.pop(key, None)

of(registry_key: Optional[Any] = None, *args, **kwargs) classmethod

Hierarchical factory method for creating instances of registered subclasses.

This is the core factory method that provides intelligent, hierarchy-aware instance creation. The behavior depends on whether the calling class is abstract or concrete, and whether a registry_key is provided. The method enforces hierarchical scoping - classes can only instantiate their own subclasses.

Hierarchical Factory Modes:

**1. Direct Instantiation (Concrete Classes):**
Concrete classes can create instances directly without registry keys:
```python
class Dog(Animal):  # Concrete class
    def __init__(self, name="Buddy"):
        self.name = name

dog = Dog.of()                    # ✅ Direct instantiation
dog = Dog.of(name="Rex")          # ✅ With constructor args
dog = Dog.of("Dog", name="Max")   # ✅ Also works with matching key
```

**2. Factory Method (Abstract Classes):**
Abstract classes require registry keys to specify which subclass to create:
```python
class Animal(Registry, ABC):  # Abstract base
    pass

dog = Animal.of("Dog", name="Buddy")    # ✅ Key required
cat = Animal.of("Cat", name="Whiskers") # ✅ Key required
# animal = Animal.of()                  # ❌ TypeError: key required
```

**3. Hierarchical Scoping:**
Classes can only instantiate their own subclasses, enforcing architecture:
```python
class Mammal(Animal, ABC):
    pass

class Cat(Mammal, ABC):
    pass

class Dog(Mammal):
    pass

class TabbyCat(Cat):
    pass

# Hierarchical restrictions enforced
tabby = Cat.of("TabbyCat")         # ✅ TabbyCat is Cat subclass
dog = Mammal.of("Dog")             # ✅ Dog is Mammal subclass
any_animal = Animal.of("Dog")      # ✅ Dog is Animal subclass

# dog = Cat.of("Dog")              # ❌ KeyError: Dog not Cat subclass
```

Parameters:

Name Type Description Default
registry_key Any

Key to look up subclass. Can be: - None: For concrete classes, instantiates the class directly - str: Class name or alias (case-insensitive, normalized) - tuple: Custom composite key from _registry_keys() - Any: Custom key type from _registry_keys()

If None and the class is concrete, instantiates that class directly. If None and the class is abstract, raises TypeError.

None
*args

Positional arguments passed to the subclass constructor

()
**kwargs

Keyword arguments passed to the subclass constructor

{}

Returns:

Type Description

Instance of the found subclass or the class itself (for direct instantiation)

Raises:

Type Description
TypeError

If called on Registry base class directly, or if abstract class called without registry_key, or multiple subclasses match key

KeyError

If registry_key not found in the class's hierarchy

Registry Key Types
# String keys (normalized: case-insensitive, space/dash/underscore flexible)
service = Service.of("EmailService")
service = Service.of("email-service")      # Same as above
service = Service.of("EMAIL_SERVICE")      # Same as above

# Alias keys
class EmailService(Service):
    aliases = ["email", "mail", "smtp"]

service = Service.of("email")              # Using alias

# Tuple keys from _registry_keys()
class HTTPService(Service):
    @classmethod
    def _registry_keys(cls):
        return [("protocol", "http"), ("service", "web")]

service = Service.of(("protocol", "http"))
service = Service.of(("service", "web"))

# Direct instantiation (concrete classes only)
service = EmailService.of()                # No key needed
Hierarchical Architecture Example
class Service(Registry, ABC):
    @abstractmethod
    def process(self, data): pass

class DataService(Service, ABC):
    '''Base for data processing services'''
    pass

class NotificationService(Service, ABC):
    '''Base for notification services'''
    pass

class FileService(DataService):
    def process(self, data):
        return f"Processing file: {data}"

class EmailService(NotificationService):
    aliases = ["email", "mail"]

    def process(self, data):
        return f"Sending email: {data}"

class SMSService(NotificationService):
    def process(self, data):
        return f"Sending SMS: {data}"

# Hierarchical scoping in action
file_svc = DataService.of("FileService")           # ✅ FileService ∈ DataService
email_svc = NotificationService.of("EmailService") # ✅ EmailService ∈ NotificationService
mail_svc = NotificationService.of("mail")          # ✅ Using alias
any_svc = Service.of("SMSService")                 # ✅ SMSService ∈ Service

# These fail due to hierarchical restrictions
# email_svc = DataService.of("EmailService")       # ❌ EmailService ∉ DataService
# file_svc = NotificationService.of("FileService") # ❌ FileService ∉ NotificationService

# Direct instantiation for concrete classes
file_svc = FileService.of()                        # ✅ Direct concrete instantiation
email_svc = EmailService.of(smtp_host="smtp.com")  # ✅ With constructor args
Error Handling
# Registry base class usage
try:
    Registry.of("SomeClass")
except TypeError as e:
    # "The 'of' factory method cannot be called directly on Registry class"

# Abstract class without key
try:
    Service.of()  # Abstract class, no key
except TypeError as e:
    # "Cannot instantiate abstract class 'Service' without specifying a registry_key"

# Key not found in hierarchy
try:
    DataService.of("EmailService")  # EmailService not in DataService hierarchy
except KeyError as e:
    # "Could not find subclass of DataService using registry_key: 'EmailService'"
    # Shows available keys in DataService hierarchy

# Multiple subclasses (when _allow_multiple_subclasses=True)
try:
    Service.of("shared_alias")  # Multiple classes have same alias
except TypeError as e:
    # "Cannot instantiate using registry_key 'shared_alias' because multiple subclasses"
Performance Notes
  • Registry lookups are O(1) hash table operations
  • Hierarchical filtering is optimized for inheritance chains
  • String normalization is cached during registration
  • Direct instantiation (concrete classes) bypasses registry lookup entirely
See Also

get_subclass(): Get class by key without creating instance subclasses(): Get all registered subclasses in hierarchy _get_hierarchical_subclass(): Internal hierarchical lookup method

Source code in src/morphic/registry.py
@classmethod
def of(cls, registry_key: Optional[Any] = None, *args, **kwargs):
    """
    Hierarchical factory method for creating instances of registered subclasses.

    This is the core factory method that provides intelligent, hierarchy-aware instance creation.
    The behavior depends on whether the calling class is abstract or concrete, and whether a
    registry_key is provided. The method enforces hierarchical scoping - classes can only
    instantiate their own subclasses.

    Hierarchical Factory Modes:

        **1. Direct Instantiation (Concrete Classes):**
        Concrete classes can create instances directly without registry keys:
        ```python
        class Dog(Animal):  # Concrete class
            def __init__(self, name="Buddy"):
                self.name = name

        dog = Dog.of()                    # ✅ Direct instantiation
        dog = Dog.of(name="Rex")          # ✅ With constructor args
        dog = Dog.of("Dog", name="Max")   # ✅ Also works with matching key
        ```

        **2. Factory Method (Abstract Classes):**
        Abstract classes require registry keys to specify which subclass to create:
        ```python
        class Animal(Registry, ABC):  # Abstract base
            pass

        dog = Animal.of("Dog", name="Buddy")    # ✅ Key required
        cat = Animal.of("Cat", name="Whiskers") # ✅ Key required
        # animal = Animal.of()                  # ❌ TypeError: key required
        ```

        **3. Hierarchical Scoping:**
        Classes can only instantiate their own subclasses, enforcing architecture:
        ```python
        class Mammal(Animal, ABC):
            pass

        class Cat(Mammal, ABC):
            pass

        class Dog(Mammal):
            pass

        class TabbyCat(Cat):
            pass

        # Hierarchical restrictions enforced
        tabby = Cat.of("TabbyCat")         # ✅ TabbyCat is Cat subclass
        dog = Mammal.of("Dog")             # ✅ Dog is Mammal subclass
        any_animal = Animal.of("Dog")      # ✅ Dog is Animal subclass

        # dog = Cat.of("Dog")              # ❌ KeyError: Dog not Cat subclass
        ```

    Args:
        registry_key (Any, optional): Key to look up subclass. Can be:
            - **None**: For concrete classes, instantiates the class directly
            - **str**: Class name or alias (case-insensitive, normalized)
            - **tuple**: Custom composite key from _registry_keys()
            - **Any**: Custom key type from _registry_keys()

            If None and the class is concrete, instantiates that class directly.
            If None and the class is abstract, raises TypeError.

        *args: Positional arguments passed to the subclass constructor
        **kwargs: Keyword arguments passed to the subclass constructor

    Returns:
        Instance of the found subclass or the class itself (for direct instantiation)

    Raises:
        TypeError: If called on Registry base class directly, or if abstract class
                  called without registry_key, or multiple subclasses match key
        KeyError: If registry_key not found in the class's hierarchy

    Registry Key Types:
        ```python
        # String keys (normalized: case-insensitive, space/dash/underscore flexible)
        service = Service.of("EmailService")
        service = Service.of("email-service")      # Same as above
        service = Service.of("EMAIL_SERVICE")      # Same as above

        # Alias keys
        class EmailService(Service):
            aliases = ["email", "mail", "smtp"]

        service = Service.of("email")              # Using alias

        # Tuple keys from _registry_keys()
        class HTTPService(Service):
            @classmethod
            def _registry_keys(cls):
                return [("protocol", "http"), ("service", "web")]

        service = Service.of(("protocol", "http"))
        service = Service.of(("service", "web"))

        # Direct instantiation (concrete classes only)
        service = EmailService.of()                # No key needed
        ```

    Hierarchical Architecture Example:
        ```python
        class Service(Registry, ABC):
            @abstractmethod
            def process(self, data): pass

        class DataService(Service, ABC):
            '''Base for data processing services'''
            pass

        class NotificationService(Service, ABC):
            '''Base for notification services'''
            pass

        class FileService(DataService):
            def process(self, data):
                return f"Processing file: {data}"

        class EmailService(NotificationService):
            aliases = ["email", "mail"]

            def process(self, data):
                return f"Sending email: {data}"

        class SMSService(NotificationService):
            def process(self, data):
                return f"Sending SMS: {data}"

        # Hierarchical scoping in action
        file_svc = DataService.of("FileService")           # ✅ FileService ∈ DataService
        email_svc = NotificationService.of("EmailService") # ✅ EmailService ∈ NotificationService
        mail_svc = NotificationService.of("mail")          # ✅ Using alias
        any_svc = Service.of("SMSService")                 # ✅ SMSService ∈ Service

        # These fail due to hierarchical restrictions
        # email_svc = DataService.of("EmailService")       # ❌ EmailService ∉ DataService
        # file_svc = NotificationService.of("FileService") # ❌ FileService ∉ NotificationService

        # Direct instantiation for concrete classes
        file_svc = FileService.of()                        # ✅ Direct concrete instantiation
        email_svc = EmailService.of(smtp_host="smtp.com")  # ✅ With constructor args
        ```

    Error Handling:
        ```python
        # Registry base class usage
        try:
            Registry.of("SomeClass")
        except TypeError as e:
            # "The 'of' factory method cannot be called directly on Registry class"

        # Abstract class without key
        try:
            Service.of()  # Abstract class, no key
        except TypeError as e:
            # "Cannot instantiate abstract class 'Service' without specifying a registry_key"

        # Key not found in hierarchy
        try:
            DataService.of("EmailService")  # EmailService not in DataService hierarchy
        except KeyError as e:
            # "Could not find subclass of DataService using registry_key: 'EmailService'"
            # Shows available keys in DataService hierarchy

        # Multiple subclasses (when _allow_multiple_subclasses=True)
        try:
            Service.of("shared_alias")  # Multiple classes have same alias
        except TypeError as e:
            # "Cannot instantiate using registry_key 'shared_alias' because multiple subclasses"
        ```

    Performance Notes:
        - Registry lookups are O(1) hash table operations
        - Hierarchical filtering is optimized for inheritance chains
        - String normalization is cached during registration
        - Direct instantiation (concrete classes) bypasses registry lookup entirely

    See Also:
        get_subclass(): Get class by key without creating instance
        subclasses(): Get all registered subclasses in hierarchy
        _get_hierarchical_subclass(): Internal hierarchical lookup method
    """
    # Import here to avoid circular imports.
    from .function import is_abstract

    # Prevent calling 'of' directly on Registry class
    if cls is Registry:
        raise TypeError(
            "The 'of' factory method cannot be called directly on Registry class. "
            "It must be called on a subclass of Registry."
        )

    # Ensure this is called on a Registry subclass
    if not issubclass(cls, Registry):
        raise TypeError(
            f"The 'of' method can only be called on Registry subclasses, "
            f"but {cls.__name__} is not a Registry subclass."
        )

    # Handle case where no registry_key is provided
    if registry_key is None:
        if not is_abstract(cls):
            # Concrete class without registry_key - instantiate directly
            return cls(*args, **kwargs)
        else:
            # Abstract class without registry_key - cannot instantiate
            raise TypeError(
                f"Cannot instantiate abstract class '{cls.__name__}' without specifying "
                f"a registry_key to identify which subclass to create."
            )

    # Use hierarchical lookup to find the subclass
    subclass = cls._get_hierarchical_subclass(registry_key)

    if subclass is None:
        # Build error message showing available keys in this hierarchy
        available_classes = set()

        # If concrete, the class itself is available
        if not is_abstract(cls):
            available_classes.add(cls.__name__)

        # Add subclasses
        for sub in cls.subclasses(keep_abstract=True):
            available_classes.add(sub.__name__)
            if hasattr(sub, "aliases"):
                available_classes.update(as_list(sub.aliases))

        available_keys = sorted(available_classes)
        raise KeyError(
            f'Could not find subclass of {cls.__name__} using registry_key: "{registry_key}" (type={type(registry_key)}). '
            f"Available keys in this hierarchy are: {available_keys}"
        )

    # Handle case where multiple subclasses are registered to the same registry_key
    if isinstance(subclass, list):
        if len(subclass) == 1:
            subclass = subclass[0]
        else:
            raise TypeError(
                f"Cannot instantiate using registry_key '{registry_key}' because multiple subclasses "
                f"are registered: {[sc.__name__ for sc in subclass]}. "
                f"Use a more specific registry_key to select a single subclass."
            )

    # Create and return instance
    return subclass(*args, **kwargs)