Skip to content

Commit d7ec38f

Browse files
committed
Update base utility files for enablement of the class based converters
1 parent cb2fc3f commit d7ec38f

File tree

12 files changed

+1743
-991
lines changed

12 files changed

+1743
-991
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies = [
4040
"click",
4141
"datasets",
4242
"deepspeed",
43+
"eval_type_backport",
4344
"httpx[http2]",
4445
"huggingface-hub",
4546
"loguru",

src/speculators/model.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
from transformers.generation.utils import GenerateOutput
3838

3939
from speculators.config import SpeculatorModelConfig
40-
from speculators.utils import ClassRegistryMixin
40+
from speculators.utils import RegistryMixin
4141

4242

43-
class SpeculatorModel(ClassRegistryMixin, PreTrainedModel, GenerationMixin): # type: ignore[misc]
43+
class SpeculatorModel( # type: ignore[misc]
44+
RegistryMixin[type["SpeculatorModel"]], PreTrainedModel, GenerationMixin
45+
):
4446
"""
4547
Abstract base class for all speculator models in the Speculators library.
4648

src/speculators/utils/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from .auto_importer import AutoImporterMixin
22
from .pydantic_utils import PydanticClassRegistryMixin, ReloadableBaseModel
3-
from .registry import ClassRegistryMixin
3+
from .registry import RegistryMixin
44

55
__all__ = [
66
"AutoImporterMixin",
7-
"ClassRegistryMixin",
87
"PydanticClassRegistryMixin",
8+
"RegistryMixin",
99
"ReloadableBaseModel",
1010
]

src/speculators/utils/auto_importer.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,58 @@
11
"""
22
Automatic module importing utilities for dynamic class discovery.
33
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,
55
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.
169
"""
1710

11+
from __future__ import annotations
12+
1813
import importlib
1914
import pkgutil
2015
import sys
21-
from typing import ClassVar, Optional, Union
16+
from typing import ClassVar
2217

2318
__all__ = ["AutoImporterMixin"]
2419

2520

2621
class AutoImporterMixin:
2722
"""
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
4842
"""
4943

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
5347

5448
@classmethod
55-
def auto_import_package_modules(cls):
49+
def auto_import_package_modules(cls) -> None:
5650
"""
57-
Automatically imports all modules within the specified package(s).
51+
Automatically import all modules within the specified package(s).
5852
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`.
6256
6357
:raises ValueError: If the `auto_package` class variable is not set
6458
"""

0 commit comments

Comments
 (0)