Repository navigation
Identity-only DAG discovery and byte-importing - #73118
Merged
Merged
Conversation
dilnazanlid
reviewed
Sep 14, 2026
dilnazanlid
left a comment
Contributor
There was a problem hiding this comment.
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?
kaxil
reviewed
Sep 14, 2026
jason810496
reviewed
Sep 15, 2026
jason810496
left a comment
Member
There was a problem hiding this comment.
Thanks for the improvement.
uranusjr
force-pushed
the
aip-85-initial-add
branch
3 times, most recently
from
September 16, 2026 09:21
139bb45 to
512a596
Compare
jason810496
approved these changes
Sep 17, 2026
jason810496
left a comment
Member
There was a problem hiding this comment.
I took some time to understand why we won't skip the sourceless .pyc.
LGTM now, thanks.
1 task done
jason810496
marked this pull request as draft
September 23, 2026 08:31
jason810496
reviewed
Sep 23, 2026
jason810496
left a comment
Member
There was a problem hiding this comment.
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.
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
force-pushed
the
aip-85-initial-add
branch
from
September 23, 2026 11:41
d2f744b to
dd1af1d
Compare
jason810496
marked this pull request as ready for review
September 23, 2026 13:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_definitionsandlist_dag_definitionsmatch 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 insideimport_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_dagnow 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