class_docstr: Waits for a different DAG, task group, or task to complete for a specific logical date.
  
  If both `external_task_group_id` and `external_task_id` are ``None`` (default), the sensor
  waits for the DAG.
  Values for `external_task_group_id` and `external_task_id` can't be set at the same time.
  
  By default, the ExternalTaskSensor will wait for the external task to
  succeed, at which point it will also succeed. However, by default it will
  *not* fail if the external task fails, but will continue to check the status
  until the sensor times out (thus giving you time to retry the external task
  without also having to clear the sensor).
  
  By default, the ExternalTaskSensor will not skip if the external task skips.
  To change this, simply set ``skipped_states=[TaskInstanceState.SKIPPED]``.
  Note that if you are monitoring multiple tasks, and one enters error state
  and the other enters a skipped state, then the external task will react to
  whichever one it sees first. If both happen together, then the failed state
  takes priority.
  
  It is possible to alter the default behavior by setting states which
  cause the sensor to fail, e.g. by setting ``allowed_states=[DagRunState.FAILED]``
  and ``failed_states=[DagRunState.SUCCESS]`` you will flip the behaviour to
  get a sensor which goes green when the external task *fails* and immediately
  goes red if the external task *succeeds*!
  
  Note that ``soft_fail`` is respected when examining the failed_states. Thus
  if the external task enters a failed state and ``soft_fail == True`` the
  sensor will _skip_ rather than fail. As a result, setting ``soft_fail=True``
  and ``failed_states=[DagRunState.SKIPPED]`` will result in the sensor
  skipping if the external task skips. However, this is a contrived
  example---consider using ``skipped_states`` if you would like this
  behaviour. Using ``skipped_states`` allows the sensor to skip if the target
  fails, but still enter failed state on timeout. Using ``soft_fail == True``
  as above will cause the sensor to skip if the target fails, but also if it
  times out.
  
  :param external_dag_id: The dag_id that contains the task you want to
      wait for. (templated)
  :param external_task_id: The task_id that contains the task you want to
      wait for. (templated)
  :param external_task_ids: The list of task_ids that you want to wait for. (templated)
      If ``None`` (default value) the sensor waits for the DAG. Either
      external_task_id or external_task_ids can be passed to
      ExternalTaskSensor, but not both.
  :param external_task_group_id: The task_group_id that contains the task you want to
      wait for. (templated)
  :param allowed_states: Iterable of allowed states, default is ``['success']``
  :param skipped_states: Iterable of states to make this task mark as skipped, default is ``None``
  :param failed_states: Iterable of failed or dis-allowed states, default is ``None``
  :param execution_delta: time difference with the previous execution to
      look at, the default is the same logical date as the current task or DAG.
      For yesterday, use [positive!] datetime.timedelta(days=1). Either
      execution_delta or execution_date_fn can be passed to
      ExternalTaskSensor, but not both.
  :param execution_date_fn: function that receives the current execution's logical date as the first
      positional argument and optionally any number of keyword arguments available in the
      context dictionary, and returns the desired logical dates to query.
      Either execution_delta or execution_date_fn can be passed to ExternalTaskSensor,
      but not both.
  :param check_existence: Set to `True` to check if the external task exists (when
      external_task_id is not None) or check if the DAG to wait for exists (when
      external_task_id is None), and immediately cease waiting if the external task
      or DAG does not exist (default value: False).
  :param poll_interval: polling period in seconds to check for the status
  :param deferrable: Run sensor in deferrable mode
class_docstr_tok: ["Waits","for","a","different","DAG",",","task","group",",","or","task","to","complete","for","a","specific","logical","date",".","If","both","`","external_task_group_id","`","and","`","external_task_id","`","are","``","None","``","(","default",")",",","the","sensor","waits","for","the","DAG",".","Values","for","`","external_task_group_id","`","and","`","external_task_id","`","ca","n't","be","set","at","the","same","time",".","By","default",",","the","ExternalTaskSensor","will","wait","for","the","external","task","to","succeed",",","at","which","point","it","will","also","succeed",".","However",",","by","default","it","will","*","not","*","fail","if","the","external","task","fails",",","but","will","continue","to","check","the","status","until","the","sensor","times","out","(","thus","giving","you","time","to","retry","the","external","task","without","also","having","to","clear","the","sensor",")",".","By","default",",","the","ExternalTaskSensor","will","not","skip","if","the","external","task","skips",".","To","change","this",",","simply","set","``","skipped_states=","[","TaskInstanceState.SKIPPED","]","``",".","Note","that","if","you","are","monitoring","multiple","tasks",",","and","one","enters","error","state","and","the","other","enters","a","skipped","state",",","then","the","external","task","will","react","to","whichever","one","it","sees","first",".","If","both","happen","together",",","then","the","failed","state","takes","priority",".","It","is","possible","to","alter","the","default","behavior","by","setting","states","which","cause","the","sensor","to","fail",",","e.g",".","by","setting","``","allowed_states=","[","DagRunState.FAILED","]","``","and","``","failed_states=","[","DagRunState.SUCCESS","]","``","you","will","flip","the","behaviour","to","get","a","sensor","which","goes","green","when","the","external","task","*","fails","*","and","immediately","goes","red","if","the","external","task","*","succeeds","*","!","Note","that","``","soft_fail","``","is","respected","when","examining","the","failed_states",".","Thus","if","the","external","task","enters","a","failed","state","and","``","soft_fail","==","True","``","the","sensor","will","_skip_","rather","than","fail",".","As","a","result",",","setting","``","soft_fail=True","``","and","``","failed_states=","[","DagRunState.SKIPPED","]","``","will","result","in","the","sensor","skipping","if","the","external","task","skips",".","However",",","this","is","a","contrived","example","--","-consider","using","``","skipped_states","``","if","you","would","like","this","behaviour",".","Using","``","skipped_states","``","allows","the","sensor","to","skip","if","the","target","fails",",","but","still","enter","failed","state","on","timeout",".","Using","``","soft_fail","==","True","``","as","above","will","cause","the","sensor","to","skip","if","the","target","fails",",","but","also","if","it","times","out",".",":","param","external_dag_id",":","The","dag_id","that","contains","the","task","you","want","to","wait","for",".","(","templated",")",":","param","external_task_id",":","The","task_id","that","contains","the","task","you","want","to","wait","for",".","(","templated",")",":","param","external_task_ids",":","The","list","of","task_ids","that","you","want","to","wait","for",".","(","templated",")","If","``","None","``","(","default","value",")","the","sensor","waits","for","the","DAG",".","Either","external_task_id","or","external_task_ids","can","be","passed","to","ExternalTaskSensor",",","but","not","both",".",":","param","external_task_group_id",":","The","task_group_id","that","contains","the","task","you","want","to","wait","for",".","(","templated",")",":","param","allowed_states",":","Iterable","of","allowed","states",",","default","is","``","[","'success","'","]","``",":","param","skipped_states",":","Iterable","of","states","to","make","this","task","mark","as","skipped",",","default","is","``","None","``",":","param","failed_states",":","Iterable","of","failed","or","dis-allowed","states",",","default","is","``","None","``",":","param","execution_delta",":","time","difference","with","the","previous","execution","to","look","at",",","the","default","is","the","same","logical","date","as","the","current","task","or","DAG",".","For","yesterday",",","use","[","positive","!","]","datetime.timedelta","(","days=1",")",".","Either","execution_delta","or","execution_date_fn","can","be","passed","to","ExternalTaskSensor",",","but","not","both",".",":","param","execution_date_fn",":","function","that","receives","the","current","execution","'s","logical","date","as","the","first","positional","argument","and","optionally","any","number","of","keyword","arguments","available","in","the","context","dictionary",",","and","returns","the","desired","logical","dates","to","query",".","Either","execution_delta","or","execution_date_fn","can","be","passed","to","ExternalTaskSensor",",","but","not","both",".",":","param","check_existence",":","Set","to","`","True","`","to","check","if","the","external","task","exists","(","when","external_task_id","is","not","None",")","or","check","if","the","DAG","to","wait","for","exists","(","when","external_task_id","is","None",")",",","and","immediately","cease","waiting","if","the","external","task","or","DAG","does","not","exist","(","default","value",":","False",")",".",":","param","poll_interval",":","polling","period","in","seconds","to","check","for","the","status",":","param","deferrable",":","Run","sensor","in","deferrable","mode"]
code: def _check_for_existence(self, session) -> None:
      dag_to_wait = DagModel.get_current(self.external_dag_id, session)
  
      if not dag_to_wait:
          raise AirflowException(
              f"The external DAG {self.external_dag_id} does not exist."
          )
  
      if not os.path.exists(correct_maybe_zipped(dag_to_wait.fileloc)):
          raise AirflowException(
              f"The external DAG {self.external_dag_id} was deleted."
          )
  
      if self.external_task_ids:
          refreshed_dag_info = DagBag(dag_to_wait.fileloc).get_dag(
              self.external_dag_id
          )
          for external_task_id in self.external_task_ids:
              if not refreshed_dag_info.has_task(external_task_id):
                  raise AirflowException(
                      f"The external task {external_task_id} in "
                      f"DAG {self.external_dag_id} does not exist."
                  )
  
      if self.external_task_group_id:
          refreshed_dag_info = DagBag(dag_to_wait.fileloc).get_dag(
              self.external_dag_id
          )
          if not refreshed_dag_info.has_task_group(
              self.external_task_group_id
          ):
              raise AirflowException(
                  f"The external task group '{self.external_task_group_id}' in "
                  f"DAG '{self.external_dag_id}' does not exist."
              )
  
      self._has_checked_existence = True
  
imports: from __future__ import annotations
  import datetime
  import os
  import warnings
  from typing import TYPE_CHECKING, Any, Callable, Collection, Iterable
  import attr
  from sqlalchemy import func
  from airflow.configuration import conf
  from airflow.exceptions import AirflowException, AirflowSkipException, RemovedInAirflow3Warning
  from airflow.models.baseoperatorlink import BaseOperatorLink
  from airflow.models.dag import DagModel
  from airflow.models.dagbag import DagBag
  from airflow.models.dagrun import DagRun
  from airflow.models.taskinstance import TaskInstance
  from airflow.operators.empty import EmptyOperator
  from airflow.sensors.base import BaseSensorOperator
  from airflow.triggers.external_task import TaskStateTrigger
  from airflow.utils.file import correct_maybe_zipped
  from airflow.utils.helpers import build_airflow_url_with_query
  from airflow.utils.session import NEW_SESSION, provide_session
  from airflow.utils.sqlalchemy import tuple_in_condition
  from airflow.utils.state import State, TaskInstanceState
  from airflow.utils.timezone import utcnow
label_desc: Use image node_id 6 for calling the ExternalTaskSensor obj's underlying member method code with example usage: obj._check_for_existence(session) without return types