Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import datetime
import logging
import os
from collections.abc import Collection, Iterable, Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
Expand Down Expand Up @@ -186,7 +187,7 @@ def render_template(
if isinstance(value, ObjectStoragePath):
return self._render_object_storage_path(value, context, jinja_env)

if resolve := getattr(value, "resolve", None):
if not isinstance(value, os.PathLike) and (resolve := getattr(value, "resolve", None)):
return resolve(context)

# Fast path for common built-in collections.
Expand Down
20 changes: 20 additions & 0 deletions task-sdk/tests/task_sdk/definitions/_internal/test_templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ def test_not_render_literal_value(self):

assert rendered_content == "Hello {{ name }}"

def test_render_template_pathlib_path_not_resolved(self):
"""Test that pathlib.Path objects are not incorrectly resolved via their resolve() method.

pathlib.Path has a resolve() method for filesystem resolution, which should not be
confused with the Resolvable.resolve(context) protocol used by the templater.
See: https://github.com/apache/airflow/issues/55412
"""
import pathlib

templater = Templater()
templater.template_ext = []
context = {"ds": "2006-02-01"}
path = pathlib.PurePosixPath("/some/path/to/file.txt")

rendered = templater.render_template(path, context)

# The path should be returned as-is, not passed through resolve(context)
assert rendered == path
assert isinstance(rendered, pathlib.PurePosixPath)

def test_not_render_file_literal_value(self):
templater = Templater()
templater.template_ext = [".txt"]
Expand Down
Loading