From b5574f84ea64603316b9e2593e6a92b495f3577f Mon Sep 17 00:00:00 2001 From: H2Sxxa Date: Thu, 21 Mar 2024 08:47:53 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Some=20Type=20fix=20and=20Optimi?= =?UTF-8?q?ze?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/saleyo/mixin.py | 10 ++++++---- tests/gc_test.py | 7 ++++--- tests/modify_test.py | 6 ++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/saleyo/mixin.py b/src/saleyo/mixin.py index fc2a810..5388f17 100644 --- a/src/saleyo/mixin.py +++ b/src/saleyo/mixin.py @@ -20,7 +20,7 @@ class Mixin: def __init__( self, - target: IterableOrSingle, + target: IterableOrSingle[Type[Any]], toolchain: ToolChain = ToolChain(), reverse_level: bool = False, ) -> None: @@ -120,9 +120,11 @@ def apply_from_class(self, mixin: T) -> T: member.mixin(target=target, toolchain=self.toolchain) return mixin - def apply_from_operations(self, operations: Iterable[MixinOperation]) -> None: + def apply_from_operations( + self, operations: IterableOrSingle[MixinOperation] + ) -> None: """ - Use operations from a `Iterable[MixinOperation]` and apply to target. + Use operations from a `IterableOrSingle[MixinOperation]` and apply to target. Always used to mixin class manually. @@ -133,7 +135,7 @@ def apply_from_operations(self, operations: Iterable[MixinOperation]) -> None: mixin.apply_from_operations([op1, op2]) ``` """ - for operation in operations: + for operation in operations if operations is Iterable else [operations]: for target in self.target: operation.mixin(target=target, toolchain=self.toolchain) diff --git a/tests/gc_test.py b/tests/gc_test.py index d8e6529..a244f24 100644 --- a/tests/gc_test.py +++ b/tests/gc_test.py @@ -4,9 +4,10 @@ @Mixin(target=str, toolchain=GCToolChain) class MixinStr: @Pre.configure(target_name="format") - def pre_format(self, *args) -> Arguments[...]: + def pre_format(self, *args, **kwargs) -> Arguments[...]: print(f"input args: {args}") - return Arguments(self, "python") + print(f"input kwargs: {kwargs}") + return Arguments(self, *args, **kwargs) -print("hello world {}".format("saleyo")) +print("hello world {},{}".format("saleyo", "some")) diff --git a/tests/modify_test.py b/tests/modify_test.py index 4de6e2a..139c9c6 100644 --- a/tests/modify_test.py +++ b/tests/modify_test.py @@ -1,6 +1,8 @@ -from saleyo import Alias, GCToolChain +from saleyo import Alias, GCToolChain, Mixin -Alias("upper", "do_upper").mixin(str, GCToolChain) +alias = Alias("upper", "do_upper") + +Mixin(str, GCToolChain).apply_from_operations(alias) print("Hello Saleyo!".do_upper()) print("Hello Saleyo!".upper())