From 9bb9a0ff2e57546f160a3e751d59e10ac38ab7ac Mon Sep 17 00:00:00 2001 From: yazawazi <47273265+Yazawazi@users.noreply.github.com> Date: Thu, 15 Feb 2024 06:22:25 +0800 Subject: [PATCH] fix: circular import --- backend/funix/config/switch.py | 3 +-- backend/funix/decorator/__init__.py | 15 ++++++------- backend/funix/decorator/file.py | 2 +- backend/funix/decorator/layout.py | 19 +++++++++++++++- backend/funix/decorator/magic.py | 29 +++++-------------------- backend/funix/prep/global_to_session.py | 4 ---- backend/funix/util/module.py | 3 +-- 7 files changed, 34 insertions(+), 41 deletions(-) diff --git a/backend/funix/config/switch.py b/backend/funix/config/switch.py index f60631c..23a87c3 100644 --- a/backend/funix/config/switch.py +++ b/backend/funix/config/switch.py @@ -3,12 +3,11 @@ It's a global switch for quick turning on/off some key features in Funix. """ -from dataclasses import dataclass from secrets import token_hex from typing import Union -class SwitchOption(dataclass): +class SwitchOption: """Switch option.""" AUTO_CONVERT_UNDERSCORE_TO_SPACE_IN_NAME: bool = True diff --git a/backend/funix/decorator/__init__.py b/backend/funix/decorator/__init__.py index eb4c5d0..d61c8dd 100644 --- a/backend/funix/decorator/__init__.py +++ b/backend/funix/decorator/__init__.py @@ -18,6 +18,13 @@ from flask import Response, request, session from funix.decorator.layout import handle_input_layout, handle_output_layout +from funix.decorator.magic import ( + parse_function_annotation, + get_type_widget_prop, + get_type_dict, + anal_function_result, + function_param_to_widget, +) from funix.decorator.pre_fill import parse_pre_fill, get_pre_fill_metadata from funix.decorator.widget import widget_parse, parse_widget from requests import post @@ -47,14 +54,6 @@ push_counter, set_default_function, ) -from funix.decorator.magic import ( - anal_function_result, - convert_row_item, - function_param_to_widget, - get_type_dict, - get_type_widget_prop, - parse_function_annotation, -) from funix.decorator.reactive import function_reactive_update, get_reactive_config from funix.decorator.runtime import RuntimeClassVisitor from funix.decorator.theme import ( diff --git a/backend/funix/decorator/file.py b/backend/funix/decorator/file.py index b1dc7e8..ff44b43 100644 --- a/backend/funix/decorator/file.py +++ b/backend/funix/decorator/file.py @@ -10,7 +10,7 @@ from flask import abort, send_file -from funix import create_safe_tempdir +from funix.util.file import create_safe_tempdir from funix.app import app from funix.config.switch import GlobalSwitchOption from funix.util.uri import is_valid_uri diff --git a/backend/funix/decorator/layout.py b/backend/funix/decorator/layout.py index 739bb37..60becf9 100644 --- a/backend/funix/decorator/layout.py +++ b/backend/funix/decorator/layout.py @@ -1,7 +1,24 @@ """ Layout decorator """ -from funix.decorator import convert_row_item + + +def convert_row_item(row_item: dict, item_type: str) -> dict: + """ + Convert a layout row item(block) to frontend-readable item. + + Parameters: + row_item (dict): The row item. + item_type (str): The item type. + + Returns: + dict: The converted item. + """ + converted_item = row_item + converted_item["type"] = item_type + converted_item["content"] = row_item[item_type] + converted_item.pop(item_type) + return converted_item def handle_input_layout(input_layout: list) -> (list, dict): diff --git a/backend/funix/decorator/magic.py b/backend/funix/decorator/magic.py index 74ef599..93bfbe1 100644 --- a/backend/funix/decorator/magic.py +++ b/backend/funix/decorator/magic.py @@ -26,7 +26,8 @@ ipython_type_convert_dict, dataframe_convert_dict, ) -from funix.decorator import analyze, get_static_uri, handle_ipython_audio_image_video +from funix.decorator.annnotation_analyzer import analyze +from funix.decorator.file import get_static_uri, handle_ipython_audio_image_video __matplotlib_use = False """ @@ -75,10 +76,10 @@ def get_type_dict(annotation: any) -> dict: >>> import typing >>> from funix.decorator.magic import get_type_dict >>> - >>> get_type_dict(int) == {"type": "int"} - >>> get_type_dict(type(True)) == {"type": "bool"} - >>> get_type_dict(typing.Literal["a", "b", "c"]) == {'type': 'str', 'whitelist': ('a', 'b', 'c')} - >>> get_type_dict(typing.Optional[int]) == {'optional': True, 'type': 'int'} + >>> assert get_type_dict(int) == {"type": "int"} + >>> assert get_type_dict(type(True)) == {"type": "bool"} + >>> assert get_type_dict(typing.Literal["a", "b", "c"]) == {'type': 'str', 'whitelist': ('a', 'b', 'c')} + >>> assert get_type_dict(typing.Optional[int]) == {'optional': True, 'type': 'int'} Returns: dict: The type dict. @@ -266,24 +267,6 @@ def get_type_widget_prop( return {"type": "object", "widget": widget} -def convert_row_item(row_item: dict, item_type: str) -> dict: - """ - Convert a layout row item(block) to frontend-readable item. - - Parameters: - row_item (dict): The row item. - item_type (str): The item type. - - Returns: - dict: The converted item. - """ - converted_item = row_item - converted_item["type"] = item_type - converted_item["content"] = row_item[item_type] - converted_item.pop(item_type) - return converted_item - - def funix_param_to_widget(annotation: any) -> str: """ Convert the funix parameter annotation to widget. diff --git a/backend/funix/prep/global_to_session.py b/backend/funix/prep/global_to_session.py index ec6eb91..c222b6b 100644 --- a/backend/funix/prep/global_to_session.py +++ b/backend/funix/prep/global_to_session.py @@ -5,15 +5,11 @@ """ from _ast import ( - AST, Assign, - Attribute, Call, Constant, Expr, - FunctionDef, Global, - List, Load, Module, Name, diff --git a/backend/funix/util/module.py b/backend/funix/util/module.py index ed046c6..720e9a7 100644 --- a/backend/funix/util/module.py +++ b/backend/funix/util/module.py @@ -11,7 +11,6 @@ from uuid import uuid4 from funix import decorator, hint -from funix.decorator import funix, funix_class def getsourcefile_safe(obj: Any) -> str | None: @@ -111,7 +110,7 @@ def handle_module( ) if in_funix: continue - use_func = funix if is_func else funix_class + use_func = decorator.funix if is_func else decorator.funix_class if member.startswith("__") or member.startswith("_FUNIX_"): continue if need_path: