From 62f17b1ae7c96c17b30e47c2d829f91f270dd7d1 Mon Sep 17 00:00:00 2001 From: Iisakki Rotko Date: Wed, 6 Mar 2024 13:15:41 +0100 Subject: [PATCH] refactor: improve performance of finding var names --- solara/toestand.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/solara/toestand.py b/solara/toestand.py index dd62f3074..4b6bb8c5c 100644 --- a/solara/toestand.py +++ b/solara/toestand.py @@ -91,22 +91,23 @@ def __init__(self, merge: Callable = merge_state): if settings.main.log_level in ["DEBUG", "INFO"]: import inspect - for frame in inspect.stack(): - file = frame.filename - if ( - not ( - file.endswith("solara/toestand.py") - or file.endswith("solara/reactive.py") - or file.endswith("solara/hooks/use_reactive.py") - or file.endswith("reacton/core.py") - or file.endswith("components/markdown.py") - ) - and frame.code_context is not None + # All subclasses of ValueBase have at least two calls within this file + frame = sys._getframe(2) + while frame: + file = frame.f_code.co_filename + if not ( + file.endswith("solara/toestand.py") + or file.endswith("solara/reactive.py") + or file.endswith("solara/hooks/use_reactive.py") + or file.endswith("reacton/core.py") + or file.endswith("components/markdown.py") ): - if "=" not in frame.code_context[0]: - continue - elif any(op in frame.code_context[0].split("=")[1].lower() for op in ["reactive", "use_memo", "computed", "singleton"]): - declaration = frame.code_context[0].split("=")[0].strip() + frame_info = inspect.getframeinfo(frame) + if frame_info.code_context is None or "=" not in frame_info.code_context[0]: + # For some reason MyPy complains about types even though both are FrameType | None + frame = frame.f_back # type: ignore + elif any(op in frame_info.code_context[0].split("=")[1].lower() for op in ["reactive", "use_memo", "computed", "singleton"]): + declaration = frame_info.code_context[0].split("=")[0].strip() if ":" in declaration: declaration = declaration.split(":")[0].strip() self._varname: Optional[str] = declaration @@ -117,6 +118,8 @@ def __init__(self, merge: Callable = merge_state): elif file.endswith("components/markdown.py"): self._varname = "markdown_content" break + else: + frame = frame.f_back # type: ignore if not hasattr(self, "_varname"): logger.info("No varname found") self._varname = None