<#1265 feat: use ruff to bundle lazy/conditional i...
# flyte-github
a
#1265 feat: use ruff to bundle lazy/conditional imports for loaded_modules Pull request opened by paullongtan Issue With the default copy_style="loaded_modules", the SDK decides which local files to ship by inspecting sys.modules at runtime (list_imported_modules_as_files). Only modules already imported by the time bundling runs are captured, so lazily or conditionally imported local modules get silently dropped from the bundle and surface on the cluster as ModuleNotFoundError. This affects: • Function-level imports:
from helper import compute
inside a task body never runs at packaging time, so
helper
is never in sys.modules. • if TYPE_CHECKING: imports: never executed at runtime, but needed when annotations are evaluated on the cluster. Closes flyteorg/flyte#7595. Summary Augments the runtime sys.modules discovery with a static first-party import graph from ruff analyze graph. Falls back to the existing behavior whenever ruff is unavailable or the analysis fails — the feature is purely additive and never blocks bundling.
ls_files()
now picks a discovery strategy up front: if _ruff_is_available(): all_files = list_imported_modules_as_files_with_ruff(str(source_path), sys_modules) else: all_files = list_imported_modules_as_files(str(source_path), sys_modules) New functions: •
list_imported_modules_as_files_with_ruff()
— sys.modules discovery as seeds, expanded with everything reachable through the import graph. Returns the seeds unchanged if the analysis fails. •
_ruff_is_available()
— PATH check, logs at debug when ruff is missing. •
_create_ruff_import_dependency_graph()
— runs
ruff analyze graph
, parses the JSON, resolves ruff's cwd-relative paths to absolute. •
_collect_transitive_dependencies()
— BFS over the graph from the seeds. •
_ruff_analyze_timeout_seconds()
— reads the timeout, with validation. • Extracted the user-file filter into
_is_user_file()
/
_build_invalid_directories()
. Timeout The subprocess is capped at 30s (
_RUFF_ANALYZE_TIMEOUT_SECONDS
), overridable via
FLYTE_RUFF_ANALYZE_TIMEOUT_SECONDS
. Invalid or non-positive values log a warning and fall back to the default. This is a ceiling, not a delay —
ruff analyze graph
completes in ~0.15s on flyte-sdk. Notes for reviewers • ruff is a dev dependency, not a runtime one. Users without ruff on PATH get the existing sys.modules behavior unchanged. The feature is opt-in by environment. • Bundle digests now depend on the environment. Whether ruff is installed, its version, and the user's ruff
exclude
config all influence which files are discovered, so the same source can digest differently across machines. The bundles genuinely differ in those cases, so this is correct — but it means bundle cache hit rates depend on environment uniformity across a team. • Digest ordering was raised in review:
ls_files
already sorts before hashing, so set iteration order cannot reach the digest. That behavior predates this PR. Test
Copy code
uv run pytest tests/flyte/code_bundle/test_code_bundle.py -q
Result: 30 passed. Six cover this feature: •
test_ls_files_loaded_modules_includes_function_level_import
•
test_ls_files_loaded_modules_includes_type_checking_import
•
test_ls_files_loaded_modules_falls_back_when_ruff_times_out
•
test_ruff_analyze_timeout_defaults_when_env_var_unset
•
test_ruff_analyze_timeout_env_var_override
•
test_ruff_analyze_timeout_ignores_invalid_env_var
(parametrized) flyteorg/flyte-sdk