2024-12-01 12:31:11 +08:00
|
|
|
from typing import TYPE_CHECKING, TypeVar, Union, overload
|
2023-09-27 16:00:26 +08:00
|
|
|
|
2024-12-01 12:31:11 +08:00
|
|
|
from .abstract import Driver, Mixin
|
2023-09-27 16:00:26 +08:00
|
|
|
|
|
|
|
D = TypeVar("D", bound="Driver")
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
2024-02-06 12:48:23 +08:00
|
|
|
class CombinedDriver(Driver, Mixin): ...
|
2023-09-27 16:00:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-04-16 00:33:48 +08:00
|
|
|
def combine_driver(driver: type[D]) -> type[D]: ...
|
2023-09-27 16:00:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-04-16 00:33:48 +08:00
|
|
|
def combine_driver(
|
2024-07-20 14:03:32 +08:00
|
|
|
driver: type[D], __m: type[Mixin], /, *mixins: type[Mixin]
|
2024-04-16 00:33:48 +08:00
|
|
|
) -> type["CombinedDriver"]: ...
|
2023-09-27 16:00:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
def combine_driver(
|
2024-04-16 00:33:48 +08:00
|
|
|
driver: type[D], *mixins: type[Mixin]
|
|
|
|
) -> Union[type[D], type["CombinedDriver"]]:
|
2023-09-27 16:00:26 +08:00
|
|
|
"""将一个驱动器和多个混入类合并。"""
|
|
|
|
# check first
|
|
|
|
if not issubclass(driver, Driver):
|
|
|
|
raise TypeError("`driver` must be subclass of Driver")
|
|
|
|
if not all(issubclass(m, Mixin) for m in mixins):
|
|
|
|
raise TypeError("`mixins` must be subclass of Mixin")
|
|
|
|
|
|
|
|
if not mixins:
|
|
|
|
return driver
|
|
|
|
|
|
|
|
def type_(self: "CombinedDriver") -> str:
|
|
|
|
return (
|
2023-12-08 15:03:59 +08:00
|
|
|
driver.type.__get__(self) # type: ignore
|
2023-09-27 16:00:26 +08:00
|
|
|
+ "+"
|
2023-12-08 15:03:59 +08:00
|
|
|
+ "+".join(x.type.__get__(self) for x in mixins) # type: ignore
|
2023-09-27 16:00:26 +08:00
|
|
|
)
|
|
|
|
|
2024-12-01 12:31:11 +08:00
|
|
|
return type("CombinedDriver", (*mixins, driver), {"type": property(type_)}) # type: ignore
|