Skip to content

[AIP-85] Add Dag importers to Task SDK in preparation for rewiring - #72369

Merged
jason810496 merged 10 commits into
apache:mainfrom
dilnazanlid:aip-85
Sep 14, 2026
Merged

jason810496 merged 10 commits into
apache:mainfrom
dilnazanlid:aip-85

Conversation

@dilnazanlid

@dilnazanlid dilnazanlid commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR is Part 1 (preparation) for AIP-85. It introduces and unit-tests AbstractDagImporter, PythonDagImporter, ZipImporter, and DagImporterRegistry within Task SDK. Rewiring the airflow-core Dag 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)

  • Extracted Dag file heuristics: Moved get_unique_dag_module_name, might_contain_dag, and might_contain_dag_via_default_heuristic from airflow.utils.file into airflow_shared.module_loading.dag_file.
  • Backward Compatibility: Maintained backward compatibility in airflow.utils.file via module attribute fallbacks/deprecation paths.
  • Added Tests: Added unit tests in shared/module_loading/tests/module_loading/test_dag_file.py.

2. Task SDK Importer Abstractions (task-sdk/src/airflow/sdk/importers/)

  • DagDefinition & FileDagDefinition:
    • Abstract base class representing a Dag source, with an opaque 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.
  • Diagnostics & Results:
    • 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 specifying import_definition(), can_handle(), list_dag_definitions(), and get_source_code().
  • DagImporterRegistry: Thread-safe singleton registry mapping Dag definitions to their corresponding importer implementations, with default registration for .py and .zip, conflict warnings, and test isolation reset support.
  • PythonDagImporter:
    • Importer for Python files (.py) supporting safe_mode heuristics, import timeouts, module execution with unique prefix isolation (unusual_prefix_), and top-level DAG / DagContext extraction with bundle metadata tracking.
    • Implements get_source_code returning Python source.
  • ZipImporter & ZipFileDagDefinition:
    • Composite importer handling .zip archives.
    • Protects against ZipSlip directory traversal attacks.
    • Implements archive-aware freshness_token and 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 include airflow.sdk.importers.

Was generative AI tooling used to co-author this PR?
  • Yes

Generated-by: Antigravity following the guidelines

@boring-cyborg

boring-cyborg Bot commented Sep 1, 2026

Copy link
Copy Markdown

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
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: [email protected]
    Slack: https://s.apache.org/airflow-slack

Comment thread airflow-core/src/airflow/utils/file.py Resolved

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@uranusjr

uranusjr commented Sep 8, 2026

Copy link
Copy Markdown
Member

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).

@dilnazanlid

Copy link
Copy Markdown
Contributor Author

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.
@dilnazanlid

Copy link
Copy Markdown
Contributor Author

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).

I would rather have this PR separately - because the actually replacing the dag_processing/importers with sdk/importers also includes moving away from filesystem path to DagDefinition, and it is quite big change too. So let me rename the PR to make it clear that this is just preparation. Thanks!

@dilnazanlid dilnazanlid changed the title [AIP-85] Move DAG importer abstractions and registry into Task SDK [AIP-85] Add Dag importers to Task SDK in preparation for rewiring Sep 8, 2026
@uranusjr

uranusjr commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

I added a refactor to ZipImporter so import_definitions no longer extra the entire archive eagerly, but only on-demand. This should reduce the impact to parsing a zip, and ZipImporter now works more similarly to how zip files are currently processed in the dag processor (for Python files).

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 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the update. Here're some more comments that I found.

@jason810496 jason810496 added area:dag-processor AIP-85: Dag Importer https://cwiki.apache.org/confluence/spaces/AIRFLOW/pages/315494137/AIP-85+DAG+importer labels Sep 11, 2026
uranusjr and others added 2 commits September 11, 2026 19:49
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.
@jason810496
jason810496 merged commit 1f5d769 into apache:main Sep 14, 2026
155 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in AIP-85 Dag Importer Sep 14, 2026
@boring-cyborg

boring-cyborg Bot commented Sep 14, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AIP-85: Dag Importer https://cwiki.apache.org/confluence/spaces/AIRFLOW/pages/315494137/AIP-85+DAG+importer area:dag-processor area:task-sdk

Projects

Development

Successfully merging this pull request may close these issues.

3 participants