From 3a8260bf120551e5ec715aec5128323d2c87420c Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Mon, 13 Jan 2025 14:32:30 -0800 Subject: [PATCH] Exclude modules/packages by import Less bespoke method of considering some packages to be part of the framework and passed over when finding user code. --- reflex/utils/console.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/reflex/utils/console.py b/reflex/utils/console.py index 982a6bd3a..8929b63b6 100644 --- a/reflex/utils/console.py +++ b/reflex/utils/console.py @@ -3,6 +3,7 @@ from __future__ import annotations import inspect +import shutil from pathlib import Path from types import FrameType @@ -193,15 +194,28 @@ def warn(msg: str, dedupe: bool = False, **kwargs): def _get_first_non_framework_frame() -> FrameType | None: + import click + import typer + import typing_extensions + import reflex as rx - framework_root = Path(rx.__file__).parent.resolve() + # Exclude utility modules that should never be the source of deprecated reflex usage. + exclude_modules = [click, rx, typer, typing_extensions] + exclude_roots = [ + p.parent.resolve() + if (p := Path(m.__file__)).name == "__init__.py" + else p.resolve() + for m in exclude_modules + ] + # Specifically exclude the reflex cli module. + if reflex_bin := shutil.which(b"reflex"): + exclude_roots.append(Path(reflex_bin.decode())) + frame = inspect.currentframe() while frame := frame and frame.f_back: frame_path = Path(inspect.getfile(frame)).resolve() - if frame_path.name == "typing_extensions.py": - continue - if not frame_path.is_relative_to(framework_root): + if not any(frame_path.is_relative_to(root) for root in exclude_roots): break return frame @@ -230,7 +244,10 @@ def deprecate( # See if we can find where the deprecation exists in "user code" origin_frame = _get_first_non_framework_frame() if origin_frame is not None: - loc = f"{origin_frame.f_code.co_filename}:{origin_frame.f_lineno}" + filename = Path(origin_frame.f_code.co_filename) + if filename.is_relative_to(Path.cwd()): + filename = filename.relative_to(Path.cwd()) + loc = f"{filename}:{origin_frame.f_lineno}" dedupe_key = f"{dedupe_key} {loc}" if dedupe_key not in _EMITTED_DEPRECATION_WARNINGS: