Skip to main content

DAG Validation & Linting Tools in Apache Airflow

Testing DAGs Before They Ever Run βœ…πŸ§Ήβ€‹

The Story: The DAG That Looked Fine β€” Until Runtime​

The DAG parsed.
It appeared in the UI.
It even showed all tasks.

But when it ran:

  • A task crashed instantly
  • A dependency was wrong
  • A variable was missing
  • A Python import failed at execution time

These are not scheduling problems.
They are validation problems.

Professional Airflow teams validate and lint DAGs before deployment, not after failures.

This article explains how to validate DAGs using Airflow CLI tools and how linting tools like flake8 enforce quality and consistency.


What Is DAG Validation?​

DAG validation ensures:

  • DAGs load correctly
  • Tasks can execute
  • Dependencies are valid
  • Parameters are resolvable

Validation answers:

β€œWill this DAG actually work when it runs?”


What Is Linting?​

Linting enforces:

  • Code style consistency
  • Import correctness
  • Syntax correctness
  • Common Python mistakes

Linting answers:

β€œIs this DAG written cleanly and correctly?”


Validation vs Linting​

AspectValidationLinting
FocusRuntime behaviorCode quality
ToolingAirflow CLIflake8
CatchesBroken DAGsBad patterns
RequiredYesYes

airflow dags list – First Validation Check​

This command verifies that Airflow can parse your DAGs.

airflow dags list

Input​

| DAG Folder | dags/ |

Output​

dag_id
----------------
sales_etl
daily_metrics
email_alert_dag

❌ If a DAG does not appear here, it will not run.


airflow dags test – The Most Powerful Validation Tool​

This command simulates a DAG run locally.

airflow dags test sales_etl 2024-01-10

πŸ“Œ It:

  • Loads the DAG
  • Executes tasks sequentially
  • Uses local executor logic
  • Skips scheduling

Input​

ParameterValue
DAG IDsales_etl
Execution date2024-01-10

Output (Success)​

[INFO] Task extract_data succeeded
[INFO] Task transform_data succeeded
[INFO] Task load_data succeeded

Output (Failure)​

[ERROR] Task transform_data failed
ImportError: No module named 'utils'

πŸ“Œ This catches runtime issues before deployment.


airflow tasks test – Isolating Task Validation​

Test a single task.

airflow tasks test sales_etl transform_data 2024-01-10

Input​

| Task | transform_data |

Output​

| Result | Failed |

πŸ“Œ Perfect for debugging task-specific issues.


Common Validation Errors Caught Early​

  • Missing Python imports
  • Incorrect task IDs
  • Bad connections
  • Missing variables
  • Invalid Jinja templates

Linting DAGs with flake8​

Airflow DAGs are Python files.
They should follow Python standards.


Installing flake8​

pip install flake8

Running flake8​

flake8 dags/

Input​

| File | sales_etl.py |

Output (Example)​

dags/sales_etl.py:42:1 F401 'datetime.timedelta' imported but unused
dags/sales_etl.py:88:80 E501 line too long (92 > 79 characters)

flake8 Rules That Matter for DAGs​

RuleMeaning
F401Unused imports
E501Line too long
F821Undefined name
W293Blank line contains whitespace

πŸ“Œ These prevent runtime surprises.


Create .flake8 in your project root.

[flake8]
max-line-length = 100
exclude = .git,__pycache__,venv

Input & Output – Validation + Linting Workflow​

Input​

  • 15 DAG files
  • 120 tasks
  • 1 broken import
  • 3 lint issues

Output​

StageResult
dags list1 DAG missing
dags testImport error
flake83 warnings
DeploymentBlocked

βœ… Errors fixed before production.


Validation in CI/CD Pipelines​

Typical pipeline:

Git Push
β†’ flake8 dags/
β†’ airflow dags list
β†’ airflow dags test
β†’ pytest
β†’ Deploy

πŸ“Œ Broken DAGs never reach production.


Common Mistakes​

❌ Skipping dags test
❌ Treating lint warnings as optional
❌ Validating only in production
❌ No CI enforcement
❌ Mixing validation with execution


Best Practices for Professional Teams​

βœ… Validate DAGs locally before commit
βœ… Enforce linting rules
βœ… Use airflow dags test for every DAG
βœ… Automate validation in CI
βœ… Fail fast, fix early


Summary πŸ§ β€‹

  • DAG validation prevents runtime failures
  • airflow CLI provides powerful validation tools
  • flake8 enforces clean, consistent code
  • Validation + linting = production safety
  • Automation is essential

Key Takeaways​

  • βœ… airflow dags test catches runtime issues early
  • βœ… flake8 prevents common Python mistakes
  • βœ… Validation belongs in CI/CD
  • βœ… Clean DAGs are reliable DAGs
  • βœ… Prevention beats firefighting

What’s Next? πŸš€β€‹

➑️ Time-Based vs Data-Aware Scheduling (Datasets)