jason810496 commented on PR #66899:
URL: https://github.com/apache/airflow/pull/66899#issuecomment-4456837127

   Tried it. Runtime-binding changes the schema and also breaks the tagged 
union itself, because Pydantic v2 needs the discriminator field to be a 
`Literal`.
   
   ```python
   from typing import Annotated, Literal, Union
   from pydantic import BaseModel, Field, TypeAdapter
   
   # --- Current pattern: Literal type field ---
   class A1(BaseModel):
       type: Literal["A1"] = "A1"
   
   class A2(BaseModel):
       type: Literal["A2"] = "A2"
   
   print(A1.model_json_schema())
   #. {'properties': {'type': {'const': 'A1', 'default': 'A1', 'title': 'Type', 
'type': 'string'}}, 'title': 'A1', 'type': 'object'}
   TypeAdapter(Annotated[Union[A1, A2], Field(discriminator="type")])  # OK
   
   # --- Proposed pattern: runtime-bind in __init__ ---
   class B(BaseModel):
       type: str = "__undefined__"
       def __init__(self, **kw):
           super().__init__(**kw)
           self.type = type(self).__name__
   
   class B1(B): pass
   class B2(B): pass
   
   print(B1.model_json_schema())
   # {'properties': {'type': {'default': '__undefined__', 'title': 'Type', 
'type': 'string'}}, 'title': 'B1', 'type': 'object'}
   print(B1().type)  # 'B1' (instance state only; not reflected in schema)
   
   TypeAdapter(Annotated[Union[B1, B2], Field(discriminator="type")])
   # PydanticUserError: Model 'B1' needs field 'type' to be of type `Literal`
   ```
   
   So `CommsDecoder`'s `TypeAdapter`-based routing relies on the `Literal` 
annotation, and any downstream schema consumer relies on the `const` it 
produces.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to