potiuk commented on code in PR #25161: URL: https://github.com/apache/airflow/pull/25161#discussion_r927905128
########## airflow/utils/dag_parsing_context.py: ########## @@ -0,0 +1,65 @@ +# 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. +import os +from contextlib import contextmanager +from enum import Enum +from typing import NamedTuple, Optional + + +class AirflowParsingContextType(Enum): + """Type of context of parsing the DAG.""" + + DAG_PROCESSOR = "DAG_PROCESSOR" + TASK_EXECUTION = "TASK_EXECUTION" + + +class AirflowParsingContext(NamedTuple): + """Context of parsing for the DAG.""" + + context_type: Optional[AirflowParsingContextType] + dag_id: Optional[str] + task_id: Optional[str] + + +_AIRFLOW_PARSING_CONTEXT = "_AIRFLOW_PARSING_CONTEXT" +_AIRFLOW_PARSING_CONTEXT_DAG_ID = "_AIRFLOW_PARSING_CONTEXT_DAG_ID" +_AIRFLOW_PARSING_CONTEXT_TASK_ID = "_AIRFLOW_PARSING_CONTEXT_TASK_ID" + + +@contextmanager +def _airflow_parsing_context_manager( + context: AirflowParsingContextType, dag_id: Optional[str] = None, task_id: Optional[str] = None +): + os.environ[_AIRFLOW_PARSING_CONTEXT] = context.value + if context == AirflowParsingContextType.TASK_EXECUTION: + os.environ[_AIRFLOW_PARSING_CONTEXT_DAG_ID] = dag_id if dag_id is not None else "" + os.environ[_AIRFLOW_PARSING_CONTEXT_TASK_ID] = task_id if task_id is not None else "" Review Comment: Actually MyPy Complains if you do that. But indeed we should rather DELETE the values (and we should actually restore previous values not replace them with None). -- 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]
