Skip to content

Commit

Permalink
🐛 Some Type fix and Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
H2Sxxa committed Mar 21, 2024
1 parent 7bb365a commit b5574f8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/saleyo/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Mixin:

def __init__(
self,
target: IterableOrSingle,
target: IterableOrSingle[Type[Any]],
toolchain: ToolChain = ToolChain(),
reverse_level: bool = False,
) -> None:
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions tests/gc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
6 changes: 4 additions & 2 deletions tests/modify_test.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit b5574f8

Please sign in to comment.