1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17
18# This is an util module that makes Pydantic use optional. While we are using Pydantic in the airflow core
19# codebase, we don't want to make it a hard dependency for all the users of the core codebase, because
20# it is only used in the serialization and deserialization of the models for Internal API and for nothing
21# else, and since Pydantic is a very popular library, we don't want to force the users of the core codebase
22# to install specific Pydantic version - especially that a lot of libraries out there still depend on
23# Pydantic 1 and our internal API uses Pydantic 2+
24
25from __future__ import annotations
26
27from importlib import metadata
28
29from packaging import version
30
31
32def is_pydantic_2_installed() -> bool:
33 try:
34 return version.parse(metadata.version("pydantic")).major == 2
35 except ImportError:
36 return False
37
38
39if is_pydantic_2_installed():
40 from pydantic import BaseModel, ConfigDict, PlainSerializer, PlainValidator, ValidationInfo
41else:
42
43 class BaseModel: # type: ignore[no-redef] # noqa: D101
44 def __init__(self, *args, **kwargs):
45 pass
46
47 class ConfigDict: # type: ignore[no-redef] # noqa: D101
48 def __init__(self, *args, **kwargs):
49 pass
50
51 class PlainSerializer: # type: ignore[no-redef] # noqa: D101
52 def __init__(self, *args, **kwargs):
53 pass
54
55 class PlainValidator: # type: ignore[no-redef] # noqa: D101
56 def __init__(self, *args, **kwargs):
57 pass
58
59 class ValidationInfo: # type: ignore[no-redef] # noqa: D101
60 def __init__(self, *args, **kwargs):
61 pass