Skip to content

Commit c92a31f

Browse files
Fix RecursionError by implementing lazy loading for URL mapping
- Move flexdown and reflex imports inside function to avoid circular imports - Use global cache with lazy initialization to defer execution until needed - Maintain dynamic algorithmic approach without hardcoded values - Resolves Python 3.11 unit test failures in CI Co-Authored-By: [email protected] <[email protected]>
1 parent 7be71f4 commit c92a31f

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

pcweb/templates/docpage/docpage.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,18 @@ def link_pill(text: str, href: str) -> rx.Component:
274274
)
275275

276276

277-
def _build_url_to_filepath_mapping():
278-
"""Build dynamic mapping from browser URLs to filesystem paths.
277+
_URL_TO_FILEPATH_MAP = None
278+
279+
def _get_url_to_filepath_mapping():
280+
"""Get the URL-to-filepath mapping, building it lazily on first access.
279281
280-
Uses the same logic as the routing system to convert file paths to browser URLs,
281-
then creates the reverse mapping for URL-to-filepath conversion.
282+
Uses lazy loading to avoid circular imports during module initialization.
282283
"""
284+
global _URL_TO_FILEPATH_MAP
285+
286+
if _URL_TO_FILEPATH_MAP is not None:
287+
return _URL_TO_FILEPATH_MAP
288+
283289
import flexdown
284290
import reflex as rx
285291

@@ -313,9 +319,8 @@ def _build_url_to_filepath_mapping():
313319

314320
url_to_filepath[url_key] = doc_path
315321

316-
return url_to_filepath
317-
318-
_URL_TO_FILEPATH_MAP = _build_url_to_filepath_mapping()
322+
_URL_TO_FILEPATH_MAP = url_to_filepath
323+
return _URL_TO_FILEPATH_MAP
319324

320325
def convert_url_path_to_github_path(url_path) -> str:
321326
"""Convert a URL path to the corresponding GitHub filesystem path.
@@ -338,7 +343,8 @@ def convert_url_path_to_github_path(url_path) -> str:
338343
path_without_docs = string_replace_operation(path_clean, "^docs/", "")
339344

340345
result_path = path_without_docs
341-
for browser_url, file_path in _URL_TO_FILEPATH_MAP.items():
346+
url_to_filepath_map = _get_url_to_filepath_mapping()
347+
for browser_url, file_path in url_to_filepath_map.items():
342348
if browser_url != file_path.replace('.md', ''):
343349
result_path = string_replace_operation(result_path, browser_url, file_path.replace('.md', ''))
344350

@@ -354,8 +360,9 @@ def convert_url_path_to_github_path(url_path) -> str:
354360
if path.startswith("docs/"):
355361
path = path[5:]
356362

357-
if path in _URL_TO_FILEPATH_MAP:
358-
return _URL_TO_FILEPATH_MAP[path]
363+
url_to_filepath_map = _get_url_to_filepath_mapping()
364+
if path in url_to_filepath_map:
365+
return url_to_filepath_map[path]
359366

360367
if not path.endswith(".md"):
361368
path += ".md"

0 commit comments

Comments
 (0)