BasPH / pylint-airflow

Pylint plugin for static code analysis on Airflow code
MIT License
90 stars 13 forks source link

Crash 'Name' object has no attribute 'value' on airflow DAGs defined within a function #8

Open boaz-codota opened 2 years ago

boaz-codota commented 2 years ago

Bug description

Originally posted under pylint repository: https://github.com/PyCQA/pylint/issues/5448

""" creates team DAG """
import airflow
from airflow.models.dag import DAG
from airflow.operators.dummy import DummyOperator

def create_github_team_dag(dag_id: str, team_id: str):
    """
    Create Github Team DAG
    """
    dag = DAG(
        dag_id,
        schedule_interval="@daily",
        default_args={
            "depends_on_past": False,
            "email_on_failure": False,
            "email_on_retry": False,
        },
        start_date=airflow.utils.dates.days_ago(0),
        catchup=False,
    )

    with dag:
        DummyOperator(
            task_id="dummy",
        )

    return dag

pylint crashed with a AttributeError and with the following stacktrace:

Traceback (most recent call last):
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1034, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1069, in _check_file
    check_astroid_module(ast_node)
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1203, in check_astroid_module
    retval = self._check_astroid_module(
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1250, in _check_astroid_module
    walker.walk(node)
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint_airflow/checkers/dag.py", line 101, in visit_module
    dagid, dagnode = _find_dag(assign.value, func)
  File "/Users/--/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint_airflow/checkers/dag.py", line 93, in _find_dag
    return call_node.args[0].value, call_node
AttributeError: 'Name' object has no attribute 'value'

Configuration

No response

Command used

poetry run pylint --load-plugins=pylint_airflow dags/ tests/

Pylint output

Exception on node <Module.dags.tabnine.create_github_team_dag l.0 at 0x7ffdd92b15b0> in file '/Users/boazberman/Projects/Codota/airflow-dags/dags/tabnine/create_github_team_dag.py'
Traceback (most recent call last):
  File "/Users/boazberman/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/boazberman/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint_airflow/checkers/dag.py", line 101, in visit_module
    dagid, dagnode = _find_dag(assign.value, func)
  File "/Users/boazberman/Projects/Codota/airflow-dags/.venv/lib/python3.8/site-packages/pylint_airflow/checkers/dag.py", line 93, in _find_dag
    return call_node.args[0].value, call_node
AttributeError: 'Name' object has no attribute 'value'
************* Module dags.tabnine.create_github_team_dag
dags/tabnine/create_github_team_dag.py:1:0: F0001: Fatal error while checking 'dags/tabnine/create_github_team_dag.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/Users/boazberman/Library/Caches/pylint/pylint-crash-2021-12-01-10.txt'. (fatal)

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Expected behavior

It should not emit an exception

Pylint version

pylint 2.12.1
astroid 2.9.0
Python 3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]

OS / Environment

Macbook Pro, 2019, Big Sur

Additional dependencies

[tool.poetry.dependencies] python = "^3.8" apache-airflow = { version = "2.1.4", extras = ["celery", "google", "slack", "docker", "cncf.kubernetes"] }

[tool.poetry.dev-dependencies] flake8 = "^4.0.1" flake8-black = "^0.2.3" black = "^21.11b1" pylint = "^2.12.1" pylint-airflow = "^0.1.0-alpha.1" pytest = "^6.2.5"

lewismc commented 2 years ago

Hi we also experienced this crash. I'll post out crash report below as well. We encountered this after formatting the DAG with black.

First, please verify that the bug is not already filled: https://github.com/PyCQA/pylint/issues/

Then create a new crash issue: https://github.com/PyCQA/pylint/issues/new?assignees=&labels=crash%2Cneeds+triage&template=BUG-REPORT.yml

Issue title: Crash 'Name' object has no attribute 'value' (if possible, be more specific about what made pylint crash) Content: When parsing the following file:

"""
A maintenance workflow that you can deploy into Airflow to periodically clean
out the DagRun, TaskInstance, Log, XCom, Job DB and SlaMiss entries to avoid
having too much data in your Airflow MetaStore.

airflow trigger_dag --conf '[curly-braces]"maxDBEntryAgeInDays":30[curly-braces]' airflow-db-cleanup

--conf options:
    maxDBEntryAgeInDays:<INT> - Optional

"""
import airflow
from airflow import settings
from airflow.configuration import conf
from airflow.models import (
    DAG,
    DagModel,
    DagRun,
    Log,
    XCom,
    SlaMiss,
    TaskInstance,
    Variable,
)

try:
    from airflow.jobs import BaseJob
except Exception as e:
    from airflow.jobs.base_job import BaseJob
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta
import dateutil.parser
import logging
import os
from sqlalchemy import func, and_
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.orm import load_only

try:
    # airflow.utils.timezone is available from v1.10 onwards
    from airflow.utils import timezone

    now = timezone.utcnow
except ImportError:
    now = datetime.utcnow

logger = logging.getLogger("airflow.task")

# airflow-db-cleanup
DAG_ID = os.path.basename(__file__).replace(".pyc", "").replace(".py", "")
START_DATE = airflow.utils.dates.days_ago(1)
# How often to Run. @daily - Once a day at Midnight (UTC)
SCHEDULE_INTERVAL = "@daily"
# Who is listed as the owner of this DAG in the Airflow Web Server
DAG_OWNER_NAME = "jsearch-ops"
# List of email address to send email alerts to if this job fails
ALERT_EMAIL_ADDRESSES = []
# Length to retain the log files if not already provided in the conf. If this
# is set to 30, the job will remove those files that arE 30 days old or older.

DEFAULT_MAX_DB_ENTRY_AGE_IN_DAYS = int(
    Variable.get("airflow_db_cleanup__max_db_entry_age_in_days", 2)
)
# Prints the database entries which will be getting deleted; set to False to avoid printing large lists and slowdown process
PRINT_DELETES = True
# Whether the job should delete the db entries or not. Included if you want to
# temporarily avoid deleting the db entries.
ENABLE_DELETE = True

# get dag model last schedule run
try:
    dag_model_last_scheduler_run = DagModel.last_scheduler_run
except AttributeError:
    dag_model_last_scheduler_run = DagModel.last_parsed_time

AIRFLOW_EXECUTOR = str(conf.get("core", "executor"))
# List of all the objects that will be deleted. Comment out the DB objects you
# want to skip.
DATABASE_OBJECTS = [
    "BaseJob",
    "DagRun",
    "TaskInstance",
    "Log",
    "XCom",
    "SlaMiss",
    "DagModel",
    "TaskReschedule",
    "TaskFail",
    "RenderedTaskInstanceFields",
    "ImportError",
]
if AIRFLOW_EXECUTOR == "CeleryExecutor":
    DATABASE_OBJECTS.extend(["Task", "TaskSet"])

session = settings.Session()

default_args = {
    "owner": DAG_OWNER_NAME,
    "depends_on_past": False,
    "email": ALERT_EMAIL_ADDRESSES,
    "email_on_failure": True,
    "email_on_retry": False,
    "start_date": START_DATE,
    "retries": 1,
    "retry_delay": timedelta(minutes=1),
}

dag = DAG(
    DAG_ID,
    default_args=default_args,
    schedule_interval=SCHEDULE_INTERVAL,
    start_date=START_DATE,
    tags=[
        "project_jsearch",
        "section_1722",
        "airflow_maintenance",
        "airflow_database_cleanup",
        "airflow_db_cleanup_dag",
    ],
)
if hasattr(dag, "doc_md"):
    dag.doc_md = __doc__
if hasattr(dag, "catchup"):
    dag.catchup = False

def print_configuration_function(**context):
    logger.info("Loading Configurations...")
    dag_run_conf = context.get("dag_run").conf
    logger.info("dag_run.conf: " + str(dag_run_conf))
    max_db_entry_age_in_days = None
    if dag_run_conf:
        max_db_entry_age_in_days = dag_run_conf.get("maxDBEntryAgeInDays", None)
    logger.info("maxDBEntryAgeInDays from dag_run.conf: " + str(dag_run_conf))
    if max_db_entry_age_in_days is None or max_db_entry_age_in_days < 1:
        logger.info(
            "maxDBEntryAgeInDays conf variable isn't included or Variable "
            + "value is less than 1. Using Default '"
            + str(DEFAULT_MAX_DB_ENTRY_AGE_IN_DAYS)
            + "'"
        )
        max_db_entry_age_in_days = DEFAULT_MAX_DB_ENTRY_AGE_IN_DAYS
    max_date = now() + timedelta(-max_db_entry_age_in_days)
    logger.info("Finished Loading Configurations")
    # logging.info("")

    logger.info("Configurations:")
    logger.info("max_db_entry_age_in_days: " + str(max_db_entry_age_in_days))
    logger.info("max_date:                 " + str(max_date))
    logger.info("enable_delete:            " + str(ENABLE_DELETE))
    logger.info("session:                  " + str(session))
    # logging.info("")

    logger.info("Setting max_execution_date to XCom for Downstream Processes")
    context["ti"].xcom_push(key="max_date", value=max_date.isoformat())

print_configuration = PythonOperator(
    task_id="print_configuration",
    python_callable=print_configuration_function,
    provide_context=True,
    dag=dag,
)

def cleanup_function(**context):

    database_objects_dict = {
        "BaseJob": {
            "airflow_db_model": BaseJob,
            "age_check_column": BaseJob.latest_heartbeat,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
        "DagRun": {
            "airflow_db_model": DagRun,
            "age_check_column": DagRun.execution_date,
            "keep_last": True,
            "keep_last_filters": [DagRun.external_trigger.is_(False)],
            "keep_last_group_by": DagRun.dag_id,
        },
        "TaskInstance": {
            "airflow_db_model": TaskInstance,
            "age_check_column": TaskInstance.run_id,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
        "Log": {
            "airflow_db_model": Log,
            "age_check_column": Log.dttm,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
        "XCom": {
            "airflow_db_model": XCom,
            "age_check_column": XCom.execution_date,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
        "SlaMiss": {
            "airflow_db_model": SlaMiss,
            "age_check_column": SlaMiss.execution_date,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
        "DagModel": {
            "airflow_db_model": DagModel,
            "age_check_column": dag_model_last_scheduler_run,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        },
    }

    # Check for TaskReschedule model
    try:
        from airflow.models import TaskReschedule

        database_objects_dict["TaskReschedule"] = {
            "airflow_db_model": TaskReschedule,
            "age_check_column": TaskReschedule.run_id,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        }

    except Exception as e:
        logger.error(e)

    # Check for TaskFail model
    try:
        from airflow.models import TaskFail

        database_objects_dict["TaskFail"] = {
            "airflow_db_model": TaskFail,
            "age_check_column": TaskFail.execution_date,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        }

    except Exception as e:
        logger.error(e)

    # Check for RenderedTaskInstanceFields model
    try:
        from airflow.models import RenderedTaskInstanceFields

        database_objects_dict["RenderedTaskInstanceFields"] = {
            "airflow_db_model": RenderedTaskInstanceFields,
            "age_check_column": RenderedTaskInstanceFields.execution_date,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        }

    except Exception as e:
        logger.error(e)

    # Check for ImportError model
    try:
        from airflow.models import ImportError

        database_objects_dict["ImportError"] = {
            "airflow_db_model": ImportError,
            "age_check_column": ImportError.timestamp,
            "keep_last": False,
            "keep_last_filters": None,
            "keep_last_group_by": None,
        }

    except Exception as e:
        logger.error(e)

    # Check for celery executor
    AIRFLOW_EXECUTOR = str(conf.get("core", "executor"))
    logger.info("Airflow Executor: " + str(AIRFLOW_EXECUTOR))
    if AIRFLOW_EXECUTOR == "CeleryExecutor":
        logger.info("Including Celery Modules")
        try:
            from celery.backends.database.models import Task, TaskSet

            database_objects_dict["Task"] = {
                "airflow_db_model": Task,
                "age_check_column": Task.date_done,
                "keep_last": False,
                "keep_last_filters": None,
                "keep_last_group_by": None,
            }
            database_objects_dict["TaskSet"] = {
                "airflow_db_model": TaskSet,
                "age_check_column": TaskSet.date_done,
                "keep_last": False,
                "keep_last_filters": None,
                "keep_last_group_by": None,
            }

        except Exception as e:
            logger.error(e)

    logger.info("Retrieving max_execution_date from XCom")
    max_date = context["ti"].xcom_pull(
        task_ids=print_configuration.task_id, key="max_date"
    )
    max_date = dateutil.parser.parse(max_date)  # stored as iso8601 str in xcom

    object_name = str(context["params"].get("object_name")).strip()

    airflow_db_model = database_objects_dict[object_name].get("airflow_db_model")
    age_check_column = database_objects_dict[object_name].get("age_check_column")
    keep_last = database_objects_dict[object_name].get("keep_last")
    keep_last_filters = database_objects_dict[object_name].get("keep_last_filters")
    keep_last_group_by = database_objects_dict[object_name].get("keep_last_group_by")

    logging.info("Configurations:")
    logging.info("max_date:                 " + str(max_date))
    logging.info("enable_delete:            " + str(ENABLE_DELETE))
    logging.info("session:                  " + str(session))
    logging.info("airflow_db_model:         " + str(airflow_db_model))
    logging.info("age_check_column:         " + str(age_check_column))
    logging.info("keep_last:                " + str(keep_last))
    logging.info("keep_last_filters:        " + str(keep_last_filters))
    logging.info("keep_last_group_by:       " + str(keep_last_group_by))

    # logger.info("")

    logger.info("Running Cleanup Process...")

    try:
        query = session.query(airflow_db_model).options(load_only(age_check_column))

        logger.info("INITIAL QUERY : " + str(query))

        if keep_last:

            subquery = session.query(func.max(DagRun.execution_date))
            # workaround for MySQL "table specified twice" issue
            # https://github.com/teamclairvoyant/airflow-maintenance-dags/issues/41
            if keep_last_filters is not None:
                for entry in keep_last_filters:
                    subquery = subquery.filter(entry)

                logger.info("SUB QUERY [keep_last_filters]: " + str(subquery))

            if keep_last_group_by is not None:
                subquery = subquery.group_by(keep_last_group_by)
                logger.info("SUB QUERY [keep_last_group_by]: " + str(subquery))

            subquery = subquery.from_self()

            query = query.filter(
                and_(age_check_column.notin_(subquery)),
                and_(age_check_column <= max_date),
            )
            logger.info("query-if:" + str(query))
        else:
            query = query.filter(
                age_check_column <= max_date,
            )
            logger.info("query-else:" + str(query))
            logger.info("max_date: " + str(max_date))

        if PRINT_DELETES:

            entries_to_delete = query.all()

            if entries_to_delete:
                logger.info("entries_to_delete: " + str(entries_to_delete))
            else:
                logger.info("empty entries_to_delete")

            logger.info("Query: " + str(query))
            logger.info(
                "Process will be Deleting the following "
                + str(airflow_db_model.__name__)
                + "(s):"
            )
            for entry in entries_to_delete:
                logger.info(
                    "\tEntry: "
                    + str(entry)
                    + ", Date: "
                    + str(entry.__dict__[str(age_check_column).split(".")[1]])
                )

            logger.info(
                "Process will be Deleting "
                + str(len(entries_to_delete))
                + " "
                + str(airflow_db_model.__name__)
                + "(s)"
            )
        else:
            logger.warn(
                "You've opted to skip printing the db entries to be deleted. Set PRINT_DELETES to True to show entries!!!"
            )

        if ENABLE_DELETE:
            logger.info("Performing Delete...")
            # using bulk delete
            query.delete(synchronize_session=False)
            session.commit()
            logger.info("Finished Performing Delete")
        else:
            logger.warn(
                "You've opted to skip deleting the db entries. Set ENABLE_DELETE to True to delete entries!!!"
            )

        logger.info("Finished Running Cleanup Process")

    except ProgrammingError as e:
        logger.error(e)
        logger.error(
            str(airflow_db_model) + " is not present in the metadata. Skipping..."
        )

for db_object in DATABASE_OBJECTS:

    cleanup_op = PythonOperator(
        task_id="cleanup_" + str(db_object),
        python_callable=cleanup_function,
        params={"object_name": db_object},
        dag=dag,
    )

    print_configuration.set_downstream(cleanup_op)

pylint crashed with a AttributeError and with the following stacktrace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1034, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1069, in _check_file
    check_astroid_module(ast_node)
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1203, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1250, in _check_astroid_module
    walker.walk(node)
  File "/usr/local/lib/python3.9/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/lmcgibbn/Library/Python/3.9/lib/python/site-packages/pylint_airflow/checkers/dag.py", line 101, in visit_module
    dagid, dagnode = _find_dag(assign.value, func)
  File "/Users/lmcgibbn/Library/Python/3.9/lib/python/site-packages/pylint_airflow/checkers/dag.py", line 93, in _find_dag
    return call_node.args[0].value, call_node
AttributeError: 'Name' object has no attribute 'value'

Issue title: Crash 'Name' object has no attribute 'value' (if possible, be more specific about what made pylint crash) Content: When parsing the following file:

"""
A maintenance workflow that you can deploy into Airflow to periodically delete broken DAG file(s).

airflow trigger_dag airflow-delete-broken-dags

"""
from airflow.models import DAG, ImportError
from airflow.operators.python_operator import PythonOperator
from airflow import settings
from datetime import timedelta
import os
import os.path
import socket
import logging
import airflow

# airflow-delete-broken-dags
DAG_ID = os.path.basename(__file__).replace(".pyc", "").replace(".py", "")
START_DATE = airflow.utils.dates.days_ago(1)
# How often to Run. @daily - Once a day at Midnight
SCHEDULE_INTERVAL = "@daily"
# Who is listed as the owner of this DAG in the Airflow Web Server
DAG_OWNER_NAME = "operations"
# List of email address to send email alerts to if this job fails
ALERT_EMAIL_ADDRESSES = []
# Whether the job should delete the logs or not. Included if you want to
# temporarily avoid deleting the logs
ENABLE_DELETE = True

default_args = {
    "owner": DAG_OWNER_NAME,
    "email": ALERT_EMAIL_ADDRESSES,
    "email_on_failure": True,
    "email_on_retry": False,
    "start_date": START_DATE,
    "retries": 1,
    "retry_delay": timedelta(minutes=1),
}

dag = DAG(
    DAG_ID,
    default_args=default_args,
    schedule_interval=SCHEDULE_INTERVAL,
    start_date=START_DATE,
    tags=[
        "project_jsearch",
        "section_1722",
        "airflow_maintenance",
        "import_error_cleanup",
        "airflow_delete_broken_dags_dag",
    ],
)
if hasattr(dag, "doc_md"):
    dag.doc_md = __doc__
if hasattr(dag, "catchup"):
    dag.catchup = False

def delete_broken_dag_files(**context):

    logging.info("Starting to run Clear Process")

    try:
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        logging.info("Running on Machine with Host Name: " + host_name)
        logging.info("Running on Machine with IP: " + host_ip)
    except Exception as e:
        print("Unable to get Host Name and IP: " + str(e))

    session = settings.Session()

    logging.info("Configurations:")
    logging.info("enable_delete:            " + str(ENABLE_DELETE))
    logging.info("session:                  " + str(session))
    logging.info("")

    errors = session.query(ImportError).all()

    logging.info("Process will be removing broken DAG file(s) from the file system:")
    for error in errors:
        logging.info("\tFile: " + str(error.filename))
    logging.info("Process will be Deleting " + str(len(errors)) + " DAG file(s)")

    if ENABLE_DELETE:
        logging.info("Performing Delete...")
        for error in errors:
            if os.path.exists(error.filename):
                os.remove(error.filename)
            session.delete(error)
        logging.info("Finished Performing Delete")
    else:
        logging.warn("You're opted to skip Deleting the DAG file(s)!!!")

    logging.info("Finished")

delete_broken_dag_files = PythonOperator(
    task_id="delete_broken_dag_files",
    python_callable=delete_broken_dag_files,
    provide_context=True,
    dag=dag,
)

pylint crashed with a AttributeError and with the following stacktrace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1034, in _check_files
    self._check_file(get_ast, check_astroid_module, file)
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1069, in _check_file
    check_astroid_module(ast_node)
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1203, in check_astroid_module
    retval = self._check_astroid_module(
  File "/usr/local/lib/python3.9/site-packages/pylint/lint/pylinter.py", line 1250, in _check_astroid_module
    walker.walk(node)
  File "/usr/local/lib/python3.9/site-packages/pylint/utils/ast_walker.py", line 72, in walk
    callback(astroid)
  File "/Users/lmcgibbn/Library/Python/3.9/lib/python/site-packages/pylint_airflow/checkers/dag.py", line 101, in visit_module
    dagid, dagnode = _find_dag(assign.value, func)
  File "/Users/lmcgibbn/Library/Python/3.9/lib/python/site-packages/pylint_airflow/checkers/dag.py", line 93, in _find_dag
    return call_node.args[0].value, call_node
AttributeError: 'Name' object has no attribute 'value'