amoghrajesh commented on issue #56739: URL: https://github.com/apache/airflow/issues/56739#issuecomment-3414094862
Thanks @sjyangkevin for the summary, option A seems to be least performant and would involve importing `BaseModel` just to identify a pydantic model. `isinstance` is the most reliable option but not performant enough. Option B could also work but it seems to be more invasive? We could use a slightly modified version of option C, which is to detect dataclasses using standard library as mentioned in the docs: https://docs.pydantic.dev/latest/concepts/dataclasses/ > They provide a similar functionality to stdlib dataclasses with the addition of Pydantic validation. What I mean is something along the lines of: ``` import dataclasses def is_pydantic_model(cls: Any) -> bool: # __pydantic_fields__ is always present on Pydantic V2 models and is a dict[str, FieldInfo] # __pydantic_validator__ is an internal validator object, always set after model build has_pydantic_attrs = hasattr(cls, "__pydantic_fields__") and hasattr(cls, "__pydantic_validator__") return has_pydantic_attrs and not dataclasses.is_dataclass(cls) ``` Example: ``` from pydantic.dataclasses import dataclass @dataclass class BrokenPydanticDataClass: foo: str bar: int is_pydantic_model(BrokenPydanticDataClass) Out[13]: False ``` -- 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]
