|
1 | 1 | """
|
2 | 2 | Automatic module importing utilities for dynamic class discovery.
|
3 | 3 |
|
4 |
| -This module provides a mixin class for automatic module importing within a package, |
| 4 | +This module provides a mixin class for automatic module importing within packages, |
5 | 5 | enabling dynamic discovery of classes and implementations without explicit imports.
|
6 |
| -It is particularly useful for auto-registering classes in a registry pattern where |
7 |
| -subclasses need to be discoverable at runtime. |
8 |
| -
|
9 |
| -The AutoImporterMixin can be combined with registration mechanisms to create |
10 |
| -extensible systems where new implementations are automatically discovered and |
11 |
| -registered when they are placed in the correct package structure. |
12 |
| -
|
13 |
| -Classes: |
14 |
| - - AutoImporterMixin: A mixin class that provides functionality to automatically |
15 |
| - import all modules within a specified package or list of packa |
| 6 | +It is designed for registry patterns where subclasses need to be discoverable at |
| 7 | +runtime, creating extensible systems where new implementations are automatically |
| 8 | +discovered when placed in the correct package structure. |
16 | 9 | """
|
17 | 10 |
|
| 11 | +from __future__ import annotations |
| 12 | + |
18 | 13 | import importlib
|
19 | 14 | import pkgutil
|
20 | 15 | import sys
|
21 |
| -from typing import ClassVar, Optional, Union |
| 16 | +from typing import ClassVar |
22 | 17 |
|
23 | 18 | __all__ = ["AutoImporterMixin"]
|
24 | 19 |
|
25 | 20 |
|
26 | 21 | class AutoImporterMixin:
|
27 | 22 | """
|
28 |
| - A mixin class that provides functionality to automatically import all modules |
29 |
| - within a specified package or list of packages. |
30 |
| -
|
31 |
| - This mixin is designed to be used with class registration mechanisms to enable |
32 |
| - automatic discovery and registration of classes without explicit imports. When |
33 |
| - a class inherits from AutoImporterMixin, it can define the package(s) to scan |
34 |
| - for modules by setting the `auto_package` class variable. |
35 |
| -
|
36 |
| - Usage Example: |
37 |
| - ```python |
38 |
| - from speculators.utils import AutoImporterMixin |
39 |
| - class MyRegistry(AutoImporterMixin): |
40 |
| - auto_package = "my_package.implementations" |
41 |
| -
|
42 |
| - MyRegistry.auto_import_package_modules() |
43 |
| - ``` |
44 |
| -
|
45 |
| - :cvar auto_package: The package name or tuple of names to import modules from. |
46 |
| - :cvar auto_ignore_modules: Optional tuple of module names to ignore during import. |
47 |
| - :cvar auto_imported_modules: List tracking which modules have been imported. |
| 23 | + Mixin class for automatic module importing within packages. |
| 24 | +
|
| 25 | + This mixin enables dynamic discovery of classes and implementations by |
| 26 | + automatically importing all modules within specified packages. It is designed |
| 27 | + for use with class registration mechanisms to enable automatic discovery and |
| 28 | + registration of classes when they are placed in the correct package structure. |
| 29 | +
|
| 30 | + Example: |
| 31 | + :: |
| 32 | + from speculators.utils import AutoImporterMixin |
| 33 | +
|
| 34 | + class MyRegistry(AutoImporterMixin): |
| 35 | + auto_package = "my_package.implementations" |
| 36 | +
|
| 37 | + MyRegistry.auto_import_package_modules() |
| 38 | +
|
| 39 | + :cvar auto_package: Package name or tuple of package names to import modules from |
| 40 | + :cvar auto_ignore_modules: Module names to ignore during import |
| 41 | + :cvar auto_imported_modules: List tracking which modules have been imported |
48 | 42 | """
|
49 | 43 |
|
50 |
| - auto_package: ClassVar[Optional[Union[str, tuple[str, ...]]]] = None |
51 |
| - auto_ignore_modules: ClassVar[Optional[tuple[str, ...]]] = None |
52 |
| - auto_imported_modules: ClassVar[Optional[list]] = None |
| 44 | + auto_package: ClassVar[str | tuple[str, ...] | None] = None |
| 45 | + auto_ignore_modules: ClassVar[tuple[str, ...] | None] = None |
| 46 | + auto_imported_modules: ClassVar[list[str] | None] = None |
53 | 47 |
|
54 | 48 | @classmethod
|
55 |
| - def auto_import_package_modules(cls): |
| 49 | + def auto_import_package_modules(cls) -> None: |
56 | 50 | """
|
57 |
| - Automatically imports all modules within the specified package(s). |
| 51 | + Automatically import all modules within the specified package(s). |
58 | 52 |
|
59 |
| - This method scans the package(s) defined in the `auto_package` class variable |
60 |
| - and imports all modules found, tracking them in `auto_imported_modules`. It |
61 |
| - skips packages (directories) and any modules listed in `auto_ignore_modules`. |
| 53 | + Scans the package(s) defined in `auto_package` and imports all modules found, |
| 54 | + tracking them in `auto_imported_modules`. Skips packages and any modules |
| 55 | + listed in `auto_ignore_modules`. |
62 | 56 |
|
63 | 57 | :raises ValueError: If the `auto_package` class variable is not set
|
64 | 58 | """
|
|
0 commit comments