Skip to content

Commit

Permalink
♻️ Add __excore_should_convert__ and others
Browse files Browse the repository at this point in the history
  • Loading branch information
Asthestarsfalll committed Dec 3, 2024
1 parent 6079b30 commit 97c8340
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 15 additions & 7 deletions excore/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def __rshift__(self, __other: ModuleNode) -> Self:
return self

@classmethod
def __excore_check_target_type__(cls, target_type) -> bool:
def __excore_check_target_type__(cls, target_type: type[ModuleNode]) -> bool:
return False

@classmethod
def __excore_should_convert__(cls, target_type: type[ModuleNode]) -> bool:
return False

@classmethod
Expand Down Expand Up @@ -173,11 +177,11 @@ def from_node(cls, _other: ModuleNode) -> ModuleNode:


class InterNode(ModuleNode):
priority = 2
priority: int = 2

@classmethod
def __excore_check__(cls, target_type) -> bool:
return cls.priority + target_type.priority == 5
def __excore_check_target_type__(cls, target_type: type[ModuleNode]) -> bool:
return target_type is ReusedNode


class ConfigHookNode(ModuleNode):
Expand All @@ -195,16 +199,20 @@ class ReusedNode(InterNode):
def __call__(self, **params: NodeParams) -> NodeInstance | NoCallSkipFlag: # type: ignore
return super().__call__(**params)

@classmethod
def __excore_check_target_type__(cls, target_type: type[ModuleNode]) -> bool:
return target_type is InterNode


class ClassNode(InterNode):
class ClassNode(ModuleNode):
priority: int = 1

def __call__(self) -> NodeClassType | FunctionType: # type: ignore
return self.cls

@classmethod
def __excore_check__(cls, target_type) -> bool:
return cls.priority + target_type.priority == 5
def __excore_should_convert__(cls, target_type: type[ModuleNode]) -> bool:
return True


class ChainedInvocationWrapper(ConfigArgumentHook):
Expand Down
6 changes: 3 additions & 3 deletions excore/config/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
OTHER_FLAG,
REFER_FLAG,
ChainedInvocationWrapper,
ClassNode,
ModuleNode,
ModuleWrapper,
ReusedNode,
Expand Down Expand Up @@ -335,7 +334,9 @@ def _convert_node(
ori_type = source[name].__class__
logger.ex(f"Original_type is `{ori_type}`, target_type is `{target_type}`.")
node = source[name]
if target_type.priority != ori_type.priority or target_type is ClassNode:
if target_type.priority != ori_type.priority or target_type.__excore_should_convert__(
ori_type
):
node = target_type.from_node(source[name])
self._parse_module(node)
if target_type.priority > ori_type.priority:
Expand Down Expand Up @@ -370,7 +371,6 @@ def _parse_single_param(
self.current_field = cache_field

# InterNode and ReusedNode
# if ori_type and ori_type.priority + target_type.priority == 5:
if ori_type and ori_type.__excore_check_target_type__(target_type):
raise CoreConfigParseError(
f"Error when parsing param `{ori_name}`, "
Expand Down

0 comments on commit 97c8340

Please sign in to comment.