Repository navigation
[AIP-85] Add Dag importers to Task SDK in preparation for rewiring - #72369
Conversation
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
|
jason810496
left a comment
There was a problem hiding this comment.
Thanks for the PR, the main concern for me will be the writing the temp file and reading the temp file for each import. Not sure would it be possible to improve the overall path?
|
Since this PR is called “Move” in the title, it should also rewire the internals to actually use the newly added importers, and delete the no-longer-needed code after rewiring. Otherwise, this probably should be rebranded to preparing for the move (which would still be fine since the added code is unit-tested). |
|
Thank you for the reviews! While addressing the comments, I also added the concerns from #72442 related to moving the bundle+importer config into registry. fyi @jason810496 |
…e the /importers base classes into the task SDK
Pass bundle name and path as primitive parameters to importer methods instead of requiring a BaseDagBundle instance. This effort saves us from passing whole bundle object into the DagBag for the import_definition() method.
…rom Task SDK DAG importers and standardize exceptions. Clean up test boilerplate and redundant checks.
I would rather have this PR separately - because the actually replacing the |
|
I added a refactor to ZipImporter so There are also some minor code improvements that don’t affect any functionality (type-only imports, moving lines out of try-except that don’t belong, etc.) There are still quite some improvements I think we should have, but let’s get this in first to upblock other things. (Including this being your first PR, merging this means you can commit without another person to approve your CI runs.) |
jason810496
left a comment
There was a problem hiding this comment.
Thanks a lot for the update. Here're some more comments that I found.
The whole-zip-file concat logic is removed since we are not supposed to treat the entire zip file as one dag source. Airflow's mental model is to treat a zip more like a directory, and only one member in the zip is the source file of a given dag. This tradition should be kept. Viewing an entire zip should instead be handled as a feature on the dag bundle level.
Resolving which config module to read (airflow.configuration vs airflow.sdk.configuration) by walking the call stack only worked by coincidence, since every current caller's immediate frame already sits in an airflow-prefixed module. It silently breaks for anything else in the chain (tests, wrapped calls) and re-walks the stack on every file scanned in safe mode. Every caller already has the right conf object in scope, so require it explicitly instead.
|
Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions. |
Description
This PR is Part 1 (preparation) for AIP-85. It introduces and unit-tests
AbstractDagImporter,PythonDagImporter,ZipImporter, andDagImporterRegistrywithin Task SDK. Rewiring theairflow-coreDag processor internals to use these new SDK importers and deleting the legacy code will be done in a follow-up PR to keep reviews small and manageable.Motivation & Background
Under the Airflow 3 architecture, the Task SDK (
airflow.sdk) serves as the lightweight DAG authoring and execution boundary, while DAG file processing and bundling operate without workers requiring direct database access.To support pluggable DAG importers across various formats without creating tight coupling, the fundamental DAG importer interfaces, definitions, registries, and built-in importers (
PythonDagImporter,ZipImporter) must live inside the Task SDK.Summary of Changes
1. Shared Module Loading (
shared/module_loading)get_unique_dag_module_name,might_contain_dag, andmight_contain_dag_via_default_heuristicfromairflow.utils.fileintoairflow_shared.module_loading.dag_file.airflow.utils.filevia module attribute fallbacks/deprecation paths.shared/module_loading/tests/module_loading/test_dag_file.py.2. Task SDK Importer Abstractions (
task-sdk/src/airflow/sdk/importers/)DagDefinition&FileDagDefinition:freshness_token(used for cache invalidation and change detection), relative location resolution, direct content reading (read_bytes,read_text), and a context manager for file access (as_file()).FileDagDefinition: Concrete implementation backed by local filesystem files.DagImportError: Structured error reporting capturing source references, line/column numbers, error types, contextual snippets, suggestions, and stack traces.DagImportWarning: Non-fatal warnings captured during Dag importing.DagImportResult: Encapsulates imported Dags, errors, skipped definitions, warnings, and dependencies.DagSourceCode: Data structure containing raw source code and language identifier for Code view presentation.AbstractDagImporter: Base interface specifyingimport_definition(),can_handle(),list_dag_definitions(), andget_source_code().DagImporterRegistry: Thread-safe singleton registry mapping Dag definitions to their corresponding importer implementations, with default registration for.pyand.zip, conflict warnings, and test isolation reset support.PythonDagImporter:.py) supportingsafe_modeheuristics, import timeouts, module execution with unique prefix isolation (unusual_prefix_), and top-levelDAG/DagContextextraction with bundle metadata tracking.get_source_codereturning Python source.ZipImporter&ZipFileDagDefinition:.ziparchives.freshness_tokenand source code extraction.3. Test Coverage
task-sdk/tests/task_sdk/importers/test_python_importer.py: Tests for importing valid Dags, syntax error reporting, skipping non-Dag files, bundle metadata propagation, and source code retrieval.task-sdk/tests/task_sdk/importers/test_registry.py: Tests for singleton lifecycle, default registrations, extension resolution, case insensitivity, and registry resets.task-sdk/tests/task_sdk/importers/test_zip_importer.py: Tests for ZIP Dag importing, ZipSlip directory traversal prevention, source code extraction (whole archive and archive member), and composite freshness tokens.task-sdk/tests/task_sdk/docs/test_public_api.py: Updated public API verification to includeairflow.sdk.importers.Was generative AI tooling used to co-author this PR?
Generated-by: Antigravity following the guidelines