code: def annotations_to_key(
      annotations: dict[str, str]
  ) -> TaskInstanceKey:
      """Build a TaskInstanceKey based on pod annotations."""
      log.debug("Creating task key for annotations %s", annotations)
      dag_id = annotations["dag_id"]
      task_id = annotations["task_id"]
      try_number = int(annotations["try_number"])
      annotation_run_id = annotations.get("run_id")
      map_index = int(annotations.get("map_index", -1))
  
      # Compat: Look up the run_id from the TI table!
      from airflow.models.dagrun import DagRun
      from airflow.models.taskinstance import (
          TaskInstance,
          TaskInstanceKey,
      )
      from airflow.settings import Session
  
      if not annotation_run_id and "execution_date" in annotations:
          execution_date = pendulum.parse(annotations["execution_date"])
          # Do _not_ use create-session, we don't want to expunge
          session = Session()
  
          task_instance_run_id = (
              session.query(TaskInstance.run_id)
              .join(TaskInstance.dag_run)
              .filter(
                  TaskInstance.dag_id == dag_id,
                  TaskInstance.task_id == task_id,
                  DagRun.execution_date == execution_date,
              )
              .scalar()
          )
      else:
          task_instance_run_id = annotation_run_id
  
      return TaskInstanceKey(
          dag_id=dag_id,
          task_id=task_id,
          run_id=task_instance_run_id,
          try_number=try_number,
          map_index=map_index,
      )
  
file_path: airflow/airflow/providers/cncf/kubernetes/kubernetes_helper_functions.py
imports: from __future__ import annotations
  import logging
  import secrets
  import string
  from typing import TYPE_CHECKING
  import pendulum
  from slugify import slugify
  from airflow.compat.functools import cache
  from airflow.configuration import conf
label_desc: Use image node_id 4 for calling a global function with example usage: annotations_to_key(annotations) and returns: TaskInstanceKey