<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Snir Israeli on Medium]]></title>
        <description><![CDATA[Stories by Snir Israeli on Medium]]></description>
        <link>https://medium.com/@snir.isl?source=rss-26578db169c9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hqA_-wwpvsyCYV7ytLWAjQ.jpeg</url>
            <title>Stories by Snir Israeli on Medium</title>
            <link>https://medium.com/@snir.isl?source=rss-26578db169c9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 04:18:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@snir.isl/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering Airflow DAG Standardization with Python’s AST: A Deep Dive into Linting at Scale]]></title>
            <link>https://medium.com/apache-airflow/mastering-airflow-dag-standardization-with-pythons-ast-a-deep-dive-into-linting-at-scale-1396771a9b90?source=rss-26578db169c9------2</link>
            <guid isPermaLink="false">https://medium.com/p/1396771a9b90</guid>
            <category><![CDATA[airflow]]></category>
            <category><![CDATA[data-platform-engineering]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[apache-airflow]]></category>
            <dc:creator><![CDATA[Snir Israeli]]></dc:creator>
            <pubDate>Mon, 07 Oct 2024 13:53:03 GMT</pubDate>
            <atom:updated>2024-11-27T11:00:46.093Z</atom:updated>
            <content:encoded><![CDATA[<h3>Background</h3><p>Apache Airflow has established itself as the <em>de facto</em> tool for orchestrating data pipelines, powering countless production environments. If you’re a data engineer, you’ve likely spent hours creating and maintaining DAGs (Directed Acyclic Graphs) that guide your organization’s data workflows. As much as we love the flexibility that Airflow offers, this very flexibility can lead to significant variability in how DAGs are written across different engineers. The result? An inconsistent, hard-to-maintain codebase that can hinder scalability and introduce bugs.</p><p>At Next Insurance, we faced a similar challenge. Since our data engineering group is quite big, we realized we needed a way to enforce a consistent structure across all DAGs to ensure scalability and maintainability. This led us to create an internal tool, DAGLint, which uses Python’s Abstract Syntax Tree (AST) to lint Airflow DAGs. While this tool isn’t open source, the concepts behind it can help any team achieve uniformity and higher quality in their DAG authoring.</p><p>In this article, I’ll introduce the idea of using AST to lint your Airflow DAGs, discuss why it’s essential for standardization, and provide insights on how to write custom linting rules that fit your team’s style. Let’s dive into why DAG linting is crucial and how you can start building your own tools.</p><h3>Why Lint Your Airflow DAGs?</h3><p>Airflow DAGs are powerful, but with power comes complexity. When every engineer on your team writes DAGs differently, inconsistencies in code style, naming conventions, and structural patterns become common. The variability can slow down onboarding, increase code review times, and make debugging a headache.</p><p>In addition, there are anti-patterns you probably want to avoid due to cost or performance implications related to how the DAGs are written and parsed.</p><p>This is where linting comes into play. A linter examines code to flag style inconsistencies, potential bugs, or violations of best practices. For Airflow, this means enforcing consistent DAG structures, task names, and patterns across all workflows. By introducing a linter tailored to your needs, you’ll create a unified codebase where everyone speaks the same “DAG language.”</p><p>Since its 2024 and it won’t be considered a real article if it doesn’t contain an AI-generated image, here you go :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vMV1z_dw5R4v0SHjSj0u0Q.jpeg" /></figure><h3>How Python’s Abstract Syntax Tree (AST) Works</h3><p>So, how does one go about linting DAGs? The key lies in Python’s Abstract Syntax Tree (AST). AST is a representation of Python source code that breaks it down into its structural components. Using the AST, you can programmatically analyze your DAGs, looking for specific patterns or anti-patterns and enforcing your team’s coding standards.</p><p>Here’s a high-level view of how AST works. Consider this simple DAG example:</p><pre>from airflow import DAG<br>from airflow.operators.dummy_operator import DummyOperator<br>from datetime import datetime<br><br>with DAG(&#39;example_dag&#39;, start_date=datetime(2023, 1, 1), schedule_interval=&#39;@daily&#39;) as dag:<br>    task1 = DummyOperator(task_id=&#39;task1&#39;)<br>    task2 = DummyOperator(task_id=&#39;task2&#39;)</pre><p>When you parse this code using AST, Python breaks it down into nodes, such as Import, FunctionDef, and Assign, which represent different parts of the script. By traversing this tree, you can inspect the DAG and write rules to enforce your team’s preferred conventions.</p><h3>Writing Your Own Linting Rules with AST</h3><p>While tools like flake8 are commonly used to lint Python code, Airflow DAGs have their own set of requirements. By writing custom AST-based linting rules, you can tailor your checks to things like task naming conventions, required docstrings, or avoiding specific operator patterns.</p><p>Here’s an example of a simple rule that checks if all DAGs have a doc_md field for documentation purposes:</p><pre>import ast<br><br>class DocStringLinter(ast.NodeVisitor):<br>    def __init__(self):<br>        self.missing_docs = []<br><br>    def visit_Call(self, node):<br>        if isinstance(node.func, ast.Attribute) and node.func.attr == &#39;DAG&#39;:<br>            # Check for the presence of the &#39;doc_md&#39; argument<br>            if not any(keyword.arg == &#39;doc_md&#39; for keyword in node.keywords):<br>                self.missing_docs.append(node)<br>        self.generic_visit(node)<br><br>def lint_dag(file_path):<br>    with open(file_path, &quot;r&quot;) as source_file:<br>        tree = ast.parse(source_file.read())<br>        linter = DocStringLinter()<br>        linter.visit(tree)<br>        if linter.missing_docs:<br>            print(f&quot;Warning: Missing &#39;doc_md&#39; in DAG {file_path}&quot;)</pre><p>This linter traverses the AST to identify DAGs that lack the doc_md argument, which we consider essential for documenting our workflows at Next Insurance. While simple, this rule highlights how powerful AST-based linting can be in ensuring consistent code practices across your DAG repository.</p><h3>Customizing Linting Rules for Your Team</h3><p>The beauty of AST-based linting is that it’s highly customizable. You can write rules that enforce naming conventions, detect common anti-patterns, or ensure that proper best practices are followed in all DAGs. For example, here’s a rule that ensures all task names follow a certain convention, like starting with a verb:</p><pre>class TaskNamingConventionLinter(ast.NodeVisitor):<br>    def visit_Assign(self, node):<br>        # Ensure task_id follows naming conventions<br>        if isinstance(node.targets[0], ast.Name) and isinstance(node.value, ast.Call):<br>            if hasattr(node.value.func, &#39;attr&#39;) and node.value.func.attr.endswith(&#39;Operator&#39;):<br>                task_id = node.value.keywords[0].value.s<br>                if not task_id.split(&#39;_&#39;)[0].islower():<br>                    print(f&quot;Error: Task ID &#39;{task_id}&#39; should start with a lowercase verb.&quot;)<br>        self.generic_visit(node)<br><br>def lint_task_naming(file_path):<br>    with open(file_path, &quot;r&quot;) as source_file:<br>        tree = ast.parse(source_file.read())<br>        linter = TaskNamingConventionLinter()<br>        linter.visit(tree)</pre><p>This linter enforces that each task_id follows a convention where it starts with a lowercase verb (e.g., process_data, load_data). By adopting these types of linting rules, you can eliminate ambiguity in how tasks are named, making DAGs easier to read and maintain across the team.</p><h3>Linting Manually During Development</h3><p>We wanted linting to be very fast and that the developer will get a quick feedback loop over the issues with the DAG and how to solve them.</p><p>By running a simple CLI command, the developer will get a table printed with all the issues, their description and where to find the violation:</p><pre>daglint &lt;name_of_dag&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VB3JOMW0VorXPOj_dccIWA.jpeg" /><figcaption>Running DAGLint for a DAG that violates some of the rules</figcaption></figure><p>After fixing the violations, running it again will print the table with all rules passed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JFXMDO1GZGatWrNI9mzmTA.png" /><figcaption>DAGLint result after fixing all violations</figcaption></figure><h3>Automating Linting in Production and Ensuring Standards Remain High</h3><p>While manual linting during development is a good start, the real power of linting shines when it becomes part of your production workflows. At Next Insurance, we’ve integrated DAGLint into our CI/CD pipelines using GitHub Workflows. An example for such workflow is something like this:</p><pre>name: Lint DAGs<br>on:<br>  pull_request<br>  <br>jobs:<br>  lint:<br>    runs-on: ubuntu-latest<br>    steps:<br>    - name: Checkout code<br>      uses: actions/checkout@v2<br>    - name: Run DAGLint<br>      run: |<br>        pip install daglint<br>        daglint path/to/dags</pre><p>With this setup, DAGLint automatically runs on every pull request, ensuring only standardized DAGs get merged into your main branch. This ensures that every DAG is graded for compliance before it goes live.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*agTg5ImnyKGgYGavwIalyA.png" /><figcaption>GitHub Workflow steps</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*4SHuslpAcqUGUFQ9lJZN3A.png" /><figcaption>Pull Request where the DAG modified fully complies our standards</figcaption></figure><p>In addition, we also wanted to have a bird eye view on all DAGs in our group, their scores and the trend of standardization per team. That is because when we launched DAGLint, most of our DAGs were not fully standardized (obviously) so we wanted to get a clear view on how we are doing and how far are we from our goal to have 100% alignment.</p><p>To do that, we created a daily Jenkins pipeline that will rank all DAGs in production and store the scoring results into a dedicated table that will be the source for a Tableau dashboard we’ll use for monitoring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jz71i-fvakECVXVkJ3flaA.png" /><figcaption>Diagram of the daily scoring mechanism for enhanced monitoring</figcaption></figure><pre>pipeline {<br>    agent any<br>    stages {<br>        stage(&#39;Lint DAGs&#39;) {<br>            steps {<br>                sh &#39;daglint path/to/dags&#39;<br>            }<br>        }<br>        stage(&#39;Grade DAGs&#39;) {<br>            steps {<br>                sh &#39;daglint --grade path/to/dags&#39;<br>            }<br>        }<br>        ... (here comes the steps to load the scoring into tables, etc.)<br>    }<br>}</pre><p>And in Tableau, you can do something like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cHUGLPdzSWVLin0hsh7nvw.png" /><figcaption>A screenshot for the monitoring Tableau dashboard</figcaption></figure><h3>Encouraging Your Team to Embrace DAG Linting</h3><p>As a data engineer, it’s easy to fall into the habit of focusing on getting the code to “just work.” But when we standardize how we write DAGs, we unlock long-term maintainability, faster onboarding for new engineers, and easier debugging for the whole team.</p><p>While DAGLint is our internal tool at Next Insurance, the underlying concepts can be applied by any team. Using Python’s AST module, you can build a powerful linter that enforces the coding style and standards your team values. Whether it’s enforcing naming conventions, ensuring documentation, or avoiding common pitfalls, DAG linting can become a critical part of your data engineering workflow.</p><h3>Final Thoughts</h3><p>Writing consistent, high-quality DAGs is crucial to maintaining a scalable and maintainable data engineering environment. By using Python’s AST to create your own linting rules, you can tailor your Airflow DAGs to meet the specific needs of your team. Integrating these linting checks into your CI/CD pipelines ensures that only the best, most standardized DAGs make it to production.</p><p>In Next Insurance, putting an emphasis on standartization among our DAGs by utilizing DAGLint and its supporting processes (GitHub workflow &amp; Tableau dashboard for monitoring) improved the way we write DAGs across the group, made maintaining other teams’ DAGs way easier and straightforward, and forced us all to write better quality DAGs. It also helped us understand where we at in terms of DAGs quality and standartization and establish a clear plan on how to achieve our goals.</p><p>It’s time to stop letting inconsistent DAGs pile up. Take control of your workflows, encourage your team to adopt linting, and ensure your DAGs are clean, readable, and scalable. Happy linting!</p><p>If you have any questions or want to discuss more about how linting has improved our workflow, feel free to reach out or drop a comment!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1396771a9b90" width="1" height="1" alt=""><hr><p><a href="https://medium.com/apache-airflow/mastering-airflow-dag-standardization-with-pythons-ast-a-deep-dive-into-linting-at-scale-1396771a9b90">Mastering Airflow DAG Standardization with Python’s AST: A Deep Dive into Linting at Scale</a> was originally published in <a href="https://medium.com/apache-airflow">Apache Airflow</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>