dilnazanlid commented on code in PR #72369: URL: https://github.com/apache/airflow/pull/72369#discussion_r3988464915
########## task-sdk/src/airflow/sdk/importers/python_importer.py: ########## @@ -0,0 +1,270 @@ +# 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. +"""Python DAG importer - imports DAGs from Python files.""" + +from __future__ import annotations + +import functools +import importlib.machinery +import importlib.util +import logging +import os +import sys +import traceback +import warnings +from pathlib import Path +from typing import TYPE_CHECKING, Any + +from airflow.sdk._shared.module_loading.dag_file import get_unique_dag_module_name, might_contain_dag +from airflow.sdk.configuration import conf +from airflow.sdk.definitions._internal.contextmanager import DagContext +from airflow.sdk.definitions.dag import DAG +from airflow.sdk.exceptions import AirflowConfigException +from airflow.sdk.execution_time.timeout import timeout +from airflow.sdk.importers.base import ( + AbstractDagImporter, + DagDefinition, + DagImportError, + DagImportResult, + DagImportWarning, + DagSourceCode, + _normalize_extensions, + find_file_dag_definitions, + get_file_suffix, +) + +if TYPE_CHECKING: + from collections.abc import Iterator + from types import ModuleType + + from airflow.dag_processing.bundles.base import BaseDagBundle # noqa: SDK002 + +log = logging.getLogger(__name__) + + +class PythonDagImporter(AbstractDagImporter): + """ + Importer for Python DAG files. + + This is the default importer registered with the DagImporterRegistry. It handles + .py files containing Python DAGs. + """ + + supported_extensions = [".py"] Review Comment: Good catch! I have updated `PythonDagImporter.supported_extensions` to include ".pyc", branched to `importlib.machinery.SourcelessFileLoader` in `parse()` when encountering `.pyc` files, and safeguarded `get_source_code()` to return a sourceless placeholder instead of raising `UnicodeDecodeError` on bytecode. -- 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]
