Skip to content

Identity-only DAG discovery and byte-importing - #73118

Merged
jason810496 merged 11 commits into
apache:mainfrom
astronomer:aip-85-initial-add
Sep 23, 2026
Merged

jason810496 merged 11 commits into
apache:mainfrom
astronomer:aip-85-initial-add

Conversation

@uranusjr

Copy link
Copy Markdown
Member

Some AIP-85 changes I mentioned I was thinking when reviewing #72369.

Discovery now filters purely by a source's identity rather than its contents. find_file_dag_definitions and list_dag_definitions match on extension and .airflowignore, skip __pycache__, and prefer a .py over a side-by-side .pyc, without reading any file or taking a safe_mode flag.

ZipImporter now processes one member at a time instead of a whole archive. It enumerates individual members during discovery, applying the same __pycache__ and .py/.pyc rules and the ZipSlip defence there, and imports a single member per call, rather than listing whole .zip files and walking their members inside import_definition.

Modules are loaded from a definition's bytes. Source and bytecode are read through the definition with custom loaders in place of the file-backed SourceFileLoader and SourcelessFileLoader, so archive members are no longer extracted to a temporary file to be scanned or imported and .py and .pyc members load in place. Accordingly, might_contain_dag now takes a definition rather than a path, and PythonDagImporter is parameterized over the file and zip-member definition types it handles.

Tests cover identity-only discovery, import-time skipping for both files and zip members, .py/.pyc dedup, .pyc import for a file and a member, and corrupt-bytecode handling.

cc @dilnazanlid

@uranusjr uranusjr added the AIP-85: Dag Importer https://cwiki.apache.org/confluence/spaces/AIRFLOW/pages/315494137/AIP-85+DAG+importer label Sep 14, 2026

@dilnazanlid dilnazanlid left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks TP!

Overall, IIUC the source identity specific discovery introduced means all the custom importers that also need to be discovered within the ZipImporter - will have to add ZipFileDagDefinition into the dag importer class or create a subclass of it?

@uranusjr
uranusjr requested a review from potiuk as a code owner September 15, 2026 08:20

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

@uranusjr uranusjr moved this from Todo to In Progress in AIP-85 Dag Importer Sep 16, 2026
@uranusjr
uranusjr force-pushed the aip-85-initial-add branch 3 times, most recently from 139bb45 to 512a596 Compare September 16, 2026 09:21

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

I took some time to understand why we won't skip the sourceless .pyc.
LGTM now, thanks.

@jason810496
jason810496 marked this pull request as draft September 23, 2026 08:31

@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 review, I just addressed all the comments in the latest commit.
I will merge once the CI pass (it seems like having degradation for providers CI currently)

Discovery now filters purely by a source's identity rather than its
contents. find_file_dag_definitions and list_dag_definitions match on
extension and .airflowignore, skip __pycache__, and prefer a .py over a
side-by-side .pyc, without reading any file or taking a safe_mode flag.
The might_contain_dag heuristic instead runs a single time, in
import_definition; plain files were previously scanned both at discovery
and again at import, so this drops a redundant scan while still reporting
non-DAG sources through DagImportResult.skipped_definitions.

ZipImporter now processes one member at a time instead of a whole
archive. It enumerates individual members during discovery, applying the
same __pycache__ and .py/.pyc rules and the ZipSlip defence there, and
imports a single member per call, rather than listing whole .zip files
and walking their members inside import_definition.

Modules are loaded from a definition's bytes. Source and bytecode are
read through the definition with custom loaders in place of the
file-backed SourceFileLoader and SourcelessFileLoader, so archive members
are no longer extracted to a temporary file to be scanned or imported and
.py and .pyc members load in place. Accordingly, might_contain_dag now
takes a definition rather than a path, and PythonDagImporter is
parameterized over the file and zip-member definition types it handles.

Tests cover identity-only discovery, per-member zip import, import-time
skipping for both files and members, .py/.pyc dedup, .pyc import for a
file and a member, and corrupt-bytecode handling.
Discovery now filters only by identity (extension, .airflowignore,
__pycache__, .py-over-.pyc); DAG detection runs once at import for both
files and zip members, surfaced through skipped_definitions. ZipImporter
processes one member at a time, and modules load from a definition's
bytes instead of an extracted file.

File sources are modelled as an abstract FileDagDefinition with
FilesystemDagDefinition and ZipMemberDagDefinition backings; importers
bind to the interface, so PythonDagImporter handles archive members
without referencing zip. The shared might_contain_dag also accepts a
definition, reading its bytes in place and falling back to as_file() only
for a custom might_contain_dag_callable; existing path/zip callers are
unchanged.
find_file_dag_definitions preferred a source over a side-by-side .pyc by
checking path.with_suffix(".py").exists(), a raw filesystem stat.

This dropped a .pyc whose .py sibling was on disk but never discovered,
excluded by .airflowignore, or not in the importer's supported extensions,
leaving nothing to import.

Decide the preference over the candidates the walk actually yields instead,
keyed by (directory, stem) so the pairing also matches case-insensitively on
the extension. Candidates keep the walk's order.
_DefinitionBytecodeLoader.get_code passed whatever marshal.loads returned
straight to exec(), so a .pyc with a valid magic header but a payload that
unmarshals to a non-code object (e.g. a marshalled string) was run as source
instead of rejected. Validate isinstance(code, types.CodeType) and raise
ImportError otherwise, keeping the corrupt-.pyc handling meaningful past the
magic-number check.
_DefinitionSourceLoader.get_data ignored its path argument and always returned
the definition's own bytes, so a pkgutil.get_data or __loader__.get_data call
for a sibling resource silently got the DAG source instead.

Raise FileNotFoundError when the requested path isn't the module's own
get_filename() value; the normal import path, which only asks for that value,
is unchanged.
list_dag_definitions is widened to yield DagDefinition | DagImportError so a
ZipImporter can be reported for the caller to forward into DagImportResult.
uranusjr and others added 4 commits September 23, 2026 19:41
A zip member is imported correctly only with its archive on sys.path, but that
setup lived in ZipImporter.import_definition. A member routed through the
registry by its .py suffix went to PythonDagImporter, which imported it without
the archive on sys.path and failed cross-member imports.

Move the setup onto the definition: FileDagDefinition.import_context is no-op
by default; ZipMemberDagDefinition overrides it to put the archive on sys.path,
and PythonDagImporter wraps the load in it.
ZIP_REGEX in airflow-core splits on os.sep, so we need to match. Join with
Path.joinpath instead (i.e. archive.zip/member.py) instead.
Identity-only discovery surfaced every extension match as a definition, so a
bundle of 10 DAGs beside a 40-module helper package became 50 definitions, and,
once wired, 50 parse processes. Content detection at import shrank each
process's work but not their count.

Move the lightweight might_contain_dag sniff back into discovery:
list_dag_definitions applies it (gated by a safe_mode parameter) so a source
that clearly holds no DAG never becomes a definition. might_contain_dag is a
method on AbstractDagImporter that returns True by default, overridden by
PythonDagImporter with the real heuristic; ZipImporter calls it on each
member's internal importer, so importers with no cheap check (non-Python ones)
keep every member. The import path no longer re-sniffs.

safe_mode moves from import_definition (where it was a redundant second check)
to list_dag_definitions, where the filter decision actually lives; it is
dropped from import_definition and the ABC.
Review of the identity-only discovery rewrite surfaced four cases where
the new byte-based paths behave differently from the file-backed ones
they replace.

The shipped configuration names the built-in heuristic in
might_contain_dag_callable, so the callable is never absent and every
scanned archive member was still written to a temporary file, which is
the cost this rewrite set out to remove. A member that failed to read
ended discovery for its whole archive, losing later valid Dags that the
file-backed path reported individually. The source-over-bytecode
preference was decided before importer lookup, so an unsupported source
could hide the bytecode beside it, and a member whose extension differed
only in case was never paired at all. A bytes-backed __init__.pyc was
treated as an ordinary module, so a sourceless package's relative
imports failed where the file-backed loader resolved them.
@jason810496
jason810496 marked this pull request as ready for review September 23, 2026 13:34
@jason810496
jason810496 merged commit b23ce7e into apache:main Sep 23, 2026
106 of 107 checks passed
@jason810496
jason810496 deleted the aip-85-initial-add branch September 23, 2026 13:35
@github-project-automation github-project-automation Bot moved this from In Progress to Done in AIP-85 Dag Importer Sep 23, 2026
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:task-sdk

Projects

Development

Successfully merging this pull request may close these issues.

5 participants