# Buggy code
def execute(self, context: Dict):
    if isinstance(self.execution_date, datetime.datetime):
        execution_date = self.execution_date
    elif isinstance(self.execution_date, str):
        execution_date = timezone.parse(self.execution_date)
        self.execution_date = execution_date
    else:
        execution_date = timezone.utcnow()

    run_id = DagRun.generate_run_id(DagRunType.MANUAL, execution_date)
    try:
        # Ignore MyPy type for self.execution_date
        # because it doesn't pick up the timezone.parse() for strings
        dag_run = trigger_dag(
            dag_id=self.trigger_dag_id,
            run_id=run_id,
            conf=self.conf,
            execution_date=self.execution_date,
            replace_microseconds=False,
        )

    except DagRunAlreadyExists as e:
        if self.reset_dag_run:
            self.log.info("Clearing %s on %s", self.trigger_dag_id, self.execution_date)

            # Get target dag object and call clear()

            dag_model = DagModel.get_current(self.trigger_dag_id)
            if dag_model is None:
                raise DagNotFound(f"Dag id {self.trigger_dag_id} not found in DagModel")

            dag_bag = DagBag(dag_folder=dag_model.fileloc, read_dags_from_db=True)

            dag = dag_bag.get_dag(self.trigger_dag_id)

            dag.clear(start_date=self.execution_date, end_date=self.execution_date)
        else:
            raise e

    if self.wait_for_completion:
        # wait for dag to complete
        while True:
            self.log.info(
                "Waiting for %s on %s to become allowed state %s ...",
                self.trigger_dag_id,
                dag_run.execution_date,
                self.allowed_states,
            )
            time.sleep(self.poke_interval)

            dag_run.refresh_from_db()
            state = dag_run.state
            if state in self.failed_states:
                raise AirflowException(
                    f"{self.trigger_dag_id} failed with failed states {state}"
                )
            if state in self.allowed_states:
                self.log.info(
                    "%s finished with allowed state %s", self.trigger_dag_id, state
                )
                return


# Traceback (UnboundLocalError)
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/airflow/models/taskinstance.py", line 984, in _run_raw_task
result = task_copy.execute(context=context)
File "/usr/local/airflow/plugins/custom_trigger_operator.py", line 138, in execute
dag_run.execution_date,
UnboundLocalError: local variable 'dag_run' referenced before assignment

# Fixed code
def execute(self, context: Dict):
    if isinstance(self.execution_date, datetime.datetime):
        execution_date = self.execution_date
    elif isinstance(self.execution_date, str):
        execution_date = timezone.parse(self.execution_date)
        self.execution_date = execution_date
    else:
        execution_date = timezone.utcnow()

    run_id = DagRun.generate_run_id(DagRunType.MANUAL, execution_date)
    try:
        # Ignore MyPy type for self.execution_date
        # because it doesn't pick up the timezone.parse() for strings
        dag_run = trigger_dag(
            dag_id=self.trigger_dag_id,
            run_id=run_id,
            conf=self.conf,
            execution_date=self.execution_date,
            replace_microseconds=False,
        )

    except DagRunAlreadyExists as e:
        if self.reset_dag_run:
            self.log.info("Clearing %s on %s", self.trigger_dag_id, self.execution_date)

            # Get target dag object and call clear()

            dag_model = DagModel.get_current(self.trigger_dag_id)
            if dag_model is None:
                raise DagNotFound(f"Dag id {self.trigger_dag_id} not found in DagModel")

            dag_bag = DagBag(dag_folder=dag_model.fileloc, read_dags_from_db=True)

            dag = dag_bag.get_dag(self.trigger_dag_id)

            dag.clear(start_date=self.execution_date, end_date=self.execution_date)

            dag_run = DagRun.find(dag_id=dag.dag_id, run_id=run_id)[0]
        else:
            raise e

    if self.wait_for_completion:
        # wait for dag to complete
        while True:
            self.log.info(
                "Waiting for %s on %s to become allowed state %s ...",
                self.trigger_dag_id,
                dag_run.execution_date,
                self.allowed_states,
            )
            time.sleep(self.poke_interval)

            dag_run.refresh_from_db()
            state = dag_run.state
            if state in self.failed_states:
                raise AirflowException(
                    f"{self.trigger_dag_id} failed with failed states {state}"
                )
            if state in self.allowed_states:
                self.log.info(
                    "%s finished with allowed state %s", self.trigger_dag_id, state
                )
                return