diff --git a/news/+lazy-modules.performance.md b/news/+lazy-modules.performance.md new file mode 100644 index 00000000000..f880dc31033 --- /dev/null +++ b/news/+lazy-modules.performance.md @@ -0,0 +1 @@ +On Python 3.15 and newer, importing `reflex.app` no longer loads the compiler, and `click`, `packaging` and `multiprocessing` are only imported once they are used (PEP 810 lazy imports), so apps import faster. Older Python versions are unaffected. diff --git a/packages/reflex-base/news/+lazy-modules.performance.md b/packages/reflex-base/news/+lazy-modules.performance.md new file mode 100644 index 00000000000..b3c93dbe475 --- /dev/null +++ b/packages/reflex-base/news/+lazy-modules.performance.md @@ -0,0 +1 @@ +On Python 3.15 and newer, the console's Rich progress, prompt and table modules and the sitemap plugin's XML module are only imported once they are used (PEP 810 lazy imports), so the `reflex` CLI starts faster. Older Python versions are unaffected. diff --git a/packages/reflex-base/src/reflex_base/plugins/sitemap.py b/packages/reflex-base/src/reflex_base/plugins/sitemap.py index c09f7adde52..edd7a933b47 100644 --- a/packages/reflex-base/src/reflex_base/plugins/sitemap.py +++ b/packages/reflex-base/src/reflex_base/plugins/sitemap.py @@ -1,5 +1,8 @@ """Sitemap plugin for Reflex.""" +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["xml.etree.ElementTree"] + import datetime import logging from collections.abc import Sequence diff --git a/packages/reflex-base/src/reflex_base/utils/console.py b/packages/reflex-base/src/reflex_base/utils/console.py index a25d7dbcd7b..5b6a7fa32d8 100644 --- a/packages/reflex-base/src/reflex_base/utils/console.py +++ b/packages/reflex-base/src/reflex_base/utils/console.py @@ -9,6 +9,9 @@ from __future__ import annotations +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["rich.progress", "rich.prompt", "rich.table"] + import contextlib import datetime import time diff --git a/reflex/app.py b/reflex/app.py index 718b7e0c4ae..4f52d4da876 100644 --- a/reflex/app.py +++ b/reflex/app.py @@ -2,6 +2,9 @@ from __future__ import annotations +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["reflex.compiler", "reflex.compiler.compiler"] + import asyncio import contextlib import copy diff --git a/reflex/utils/exec.py b/reflex/utils/exec.py index 995ad77fafc..1ee0662d184 100644 --- a/reflex/utils/exec.py +++ b/reflex/utils/exec.py @@ -2,6 +2,9 @@ from __future__ import annotations +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["click"] + import contextlib import functools import hashlib diff --git a/reflex/utils/js_runtimes.py b/reflex/utils/js_runtimes.py index 535d6ff57ad..29ed3ad9bc4 100644 --- a/reflex/utils/js_runtimes.py +++ b/reflex/utils/js_runtimes.py @@ -1,5 +1,8 @@ """This module provides utilities for managing JavaScript runtimes like Node.js and Bun.""" +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["packaging"] + import functools import json import logging diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index c7e32ac36ab..9892981e9db 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -2,6 +2,9 @@ from __future__ import annotations +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["packaging"] + import contextlib import importlib import importlib.metadata diff --git a/reflex/utils/telemetry.py b/reflex/utils/telemetry.py index e669935681c..21678a5a849 100644 --- a/reflex/utils/telemetry.py +++ b/reflex/utils/telemetry.py @@ -1,5 +1,8 @@ """Anonymous telemetry for Reflex.""" +# Imported on first use on Python 3.15+ (PEP 810); keep them out of import-time code. +__lazy_modules__ = ["multiprocessing"] + import dataclasses import importlib.metadata import json diff --git a/tests/units/test_lazy_modules.py b/tests/units/test_lazy_modules.py new file mode 100644 index 00000000000..9d1998f14c8 --- /dev/null +++ b/tests/units/test_lazy_modules.py @@ -0,0 +1,66 @@ +"""The `__lazy_modules__` declarations keep their imports lazy on Python 3.15+ (PEP 810).""" + +import json +import subprocess +import sys + +import pytest + +pytestmark = pytest.mark.skipif( + sys.version_info < (3, 15), reason="PEP 810 lazy imports need Python 3.15+" +) + +# Lists the names in the module's namespace that are still unresolved lazy imports. +_LIST_LAZY_NAMES = ( + "import importlib, json, sys, types; " + "module = importlib.import_module(sys.argv[1]); " + "print(json.dumps(sorted(name for name, value in vars(module).items() " + "if type(value) is types.LazyImportType)))" +) + + +@pytest.mark.parametrize( + ("module", "lazy_names"), + [ + ("reflex.app", ["compiler", "readable_name_from_component"]), + ("reflex.utils.exec", ["click"]), + ("reflex.utils.js_runtimes", ["version"]), + ("reflex.utils.prerequisites", ["version"]), + ("reflex.utils.telemetry", ["multiprocessing"]), + ( + "reflex_base.plugins.sitemap", + ["Element", "SubElement", "indent", "tostring"], + ), + ( + "reflex_base.utils.console", + [ + "MofNCompleteColumn", + "Progress", + "Prompt", + "Table", + "TaskID", + "TimeElapsedColumn", + ], + ), + ], +) +def test_lazy_modules_are_not_imported_by_the_module_itself( + module: str, lazy_names: list[str] +): + """Importing the module leaves the imports of its `__lazy_modules__` unresolved. + + Each module runs in a fresh interpreter: an import of a module that is + already loaded binds the real object instead of a lazy proxy. + + Args: + module: The module that declares `__lazy_modules__`. + lazy_names: The names its imports of those modules bind. + """ + result = subprocess.run( + [sys.executable, "-c", _LIST_LAZY_NAMES, module], + check=True, + capture_output=True, + text=True, + ) + + assert json.loads(result.stdout) == lazy_names