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.
This commit is contained in:
Masen Furer 2025-01-13 14:32:30 -08:00
parent 51edebb8fa
commit 3a8260bf12
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -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: