Skip to content

Commit bbf2c5b

Browse files
authored
Fix Lang-SDK decoder typing for pydantic 2.14 TypeForm signature (#74628)
pydantic 2.14 types TypeAdapter's argument as TypeForm[T], so mypy rejects the Lang-SDK decoder built from get_args(ToManager)[0], which is not a static type expression. Name the ToManager message union ToManagerMessage and build the Lang-SDK message type LangSDKToManager from it; the runtime union is the same object as before. Declare each concrete processor's decoder with its own precise TypeAdapter type instead of on BaseDagFileProcessorProcess: TypeAdapter is invariant, so the Lang-SDK decoder, which accepts a superset of ToManager, cannot override a base TypeAdapter[ToManager].
1 parent 5ab6989 commit bbf2c5b

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎airflow-core/src/airflow/dag_processing/lang_sdk_processor.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from contextlib import suppress
2727
from pathlib import Path
2828
from socket import socket
29-
from typing import TYPE_CHECKING, Annotated, ClassVar, Literal, cast, get_args
29+
from typing import TYPE_CHECKING, Annotated, ClassVar, Literal, cast
3030

3131
import attrs
3232
import msgspec
@@ -42,7 +42,7 @@
4242
BaseDagFileProcessorProcess,
4343
DagFileParseRequest,
4444
DagFileParsingResult,
45-
ToManager,
45+
ToManagerMessage,
4646
)
4747
from airflow.exceptions import DeserializationError
4848
from airflow.models.pool import Pool
@@ -101,6 +101,9 @@ class LangSDKRuntimeSchemaVersion(BaseModel):
101101
type: Literal["LangSDKRuntimeSchemaVersion"] = "LangSDKRuntimeSchemaVersion"
102102

103103

104+
LangSDKToManager = Annotated[LangSDKRuntimeSchemaVersion | ToManagerMessage, Field(discriminator="type")]
105+
106+
104107
def _get_import_timeout(path: str) -> float | None:
105108
"""Return the ``get_dagbag_import_timeout`` policy's timeout for *path*; ``None`` means none."""
106109
timeout = settings.get_dagbag_import_timeout(path)
@@ -165,9 +168,7 @@ class LangSDKDagFileProcessorProcess(BaseDagFileProcessorProcess):
165168
``DagFileParseRequest`` itself, so the request is sent once it has connected.
166169
"""
167170

168-
decoder = TypeAdapter(
169-
Annotated[LangSDKRuntimeSchemaVersion | get_args(ToManager)[0], Field(discriminator="type")]
170-
)
171+
decoder: ClassVar[TypeAdapter[LangSDKToManager]] = TypeAdapter[LangSDKToManager](LangSDKToManager)
171172

172173
_listeners: dict[_Channel, socket]
173174
_parse_request: DagFileParseRequest

‎airflow-core/src/airflow/dag_processing/processor.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class DagFileParsingResult(BaseModel):
135135
type: Literal["DagFileParsingResult"] = "DagFileParsingResult"
136136

137137

138-
ToManager = Annotated[
138+
ToManagerMessage = (
139139
DagFileParsingResult
140140
| GetConnection
141141
| GetVariable
@@ -151,9 +151,10 @@ class DagFileParsingResult(BaseModel):
151151
| GetXComCount
152152
| GetXComSequenceItem
153153
| GetXComSequenceSlice
154-
| MaskSecret,
155-
Field(discriminator="type"),
156-
]
154+
| MaskSecret
155+
)
156+
157+
ToManager = Annotated[ToManagerMessage, Field(discriminator="type")]
157158

158159
ToDagProcessor = Annotated[
159160
DagFileParseRequest
@@ -586,7 +587,6 @@ class BaseDagFileProcessorProcess(WatchedSubprocess, LoggingMixin):
586587
)
587588
"""The file's parse log, which the process closes. Without one, the output is discarded."""
588589
parsing_result: DagFileParsingResult | None = None
589-
decoder: ClassVar[TypeAdapter[ToManager]] = TypeAdapter[ToManager](ToManager)
590590
had_callbacks: bool = False # Track if this process was started with callbacks to prevent stale DAG detection false positives
591591

592592
client: Client | None = None
@@ -711,6 +711,7 @@ class DagFileProcessorProcess(BaseDagFileProcessorProcess):
711711
"""
712712

713713
client: Client
714+
decoder: ClassVar[TypeAdapter[ToManager]] = TypeAdapter[ToManager](ToManager)
714715

715716
_request_handlers: ClassVar[dict[type[BaseModel], RequestHandler[DagFileProcessorProcess]]] = {
716717
**BaseDagFileProcessorProcess._common_request_handlers,

0 commit comments

Comments
 (0)