disdantic.importer¶
Automatic package directory traversal, layout analysis, and loading loops.
This module provides mechanisms for scanning package directories recursively, identifying submodules, and dynamically importing them to populate class registries. It simplifies automatic discovery of polymorphic model shapes while maintaining utility to clear imported caches in testing environments.
AutoImporterMixin
¶
Provides recursive package directory module scanning with cache wiping.
This mixin enables classes to dynamically discover and import submodules in a configured package hierarchy. It is typically integrated into registry systems to trigger dynamic registration of Pydantic model subclasses at runtime.
Example
.. code-block:: python
from disdantic.importer import AutoImporterMixin
class MyRegistry(AutoImporterMixin):
auto_package = "my_app.models"
auto_ignore_modules = ["my_app.models.private_model"]
# Scan and load the modules
MyRegistry.auto_import_package_modules()
Source code in src/disdantic/importer.py
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 | |
auto_ignore_modules = None
class-attribute
¶
A list of submodule names or paths to ignore during package discovery.
auto_package = None
class-attribute
¶
The target package or packages to scan for auto-importing.
auto_import_package_modules()
classmethod
¶
Walks configured module layouts recursively and imports submodules.
This method scans the configured packages, ignoring designated modules, and imports all discovered submodules to trigger dynamic registration.
:raises ValueError: If the class variable 'auto_package' has not been properly configured and settings do not specify any auto_packages. :raises ImportError: If a target package name cannot be resolved. :returns: None.
Source code in src/disdantic/importer.py
reset_importer_cache()
classmethod
¶
Purges cached system modules imported by this class.
Clears the tracked imported submodules from both sys.modules and the internal tracking set to guarantee clean test executions.
:returns: None.