Skip to content

[Templating]: attributes not defined in templated_fields also get value rendered #25409

@charlesdong1991

Description

@charlesdong1991

Apache Airflow version

2.2.2

What happened

Use a custom operator inherited from BaseOperator, and try to assign a list of sql files to an attribute that is not defined in template_fields, but it still gets rendered, unless the value assigned to the attribute is a string.

Below is the minimum reproducible example and its output, except with a query folder next to dag file, and in this example,

  • I expect all attributes starting with suffix of original shouldn’t be rendered
  • In fact, only self.original_inputs_str doesn’t get rendered (a string is assigned to it), but the other two, which are assigned a list, somehow both get rendered.

Not sure if I am misunderstanding the templated_fields, or this might be a bug.

Version: 2.2.2
Python: 3.7

What you think should happen instead

Might be a bug? Or I misunderstood how to use it.

How to reproduce

from typing import Sequence

from airflow import DAG
from airflow.models import BaseOperator
from pendulum import datetime


class TestOperator(BaseOperator):
  template_fields: Sequence[str] = ("inputs", "inputs_one", "inputs_two")
  template_ext: Sequence[str] = (".sql",)

  def __init__(self, inputs, inputs_one, inputs_two, **base_operator_kwargs):
    super().__init__(**base_operator_kwargs)

    # should render
    self.inputs = inputs
    self.inputs_one = inputs_one
    self.inputs_two = inputs_two

    # shouldn't render
    self.original_inputs_str = inputs
    self.original_inputs_one = inputs_one
    self.original_inputs_two = inputs_two

  def execute(self, context):
    # render correctly
    print(f"input: {self.inputs}")
    print(f"input 1: {self.inputs_one}")
    print(f"input 2: {self.inputs_two}")

    # expected to be NOT rendered and doesn't get rendered
    print(f"original input str: {self.original_inputs_str}")

    # both expected to be NOT rendered but get rendered
    print(f"original input 1: {self.original_inputs_one}")
    print(f"original input 2: {self.original_inputs_two}")


with DAG(
  dag_id="test",
  description="testing template fields and ext",
  start_date=datetime(2022, 7, 28),
  catchup=False,
  default_args=dict(owner="test"),
  tags=["test"],
) as dag:

  inputs_str = "query/hahaha.sql"
  inputs_one = ["query/hahaha.sql"]
  inputs_two = ["query/haha.sql", "query/hahaha.sql"]

  TestOperator(
    dag=dag,
    task_id="test",
    inputs=inputs_str,
    inputs_one=inputs_one,
    inputs_two=inputs_two,
  )

Operating System

Amazon Linux AMI

Versions of Apache Airflow Providers

No response

Deployment

MWAA

Deployment details

No response

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions