Skip to content

Fix airflow.utils.context deprecation shim returning a module object - #72593

Closed
FrankYang0529 wants to merge 1 commit into
apache:mainfrom
FrankYang0529:airflow-fix-utils-context-shim
Closed

FrankYang0529 wants to merge 1 commit into
apache:mainfrom
FrankYang0529:airflow-fix-utils-context-shim

Conversation

@FrankYang0529

Copy link
Copy Markdown
Member

Why

  • Remove TaskInstance and TaskLogReader unused methods #59922 turned airflow.utils.context into a deprecation shim, but every add_deprecated_classes target names only the module, for example "Context": "airflow.sdk.definitions.context".
  • deprecation_tools splits the target on the last dot, so the lookup becomes getattr(airflow.sdk.definitions, "context") and returns the submodule itself. from airflow.utils.context import Context succeeds, but it shows TypeError: 'module' object is not callable when users call it.

How

  • Fill in the full target for the three names that exist in the SDK: airflow.sdk.definitions.context.KNOWN_CONTEXT_KEYS, airflow.sdk.Context and airflow.sdk.definitions.context.context_merge.
  • Drop context_copy_partial, because it doesn't exist.

Verification

  • Unit test: uv run --frozen --project airflow-core pytest airflow-core/tests/unit/utils/test_context.py
  • Integration test:
  1. Create dags using old paths
mkdir -p files/dags

cat > files/dags/legacy_known_context_keys.py <<'EOF'
from __future__ import annotations

import pendulum

from airflow.sdk import DAG, BaseOperator
from airflow.utils.context import KNOWN_CONTEXT_KEYS


class KnownContextKeysOperator(BaseOperator):
    """Filter the runtime context with the KNOWN_CONTEXT_KEYS set."""

    def execute(self, context):
        self.log.info("KNOWN_CONTEXT_KEYS resolved to a %s", type(KNOWN_CONTEXT_KEYS).__name__)
        unknown = sorted(key for key in context if key not in KNOWN_CONTEXT_KEYS)
        self.log.info("context keys missing from KNOWN_CONTEXT_KEYS: %s", unknown)
        return unknown


with DAG(
    dag_id="legacy_known_context_keys",
    start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
    schedule=None,
    catchup=False,
):
    KnownContextKeysOperator(task_id="check_known_context_keys")
EOF

cat > files/dags/legacy_context.py <<'EOF'
from __future__ import annotations

import pendulum

from airflow.sdk import DAG, BaseOperator
from airflow.utils.context import Context


class ContextOperator(BaseOperator):
    """Build a Context from a subset of the runtime context."""

    def execute(self, context: Context):
        self.log.info("Context resolved to %r", Context)
        subset = Context(ds=context["ds"], run_id=context["run_id"])
        self.log.info("Context(...) built: %s", subset)
        return subset


with DAG(
    dag_id="legacy_context",
    start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
    schedule=None,
    catchup=False,
):
    ContextOperator(task_id="build_context_subset")
EOF

cat > files/dags/legacy_context_merge.py <<'EOF'
from __future__ import annotations

import pendulum

from airflow.sdk import DAG, BaseOperator
from airflow.utils.context import context_merge


class ContextMergeOperator(BaseOperator):
    """Merge extra keys into the runtime context with context_merge."""

    def execute(self, context):
        self.log.info("context_merge resolved to %r", context_merge)
        context_merge(context, region="tw", attempt=context["ti"].try_number)
        self.log.info("after context_merge: region=%s attempt=%s", context["region"], context["attempt"])
        return {"region": context["region"], "attempt": context["attempt"]}


with DAG(
    dag_id="legacy_context_merge",
    start_date=pendulum.datetime(2026, 1, 1, tz="UTC"),
    schedule=None,
    catchup=False,
):
    ContextMergeOperator(task_id="merge_into_context")
EOF
  1. Run dags
breeze run --backend sqlite bash -c '
  export AIRFLOW__CORE__DAGS_FOLDER=/files/dags AIRFLOW__CORE__LOAD_EXAMPLES=False
  airflow db migrate >/dev/null 2>&1
  for d in legacy_known_context_keys legacy_context legacy_context_merge; do
    airflow dags test "$d"; echo "##### $d exit=$?"
  done'

On main branch, all dags fail and show TypeError: 'module' object is not callable.
On this branch, all dags succeed and show

KNOWN_CONTEXT_KEYS resolved to a set [airflow.task.operators.unusual_prefix_6c88b32f386c586108a845608f09d06a13ea6e33_legacy_known_context_keys.KnownContextKeysOperator]
context keys missing from KNOWN_CONTEXT_KEYS: []
Context resolved to <class 'airflow.sdk.definitions.context.Context'>
Context(...) built: {'ds': '...', 'run_id': 'manual__...'}
context_merge resolved to <function context_merge at 0x...>
after context_merge: region=tw attempt=1

Was generative AI tooling used to co-author this PR?
  • Yes - Claude Code

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@FrankYang0529
FrankYang0529 marked this pull request as ready for review September 7, 2026 05:08
Signed-off-by: PoAn Yang <payang@apache.org>
@potiuk

potiuk commented Sep 25, 2026

Copy link
Copy Markdown
Member

Hello @FrankYang0529 - thank you for your contributions to Apache Airflow!

The Airflow community has introduced a limit of 5 open pull requests at a time for contributors without write access to the repository. You currently have 30 open pull requests, so - as a one-time step of introducing the limit - we closed the ones where maintainers have not engaged yet:

These pull requests stay open because maintainers are already engaged in them - they count towards your limit:

This is not a judgement of you or of your changes. We never told contributors before that opening many pull requests at once was a problem, so there is nothing to feel bad about - and nothing is lost: your branches, commits and the review history stay where they are.

What we ask you to do is to make your first prioritization decision: choose which of the pull requests above matter most to you, and reopen them (up to 5 open at a time, including the ones still open) with the "Reopen pull request" button or gh pr reopen <PR_NUMBER> --repo apache/airflow. Reopen the ones you are ready to follow through - keep them rebased, respond to review comments and fix failing checks.

While your pull requests are waiting for review, the most valuable thing you can do is help in other ways - reviewing other contributors' pull requests, helping with issues, and taking part in the discussions on the devlist and Slack.

Why we introduced the limit, what it means for you and how to reopen or restore a pull request is explained in https://github.com/apache/airflow/blob/main/contributing-docs/32_open_pull_request_limit.rst.


Drafted-by: Claude Code (Opus 5); reviewed by @potiuk before posting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

closed because of open PR limit Closed as a one-time step of introducing the open pull request limit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants