uranusjr commented on code in PR #28569: URL: https://github.com/apache/airflow/pull/28569#discussion_r1060298382
########## airflow/template/templater.py: ########## @@ -0,0 +1,187 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +from typing import TYPE_CHECKING, Any, Collection, Iterable, Sequence + +from airflow.utils.context import Context +from airflow.utils.helpers import render_template_as_native, render_template_to_string +from airflow.utils.log.logging_mixin import LoggingMixin +from airflow.utils.mixins import ResolveMixin +from airflow.utils.session import NEW_SESSION, provide_session + +if TYPE_CHECKING: + import jinja2 + from sqlalchemy.orm import Session + + from airflow import DAG + + +class Templater(LoggingMixin): + """This renders the template fields of object.""" + + # For derived classes to define which fields will get jinjaified. + template_fields: Collection[str] + # Defines which files extensions to look for in the templated fields. + template_ext: Sequence[str] + + def get_template_env(self, dag: DAG | None = None) -> jinja2.Environment: + """Fetch a Jinja template environment from the DAG or instantiate empty environment if no DAG.""" + # This is imported locally since Jinja2 is heavy and we don't need it + # for most of the functionalities. It is imported by get_template_env() + # though, so we don't need to put this after the 'if dag' check. + from airflow.templates import SandboxedEnvironment + + if dag: + return dag.get_template_env(force_sandboxed=False) Review Comment: I would move this part to AbstractOperator and have Templater simply implement ```python def get_template_env(self) -> jinja2.Environment: return SandboxedEnvironment(cache_size=0) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
