Skip to content
SR-Forge

Class Registry

ClassRegistry

Singleton registry for automatic class discovery and lookup.

Automatically discovers classes decorated with @register_class and enables configuration-based instantiation without full module paths.

Attributes:

Name Type Description
classes

Dictionary mapping public names to (module, class_name) tuples.

_discovered

Set of root packages already scanned.

_metadata_dir

Directory for saving registry metadata.

set_metadata_dir(path: Union[str, Path])

Set the directory for saving registry metadata.

Parameters:

Name Type Description Default
path Union[str, Path]

Directory path for metadata files.

required

discover(root: Union[str, Path])

Discover and register classes in the specified package or directory.

Scans all Python files and registers classes decorated with @register_class.

Parameters:

Name Type Description Default
root Union[str, Path]

Package name (e.g., "srforge") or Path object to source directory.

required

register(name: str, cls: Type)

Register a class with a public lookup name.

Parameters:

Name Type Description Default
name str

Public name for configuration-based lookup.

required
cls Type

Class type to register.

required

register_class(cls_or_name: Union[type, str] = None) -> Union[type, callable]

Decorator to register a class for configuration-based instantiation.

Enables using short names in YAML configs instead of full module paths.

Parameters:

Name Type Description Default
cls_or_name Union[type, str]

Class to register, or custom name string.

None

Returns:

Type Description
Union[type, callable]

Decorated class (unchanged).

Example

@register_class ... class MyModel(Model): ... pass

@register_class("custom_name") ... class AnotherModel(Model): ... pass