Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/+lazy-modules.performance.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions packages/reflex-base/news/+lazy-modules.performance.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions packages/reflex-base/src/reflex_base/plugins/sitemap.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions packages/reflex-base/src/reflex_base/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions reflex/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions reflex/utils/js_runtimes.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions reflex/utils/telemetry.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
66 changes: 66 additions & 0 deletions tests/units/test_lazy_modules.py
Original file line number Diff line number Diff line change
@@ -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
Loading