Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the errors raised when None is passed to template filters #25593

Merged
merged 2 commits into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions airflow/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
# specific language governing permissions and limitations
# under the License.

from __future__ import annotations

import datetime

import jinja2.nativetypes
import jinja2.sandbox

Expand Down Expand Up @@ -44,23 +48,33 @@ class SandboxedEnvironment(_AirflowEnvironmentMixin, jinja2.sandbox.SandboxedEnv
"""SandboxedEnvironment for Airflow task templates."""


def ds_filter(value):
def ds_filter(value: datetime.date | datetime.time | None) -> str | None:
if value is None:
return None
return value.strftime('%Y-%m-%d')


def ds_nodash_filter(value):
def ds_nodash_filter(value: datetime.date | datetime.time | None) -> str | None:
if value is None:
return None
return value.strftime('%Y%m%d')


def ts_filter(value):
def ts_filter(value: datetime.date | datetime.time | None) -> str | None:
if value is None:
return None
return value.isoformat()


def ts_nodash_filter(value):
def ts_nodash_filter(value: datetime.date | datetime.time | None) -> str | None:
if value is None:
return None
return value.strftime('%Y%m%dT%H%M%S')


def ts_nodash_with_tz_filter(value):
def ts_nodash_with_tz_filter(value: datetime.date | datetime.time | None) -> str | None:
if value is None:
return None
return value.isoformat().replace('-', '').replace(':', '')


Expand Down