context.pyi 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. # This stub exists to "fake" the Context class as a TypedDict to provide
  19. # better typehint and editor support.
  20. #
  21. # Unfortunately 'conn', 'macros', 'var.json', and 'var.value' need to be
  22. # annotated as Any and loose discoverability because we don't know what
  23. # attributes are injected at runtime, and giving them a class would trigger
  24. # undefined attribute errors from Mypy. Hopefully there will be a mechanism to
  25. # declare "these are defined, but don't error if others are accessed" someday.
  26. from __future__ import annotations
  27. from typing import Any, Collection, Container, Iterable, Iterator, Mapping, Sequence, overload
  28. from pendulum import DateTime
  29. from sqlalchemy.orm import Session
  30. from airflow.configuration import AirflowConfigParser
  31. from airflow.datasets import Dataset, DatasetAlias, DatasetAliasEvent
  32. from airflow.models.baseoperator import BaseOperator
  33. from airflow.models.dag import DAG
  34. from airflow.models.dagrun import DagRun
  35. from airflow.models.dataset import DatasetEvent
  36. from airflow.models.param import ParamsDict
  37. from airflow.models.taskinstance import TaskInstance
  38. from airflow.serialization.pydantic.dag_run import DagRunPydantic
  39. from airflow.serialization.pydantic.dataset import DatasetEventPydantic
  40. from airflow.serialization.pydantic.taskinstance import TaskInstancePydantic
  41. from airflow.typing_compat import TypedDict
  42. KNOWN_CONTEXT_KEYS: set[str]
  43. class _VariableAccessors(TypedDict):
  44. json: Any
  45. value: Any
  46. class VariableAccessor:
  47. def __init__(self, *, deserialize_json: bool) -> None: ...
  48. def get(self, key, default: Any = ...) -> Any: ...
  49. class ConnectionAccessor:
  50. def get(self, key: str, default_conn: Any = None) -> Any: ...
  51. class OutletEventAccessor:
  52. def __init__(
  53. self,
  54. *,
  55. extra: dict[str, Any],
  56. raw_key: str | Dataset | DatasetAlias,
  57. dataset_alias_events: list[DatasetAliasEvent],
  58. ) -> None: ...
  59. def add(self, dataset: Dataset | str, extra: dict[str, Any] | None = None) -> None: ...
  60. extra: dict[str, Any]
  61. raw_key: str | Dataset | DatasetAlias
  62. dataset_alias_events: list[DatasetAliasEvent]
  63. class OutletEventAccessors(Mapping[str, OutletEventAccessor]):
  64. def __iter__(self) -> Iterator[str]: ...
  65. def __len__(self) -> int: ...
  66. def __getitem__(self, key: str | Dataset | DatasetAlias) -> OutletEventAccessor: ...
  67. class InletEventsAccessor(Sequence[DatasetEvent]):
  68. @overload
  69. def __getitem__(self, key: int) -> DatasetEvent: ...
  70. @overload
  71. def __getitem__(self, key: slice) -> Sequence[DatasetEvent]: ...
  72. def __len__(self) -> int: ...
  73. class InletEventsAccessors(Mapping[str, InletEventsAccessor]):
  74. def __init__(self, inlets: list, *, session: Session) -> None: ...
  75. def __iter__(self) -> Iterator[str]: ...
  76. def __len__(self) -> int: ...
  77. def __getitem__(self, key: int | str | Dataset | DatasetAlias) -> InletEventsAccessor: ...
  78. # NOTE: Please keep this in sync with the following:
  79. # * KNOWN_CONTEXT_KEYS in airflow/utils/context.py
  80. # * Table in docs/apache-airflow/templates-ref.rst
  81. class Context(TypedDict, total=False):
  82. conf: AirflowConfigParser
  83. conn: Any
  84. dag: DAG
  85. dag_run: DagRun | DagRunPydantic
  86. data_interval_end: DateTime
  87. data_interval_start: DateTime
  88. outlet_events: OutletEventAccessors
  89. ds: str
  90. ds_nodash: str
  91. exception: BaseException | str | None
  92. execution_date: DateTime
  93. expanded_ti_count: int | None
  94. inlets: list
  95. inlet_events: InletEventsAccessors
  96. logical_date: DateTime
  97. macros: Any
  98. map_index_template: str
  99. next_ds: str | None
  100. next_ds_nodash: str | None
  101. next_execution_date: DateTime | None
  102. outlets: list
  103. params: ParamsDict
  104. prev_data_interval_start_success: DateTime | None
  105. prev_data_interval_end_success: DateTime | None
  106. prev_ds: str | None
  107. prev_ds_nodash: str | None
  108. prev_execution_date: DateTime | None
  109. prev_execution_date_success: DateTime | None
  110. prev_start_date_success: DateTime | None
  111. prev_end_date_success: DateTime | None
  112. reason: str | None
  113. run_id: str
  114. task: BaseOperator
  115. task_instance: TaskInstance | TaskInstancePydantic
  116. task_instance_key_str: str
  117. test_mode: bool
  118. templates_dict: Mapping[str, Any] | None
  119. ti: TaskInstance | TaskInstancePydantic
  120. tomorrow_ds: str
  121. tomorrow_ds_nodash: str
  122. triggering_dataset_events: Mapping[str, Collection[DatasetEvent | DatasetEventPydantic]]
  123. ts: str
  124. ts_nodash: str
  125. ts_nodash_with_tz: str
  126. try_number: int | None
  127. var: _VariableAccessors
  128. yesterday_ds: str
  129. yesterday_ds_nodash: str
  130. class AirflowContextDeprecationWarning(DeprecationWarning): ...
  131. @overload
  132. def context_merge(context: Context, additions: Mapping[str, Any], **kwargs: Any) -> None: ...
  133. @overload
  134. def context_merge(context: Context, additions: Iterable[tuple[str, Any]], **kwargs: Any) -> None: ...
  135. @overload
  136. def context_merge(context: Context, **kwargs: Any) -> None: ...
  137. def context_update_for_unmapped(context: Context, task: BaseOperator) -> None: ...
  138. def context_copy_partial(source: Context, keys: Container[str]) -> Context: ...
  139. def lazy_mapping_from_context(source: Context) -> Mapping[str, Any]: ...
  140. def context_get_outlet_events(context: Context) -> OutletEventAccessors: ...