-
Notifications
You must be signed in to change notification settings - Fork 17.1k
Enforce supervisor schema class name matches its type literal
#66899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jason810496
merged 2 commits into
apache:main
from
jason810496:ci/guard-supervisor-schema-name-and-type-in-sync
May 15, 2026
+81
−3
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
task-sdk/tests/task_sdk/execution_time/test_supervisor_schemas_name_type_sync.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """ | ||
| Guard the invariant that every supervisor-schema body's class | ||
| ``__name__`` equals the value of its ``type`` ``Literal`` discriminator. | ||
|
|
||
| ``CommsDecoder`` routes incoming wire frames against the ``type`` literal | ||
| on each member of a discriminated union, but downstream consumers | ||
| (registry lookups, snapshot codegen, debug output) identify the head | ||
| class by ``__name__``. If the two strings drift, a frame decodes against | ||
| one class then surfaces under a different name -- a silent contract | ||
| break. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Annotated, get_args, get_origin | ||
|
|
||
| import pytest | ||
| from pydantic import BaseModel | ||
|
|
||
| from airflow.dag_processing.processor import ToDagProcessor, ToManager | ||
| from airflow.jobs.triggerer_job_runner import ToTriggerRunner, ToTriggerSupervisor | ||
| from airflow.sdk.execution_time.comms import ToSupervisor, ToTask | ||
|
|
||
|
|
||
| def _members_of_union(union: object) -> tuple[type[BaseModel], ...]: | ||
| """Return the BaseModel classes composing an ``Annotated[A | B | ..., Field(...)]``.""" | ||
| if get_origin(union) is Annotated: | ||
| union = get_args(union)[0] | ||
| return tuple(m for m in get_args(union) if isinstance(m, type) and issubclass(m, BaseModel)) | ||
|
|
||
|
|
||
| # All six supervisor discriminated unions. Triggerer's two unions are | ||
| # not part of the lang-SDK-facing registry, but the same name/type | ||
| # invariant is required for ``CommsDecoder`` to round-trip them. | ||
| SUPERVISOR_UNIONS = [ | ||
| pytest.param(ToTask, id="ToTask"), | ||
| pytest.param(ToSupervisor, id="ToSupervisor"), | ||
| pytest.param(ToManager, id="ToManager"), | ||
| pytest.param(ToDagProcessor, id="ToDagProcessor"), | ||
| pytest.param(ToTriggerRunner, id="ToTriggerRunner"), | ||
| pytest.param(ToTriggerSupervisor, id="ToTriggerSupervisor"), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("union", SUPERVISOR_UNIONS) | ||
| def test_class_name_matches_type_literal(union): | ||
| """For every member, ``cls.__name__`` must equal its ``type`` Literal value.""" | ||
| mismatches: list[str] = [] | ||
| for member in _members_of_union(union): | ||
| field = member.model_fields.get("type") | ||
| if field is None: | ||
| mismatches.append(f"{member.__name__}: missing `type` field") | ||
| continue | ||
| args = get_args(field.annotation) | ||
| if len(args) != 1: | ||
| mismatches.append(f"{member.__name__}: `type` must be a single-value Literal, got {args!r}") | ||
| continue | ||
| literal = args[0] | ||
| if literal != member.__name__: | ||
| mismatches.append(f"{member.__name__}: type literal = {literal!r}, expected {member.__name__!r}") | ||
|
|
||
| assert not mismatches, "Class __name__ must equal its `type` Literal value:\n " + "\n ".join(mismatches) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.